Blame SOURCES/nodejs-symlink-deps

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