Blame SOURCES/nodejs-fixdep

3cc9b1
#!/usr/bin/python
3cc9b1
3cc9b1
"""Modify a dependency listed in a package.json file"""
3cc9b1
3cc9b1
# Copyright 2013 T.C. Hollingsworth <tchollingsworth@gmail.com>
3cc9b1
#
3cc9b1
# Permission is hereby granted, free of charge, to any person obtaining a copy
3cc9b1
# of this software and associated documentation files (the "Software"), to
3cc9b1
# deal in the Software without restriction, including without limitation the
3cc9b1
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3cc9b1
# sell copies of the Software, and to permit persons to whom the Software is
3cc9b1
# furnished to do so, subject to the following conditions:
3cc9b1
#
3cc9b1
# The above copyright notice and this permission notice shall be included in
3cc9b1
# all copies or substantial portions of the Software.
3cc9b1
#
3cc9b1
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3cc9b1
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3cc9b1
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3cc9b1
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3cc9b1
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3cc9b1
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3cc9b1
# IN THE SOFTWARE.
3cc9b1
3cc9b1
import json
3cc9b1
import optparse
3cc9b1
import os
3cc9b1
import re
3cc9b1
import shutil
3cc9b1
import sys
3cc9b1
3cc9b1
RE_VERSION = re.compile(r'\s*v?([<>=~^]{0,2})\s*([0-9][0-9\.\-]*)\s*')
3cc9b1
3cc9b1
p = optparse.OptionParser(
3cc9b1
        description='Modifies dependency entries in package.json files')
3cc9b1
3cc9b1
p.add_option('-r', '--remove', action='store_true')
3cc9b1
p.add_option('--dev', action='store_const', const='devDependencies', 
3cc9b1
             dest='deptype', help='affect devDependencies')
3cc9b1
p.add_option('--optional', action='store_const', const='optionalDependencies', 
3cc9b1
             dest='deptype', help='affect optionalDependencies')
3cc9b1
p.add_option('--caret', action='store_true',
3cc9b1
             help='convert all or specified dependencies to use the caret operator')
3cc9b1
3cc9b1
options, args = p.parse_args()
3cc9b1
3cc9b1
if not os.path.exists('package.json~'):
3cc9b1
    shutil.copy2('package.json', 'package.json~')
3cc9b1
3cc9b1
md = json.load(open('package.json'))
3cc9b1
3cc9b1
deptype = options.deptype if options.deptype is not None else 'dependencies'
3cc9b1
3cc9b1
if deptype not in md:
3cc9b1
    md[deptype] = {}
3cc9b1
3cc9b1
# convert alternate JSON dependency representations to a dictionary
3cc9b1
if not options.caret and not isinstance(md[deptype], dict):
3cc9b1
    if isinstance(md[deptype], list):
3cc9b1
        deps = md[deptype]
3cc9b1
        md[deptype] = {}
3cc9b1
        for dep in deps:
3cc9b1
            md[deptype][dep] = '*'
3cc9b1
    elif isinstance(md[deptype], basestring):
3cc9b1
        md[deptype] = { md[deptype] : '*' }
3cc9b1
3cc9b1
if options.remove:
3cc9b1
    dep = args[0]
3cc9b1
    del md[deptype][dep]
3cc9b1
elif options.caret:
3cc9b1
    if not isinstance(md[deptype], dict):
3cc9b1
        sys.stderr.write('All dependencies are unversioned.  Unable to apply ' +
3cc9b1
                         'caret operator.\n')
3cc9b1
        sys.exit(2)
3cc9b1
        
3cc9b1
    deps = args if len(args) > 0 else md[deptype].keys()
3cc9b1
    for dep in deps:
3cc9b1
        if md[deptype][dep][0] == '^':
3cc9b1
            continue
3cc9b1
        elif md[deptype][dep][0] in ('~','0','1','2','3','4','5','6','7','8','9'):
3cc9b1
            ver = re.match(RE_VERSION, md[deptype][dep]).group(2)
3cc9b1
            md[deptype][dep] = '^' + ver
3cc9b1
        else:
3cc9b1
            sys.stderr.write('Attempted to convert non-numeric or tilde ' +
3cc9b1
                'dependency to caret.  This is not permitted.\n')
3cc9b1
            sys.exit(1)
3cc9b1
else:
3cc9b1
    dep = args[0]
3cc9b1
3cc9b1
    if len(args) > 1:
3cc9b1
        ver = args[1]
3cc9b1
    else:
3cc9b1
        ver = '*'
3cc9b1
3cc9b1
    md[deptype][dep] = ver
3cc9b1
3cc9b1
fh = open('package.json', 'w')
3cc9b1
data = json.JSONEncoder(indent=4).encode(md)
3cc9b1
fh.write(data)
3cc9b1
fh.close()