Blame SOURCES/nodejs-symlink-deps

27913e
#!/usr/bin/python
27913e
27913e
"""Symlink a node module's dependencies into the node_modules directory so users
27913e
can `npm link` RPM-installed modules into their personal projects."""
27913e
27913e
# Copyright 2012, 2013 T.C. Hollingsworth <tchollingsworth@gmail.com>
27913e
#
27913e
# Permission is hereby granted, free of charge, to any person obtaining a copy
27913e
# of this software and associated documentation files (the "Software"), to
27913e
# deal in the Software without restriction, including without limitation the
27913e
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
27913e
# sell copies of the Software, and to permit persons to whom the Software is
27913e
# furnished to do so, subject to the following conditions:
27913e
#
27913e
# The above copyright notice and this permission notice shall be included in
27913e
# all copies or substantial portions of the Software.
27913e
#
27913e
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27913e
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27913e
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27913e
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27913e
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27913e
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27913e
# IN THE SOFTWARE.
27913e
27913e
import json
27913e
import os
27913e
import shutil
27913e
import sys
27913e
27913e
def symlink(source, dest):
27913e
    try:
27913e
        os.symlink(source, dest)
27913e
    except OSError:
27913e
        if os.path.islink(dest) and os.path.realpath(dest) == os.path.normpath(source):
27913e
            sys.stderr.write("""
27913e
WARNING: the symlink for dependency "{0}" already exists
27913e
27913e
This could mean that the dependency exists in both devDependencies and 
27913e
dependencies, which may cause trouble for people using this module with npm.
27913e
27913e
Please report this to upstream. For more information, see:
27913e
    <https://github.com/tchollingsworth/nodejs-packaging/pull/1>
27913e
""".format(dest))
27913e
            
27913e
        elif '--force' in sys.argv:
27913e
            if os.path.isdir(dest):
27913e
                shutil.rmtree(dest)
27913e
            else:
27913e
                os.unlink(dest)
27913e
                
27913e
            os.symlink(source, dest)
27913e
            
27913e
        else:
27913e
            sys.stderr.write("""
27913e
ERROR: the path for dependency "{0}" already exists
27913e
27913e
This could mean that bundled modules are being installed.  Bundled libraries are
27913e
forbidden in Fedora. For more information, see:
27913e
    <https://fedoraproject.org/wiki/Packaging:No_Bundled_Libraries>
27913e
    
27913e
It is generally reccomended to remove the entire "node_modules" directory in
27913e
%prep when it exists. For more information, see:
27913e
    <https://fedoraproject.org/wiki/Packaging:Node.js#Removing_bundled_modules>
27913e
    
27913e
If you have obtained permission from the Fedora Packaging Committee to bundle
27913e
libraries, please use `%nodejs_fixdep -r` in %prep to remove the dependency on
27913e
the bundled module. This will prevent an unnecessary dependency on the system
27913e
version of the module and eliminate this error.
27913e
""".format(dest))
27913e
            sys.exit(1)
27913e
        
27913e
27913e
def symlink_deps(deps, check):
27913e
    if isinstance(deps, dict):
27913e
        #read in the list of mutiple-versioned packages
27913e
        mvpkgs = open('/opt/rh/rh-nodejs12/root/usr/share/node/multiver_modules').read().split('\n')
27913e
            
27913e
        for dep, ver in deps.items():
27913e
            if dep in mvpkgs and ver != '' and ver != '*' and ver != 'latest':
27913e
                depver = ver.lstrip('~^').split('.')[0]
27913e
                target = os.path.join(sitelib, '{0}@{1}'.format(dep, depver))
27913e
            else:
27913e
                target = os.path.join(sitelib, dep)
27913e
                
27913e
            if not check or os.path.exists(target):
27913e
                symlink(target, dep)
27913e
                
27913e
    elif isinstance(deps, list):
27913e
        for dep in deps:
27913e
            target = os.path.join(sitelib, dep)
27913e
            if not check or os.path.exists(target):
27913e
                symlink(target, dep)
27913e
    
27913e
    elif isinstance(deps, str):
27913e
        target = os.path.join(sitelib, deps)
27913e
        if not check or os.path.exists(target):
27913e
            symlink(target, deps)
27913e
            
27913e
    else:
27913e
        raise TypeError("Invalid package.json: dependencies weren't a recognized type")
27913e
27913e
27913e
#the %nodejs_symlink_deps macro passes %nodejs_sitelib as the first argument
27913e
sitelib = sys.argv[1]
27913e
27913e
if '--check' in sys.argv or '--build' in sys.argv:
27913e
    check = True
27913e
    modules = [os.getcwd()]
27913e
else:
27913e
    check = False
27913e
    br_sitelib = os.path.join(os.environ['RPM_BUILD_ROOT'], sitelib.lstrip('/'))
27913e
    modules = [os.path.join(br_sitelib, module) for module in os.listdir(br_sitelib)]
27913e
27913e
if '--optional' in sys.argv:
27913e
    optional = True
27913e
else:
27913e
    optional = False
27913e
27913e
for path in modules:
27913e
    os.chdir(path)
27913e
    md = json.load(open('package.json'))
27913e
    
27913e
    if 'dependencies' in md or (check and 'devDependencies' in md) or (optional and 'optionalDependencies' in md):
27913e
        try:
27913e
            os.mkdir('node_modules')
27913e
        except OSError:
27913e
            sys.stderr.write('WARNING: node_modules already exists. Make sure you have ' +
27913e
                                'no bundled dependencies.\n')
27913e
27913e
        os.chdir('node_modules')
27913e
27913e
        if 'dependencies' in md:
27913e
            symlink_deps(md['dependencies'], check)
27913e
27913e
        if check and '--no-devdeps' not in sys.argv and 'devDependencies' in md:
27913e
            symlink_deps(md['devDependencies'], check)
27913e
27913e
        if optional and 'optionalDependencies' in md:
27913e
            symlink_deps(md['optionalDependencies'], check)