8de3f91
#!/usr/bin/python3
8de3f91
8de3f91
"""Set a package version in a package.json file"""
8de3f91
8de3f91
# Copyright 2018 Tom Hughes <tom@compton.nu>
8de3f91
#
8de3f91
# Permission is hereby granted, free of charge, to any person obtaining a copy
8de3f91
# of this software and associated documentation files (the "Software"), to
8de3f91
# deal in the Software without restriction, including without limitation the
8de3f91
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8de3f91
# sell copies of the Software, and to permit persons to whom the Software is
8de3f91
# furnished to do so, subject to the following conditions:
8de3f91
#
8de3f91
# The above copyright notice and this permission notice shall be included in
8de3f91
# all copies or substantial portions of the Software.
8de3f91
#
8de3f91
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8de3f91
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8de3f91
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8de3f91
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8de3f91
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
8de3f91
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
8de3f91
# IN THE SOFTWARE.
8de3f91
8de3f91
import json
8de3f91
import os
8de3f91
import shutil
8de3f91
import sys
8de3f91
8de3f91
if not os.path.exists('package.json~'):
8de3f91
    shutil.copy2('package.json', 'package.json~')
8de3f91
8de3f91
md = json.load(open('package.json'))
8de3f91
8de3f91
if 'version' in md and sys.argv[1] != md['version']:
8de3f91
    raise RuntimeError('Version is already set to {0}'.format(md['version']))
8de3f91
else:
8de3f91
    md['version'] = sys.argv[1]
8de3f91
8de3f91
fh = open('package.json', 'w')
8de3f91
data = json.JSONEncoder(indent=4).encode(md)
8de3f91
fh.write(data)
8de3f91
fh.close()