Blame SOURCES/nodejs-fixdep

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