|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# Copyright Contributors to the OpenTimelineIO project |
| 5 | + |
| 6 | +__doc__ = """Manage and apply the version in the OTIO_VERSION.json file""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import sys |
| 10 | +import json |
| 11 | + |
| 12 | +OTIO_VERSION_JSON_PATH = "OTIO_VERSION.json" |
| 13 | + |
| 14 | + |
| 15 | +def version(): |
| 16 | + with open(OTIO_VERSION_JSON_PATH, 'r') as fi: |
| 17 | + return json.load(fi)['version'] |
| 18 | + |
| 19 | + |
| 20 | +def _parsed_args(): |
| 21 | + parser = argparse.ArgumentParser( |
| 22 | + description='Fetch a list of contributors for a given GitHub repo.' |
| 23 | + ) |
| 24 | + |
| 25 | + op_grp = parser.add_mutually_exclusive_group(required=True) |
| 26 | + op_grp.add_argument( |
| 27 | + "-i", |
| 28 | + "--increment", |
| 29 | + type=str, |
| 30 | + default=None, |
| 31 | + choices=("major", "minor", "patch"), |
| 32 | + help="Increment either the major or minor version number." |
| 33 | + ) |
| 34 | + op_grp.add_argument( |
| 35 | + "-s", |
| 36 | + "--set", |
| 37 | + type=str, |
| 38 | + default=None, |
| 39 | + nargs=3, |
| 40 | + help="Set the version string, in the form of MAJOR MINOR PATCH" |
| 41 | + ) |
| 42 | + op_grp.add_argument( |
| 43 | + "-q", |
| 44 | + "--query", |
| 45 | + default=False, |
| 46 | + action="store_true", |
| 47 | + help="Query/print the current version without changing it" |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "-d", |
| 51 | + "--dryrun", |
| 52 | + default=False, |
| 53 | + action="store_true", |
| 54 | + help="Perform actions but modify no files on disk." |
| 55 | + ) |
| 56 | + return parser.parse_args() |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + args = _parsed_args() |
| 61 | + |
| 62 | + major, minor, patch = (int(v) for v in version()) |
| 63 | + |
| 64 | + if args.increment == "major": |
| 65 | + major += 1 |
| 66 | + minor = 0 |
| 67 | + patch = 0 |
| 68 | + elif args.increment == "minor": |
| 69 | + minor += 1 |
| 70 | + patch = 0 |
| 71 | + elif args.increment == "patch": |
| 72 | + patch += 1 |
| 73 | + elif args.set: |
| 74 | + major, minor, patch = args.set |
| 75 | + elif args.query: |
| 76 | + print(".".join(str(v) for v in (major, minor, patch))) |
| 77 | + return |
| 78 | + |
| 79 | + print("Setting version to: {}.{}.{}".format(major, minor, patch)) |
| 80 | + |
| 81 | + # update the OTIO_VERSION file |
| 82 | + with open(OTIO_VERSION_JSON_PATH, "w") as fo: |
| 83 | + fo.write( |
| 84 | + json.dumps({"version": [str(v) for v in (major, minor, patch)]}) |
| 85 | + ) |
| 86 | + print("Updated {}".format(OTIO_VERSION_JSON_PATH)) |
| 87 | + |
| 88 | + # update the CMakeLists.txt |
| 89 | + with open("CMakeLists.txt", 'r') as fi: |
| 90 | + cmake_input = fi.read() |
| 91 | + |
| 92 | + cmake_output = [] |
| 93 | + key_map = {"MAJOR": major, "MINOR": minor, "PATCH": patch} |
| 94 | + for ln in cmake_input.split("\n"): |
| 95 | + for label, new_value in key_map.items(): |
| 96 | + if "set(OTIO_VERSION_{} \"".format(label) in ln: |
| 97 | + cmake_output.append( |
| 98 | + "set(OTIO_VERSION_{} \"{}\")".format(label, new_value) |
| 99 | + ) |
| 100 | + break |
| 101 | + else: |
| 102 | + cmake_output.append(ln) |
| 103 | + |
| 104 | + with open("CMakeLists.txt", 'w') as fo: |
| 105 | + fo.write("\n".join(cmake_output)) |
| 106 | + print("Updated {}".format("CMakeLists.txt")) |
| 107 | + |
| 108 | + # update the setup.py |
| 109 | + with open("setup.py", 'r') as fi: |
| 110 | + setup_input = fi.read() |
| 111 | + |
| 112 | + setup_output = [] |
| 113 | + for ln in setup_input.split("\n"): |
| 114 | + if "\"version\": " in ln: |
| 115 | + |
| 116 | + setup_output.append( |
| 117 | + " \"version\": \"{}.{}.{}{}\",".format( |
| 118 | + major, |
| 119 | + minor, |
| 120 | + patch, |
| 121 | + (".dev1" in ln) and ".dev1" or "" |
| 122 | + ) |
| 123 | + ) |
| 124 | + else: |
| 125 | + setup_output.append(ln) |
| 126 | + |
| 127 | + with open("setup.py", 'w') as fo: |
| 128 | + fo.write("\n".join(setup_output)) |
| 129 | + print("Updated {}".format("setup.py")) |
| 130 | + |
| 131 | + |
| 132 | +def add_suffix(content, version): |
| 133 | + if version not in content: |
| 134 | + sys.stderr.write( |
| 135 | + "Version {} not found, suffix may have already been " |
| 136 | + "added.\n".format(version) |
| 137 | + ) |
| 138 | + return False |
| 139 | + |
| 140 | + print("adding suffix, version will be: {}".format(version + ".dev1")) |
| 141 | + content.replace(version, version + ".dev1") |
| 142 | + return True |
| 143 | + |
| 144 | + |
| 145 | +def remove_suffix(content, version): |
| 146 | + if version + '.dev1' not in content: |
| 147 | + sys.stderr.write( |
| 148 | + "Version+Suffix {} not found, suffix may have already been " |
| 149 | + "removed.\n".format(version + '.dev1') |
| 150 | + ) |
| 151 | + return False |
| 152 | + |
| 153 | + content.replace(version + ' .dev1', version) |
| 154 | + return True |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
0 commit comments