Blame SOURCES/nodejs-symlink-deps

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