Blame SOURCES/nodejs-fixdep

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