Blame SOURCES/nodejs-fixdep

fb565e
#!/usr/bin/python
fb565e
fb565e
"""Modify a dependency listed in a package.json file"""
fb565e
fb565e
# Copyright 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 optparse
fb565e
import os
fb565e
import re
fb565e
import shutil
fb565e
import sys
fb565e
fb565e
RE_VERSION = re.compile(r'\s*v?([<>=~^]{0,2})\s*([0-9][0-9\.\-]*)\s*')
fb565e
fb565e
p = optparse.OptionParser(
fb565e
        description='Modifies dependency entries in package.json files')
fb565e
fb565e
p.add_option('-r', '--remove', action='store_true')
fb565e
p.add_option('-m', '--move', action='store_true')
fb565e
p.add_option('--dev', action='store_const', const='devDependencies', 
fb565e
             dest='deptype', help='affect devDependencies')
fb565e
p.add_option('--optional', action='store_const', const='optionalDependencies', 
fb565e
             dest='deptype', help='affect optionalDependencies')
fb565e
p.add_option('--caret', action='store_true',
fb565e
             help='convert all or specified dependencies to use the caret operator')
fb565e
fb565e
options, args = p.parse_args()
fb565e
fb565e
if not os.path.exists('package.json~'):
fb565e
    shutil.copy2('package.json', 'package.json~')
fb565e
fb565e
md = json.load(open('package.json'))
fb565e
fb565e
deptype = options.deptype if options.deptype is not None else 'dependencies'
fb565e
fb565e
if deptype not in md:
fb565e
    md[deptype] = {}
fb565e
fb565e
# convert alternate JSON dependency representations to a dictionary
fb565e
if not options.caret and not isinstance(md[deptype], dict):
fb565e
    if isinstance(md[deptype], list):
fb565e
        deps = md[deptype]
fb565e
        md[deptype] = {}
fb565e
        for dep in deps:
fb565e
            md[deptype][dep] = '*'
fb565e
    elif isinstance(md[deptype], str):
fb565e
        md[deptype] = { md[deptype] : '*' }
fb565e
fb565e
if options.remove:
fb565e
    dep = args[0]
fb565e
    del md[deptype][dep]
fb565e
elif options.move:
fb565e
    dep = args[0]
fb565e
    ver = None
fb565e
    for fromtype in ['dependencies', 'optionalDependencies', 'devDependencies']:
fb565e
        if fromtype in md:
fb565e
            if isinstance(md[fromtype], dict) and dep in md[fromtype]:
fb565e
                ver = md[fromtype][dep]
fb565e
                del md[fromtype][dep]
fb565e
            elif isinstance(md[fromtype], list) and md[fromtype].count(dep) > 0:
fb565e
                ver = '*'
fb565e
                md[fromtype].remove(dep)
fb565e
            elif isinstance(md[fromtype], str) and md[fromtype] == dep:
fb565e
                ver = '*'
fb565e
                del md[fromtype]
fb565e
    if ver != None:
fb565e
        md[deptype][dep] = ver
fb565e
elif options.caret:
fb565e
    if not isinstance(md[deptype], dict):
fb565e
        sys.stderr.write('All dependencies are unversioned.  Unable to apply ' +
fb565e
                         'caret operator.\n')
fb565e
        sys.exit(2)
fb565e
        
fb565e
    deps = args if len(args) > 0 else md[deptype].keys()
fb565e
    for dep in deps:
fb565e
        if md[deptype][dep][0] == '^':
fb565e
            continue
fb565e
        elif md[deptype][dep][0] in ('~','0','1','2','3','4','5','6','7','8','9'):
fb565e
            ver = re.match(RE_VERSION, md[deptype][dep]).group(2)
fb565e
            md[deptype][dep] = '^' + ver
fb565e
        else:
fb565e
            sys.stderr.write('Attempted to convert non-numeric or tilde ' +
fb565e
                'dependency to caret.  This is not permitted.\n')
fb565e
            sys.exit(1)
fb565e
else:
fb565e
    dep = args[0]
fb565e
fb565e
    if len(args) > 1:
fb565e
        ver = args[1]
fb565e
    else:
fb565e
        ver = '*'
fb565e
fb565e
    md[deptype][dep] = ver
fb565e
fb565e
fh = open('package.json', 'w')
fb565e
data = json.JSONEncoder(indent=4).encode(md)
fb565e
fh.write(data)
fb565e
fh.close()