Blame SOURCES/nodejs-fixdep

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