Blame SOURCES/nodejs-symlink-deps

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