Blame SOURCES/nodejs-fixdep

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