github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/bin/get_version (about) 1 #!/usr/bin/env python3 2 3 # Copyright 2016, 2017 Intel Corporation 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # ------------------------------------------------------------------------------ 17 18 import os 19 import subprocess 20 import sys 21 22 top_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 23 24 version_file = top_dir + "/VERSION" 25 26 with open(version_file, 'r') as f: 27 version_data = f.read().strip() 28 29 30 def bump_version(version): 31 (major, minor, patch) = version.split('.') 32 if 'rc' in patch: 33 parts = patch.split('rc') 34 parts[1] = str(int(parts[1]) + 1) 35 patch = "rc".join(parts) 36 else: 37 patch = str(int(patch) + 1) 38 return ".".join([major, minor, patch]) 39 40 41 def auto_version(default, strict): 42 output = subprocess.check_output(['git', 'describe', '--dirty']) 43 parts = output.decode('utf-8').strip().split('-', 3) 44 parts[0] = parts[0][1:] # strip the leading 'v' 45 if len(parts) > 1: 46 parts[0] = bump_version(parts[0]) 47 if default != parts[0]: 48 msg = "VERSION file and (bumped?) git describe versions differ: " \ 49 "{} != {}".format(default, parts[0]) 50 if strict: 51 print("ERROR: " + msg, file=sys.stderr) 52 sys.exit(1) 53 else: 54 print("WARNING: " + msg, file=sys.stderr) 55 print( 56 "WARNING: using setup.py version {}".format(default), 57 file=sys.stderr) 58 parts[0] = default 59 60 if len(parts) > 1: 61 parts[0] = ".dev".join([parts[0], parts[1].replace("-", ".")]) 62 if len(parts) == 4: 63 parts[0] = parts[0] + "-" + parts[3] 64 return parts[0] 65 else: 66 return parts[0] 67 68 69 def version(default): 70 if 'VERSION' in os.environ: 71 if os.environ['VERSION'] == 'AUTO_STRICT': 72 version = auto_version(default, strict=True) 73 elif os.environ['VERSION'] == 'AUTO': 74 version = auto_version(default, strict=False) 75 else: 76 version = os.environ['VERSION'] 77 else: 78 version = default + ".dev1" 79 return version 80 81 82 print(version(version_data))