|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Update Cppcheck version |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# release: python3 release-set-version.py 2.18.0 |
| 7 | +# debug: python3 release-set-version.py 2.18.99 |
| 8 | + |
| 9 | +import glob |
| 10 | +import re |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +def git(args): |
| 16 | + return subprocess.check_output(['git'] + args).decode('utf-8').strip() |
| 17 | + |
| 18 | + |
| 19 | +def sed(args): |
| 20 | + return subprocess.check_output(['sed'] + args).decode('utf-8').strip() |
| 21 | + |
| 22 | + |
| 23 | +def egrep(args): |
| 24 | + try: |
| 25 | + return subprocess.check_output(['egrep'] + args).decode('utf-8').strip() |
| 26 | + except Exception: |
| 27 | + return '' |
| 28 | + |
| 29 | + |
| 30 | +# is there uncommitted changes in folder |
| 31 | +def is_uncommitted_changes(): |
| 32 | + for f in git(['status', '--short']).split('\n'): |
| 33 | + if not f.startswith('??'): |
| 34 | + return True |
| 35 | + return False |
| 36 | + |
| 37 | + |
| 38 | +# get current git branch |
| 39 | +def get_current_branch(): |
| 40 | + return git(['branch', '--show-current']) |
| 41 | + |
| 42 | + |
| 43 | +def set_version(new_version:str): |
| 44 | + if re.match(r'2[.][0-9][0-9][.][0-9]{1,2}([.][0-9])?', new_version) is None: |
| 45 | + print(f'Aborting, invalid version {new_version}') |
| 46 | + return |
| 47 | + |
| 48 | + v = new_version.split('.') |
| 49 | + |
| 50 | + if is_uncommitted_changes(): |
| 51 | + print('Aborting, there are uncommitted changes') |
| 52 | + #return |
| 53 | + |
| 54 | + is_dev_version = (len(v) == 3 and v[-1] == '99') |
| 55 | + |
| 56 | + if not is_dev_version: |
| 57 | + expected_branch = v[0] + '.' + v[1] + '.x' |
| 58 | + if get_current_branch() != expected_branch: |
| 59 | + print(f'Aborting, must be executed from {expected_branch} branch') |
| 60 | + return |
| 61 | + |
| 62 | + v3 = '.'.join(v[:3]) |
| 63 | + |
| 64 | + cppcheck_version_string = (v[0] + '.' + str(int(v[1])+1) + ' dev') if is_dev_version else new_version |
| 65 | + cppcheck_version = ','.join((v+['0','0','0','0'])[:4]) |
| 66 | + |
| 67 | + def check_sed(args): |
| 68 | + file = args[-1] |
| 69 | + res = re.match(r's/([^/]+)/.*', args[-2]) |
| 70 | + if res is None: |
| 71 | + raise Exception('Failed to verify sed call argument ' + args[-2]) |
| 72 | + pattern = res.group(1) |
| 73 | + if len(egrep([pattern, file])) < 4: |
| 74 | + print(f"WARNING: pattern '{pattern}' not found in file {file}") |
| 75 | + sed(args) |
| 76 | + |
| 77 | + check_sed(['-i', '-r', f's/version 2[.][0-9]+([.][0-9]+)*/version {new_version}/', 'cli/main.cpp']) |
| 78 | + check_sed(['-i', '-r', f's/VERSION 2[.][0-9]+[.]99/VERSION {v3}/', 'CMakeLists.txt']) # version must have 3 parts. |
| 79 | + check_sed(['-i', '-r', f's/#define CPPCHECK_VERSION_STRING .*/#define CPPCHECK_VERSION_STRING "{cppcheck_version_string}"/', 'lib/version.h']) |
| 80 | + check_sed(['-i', '-r', f's/#define CPPCHECK_VERSION .*/#define CPPCHECK_VERSION {cppcheck_version}/', 'lib/version.h']) |
| 81 | + check_sed(['-i', '-r', f's/<[?]define ProductName[ ]*=.*/<?define ProductName = "Cppcheck $(var.Platform) {cppcheck_version_string}" ?>/', 'win_installer/productInfo.wxi']) |
| 82 | + check_sed(['-i', '-r', f's/<[?]define ProductVersion[ ]*=.*/<?define ProductVersion = "{new_version}" ?>/', 'win_installer/productInfo.wxi']) |
| 83 | + for g in glob.glob('man/*.md'): |
| 84 | + check_sed(['-i', '-r', f's/subtitle: Version 2\.[0-9].*/subtitle: Version {cppcheck_version_string}/', g]) |
| 85 | + print('Versions have been changed.') |
| 86 | + print('') |
| 87 | + print('Please double check these results below:') |
| 88 | + for p in ('[^.0-9]2[.][0-9]+[.][0-9]', '2[.][0-9]+ dev'): |
| 89 | + for ext in ('cpp', 'h', 'md'): |
| 90 | + for g in glob.glob(f'*/*.{ext}'): |
| 91 | + s = egrep([p, g]) |
| 92 | + if s != '': |
| 93 | + print(f'{g}: {s}') |
| 94 | + print('') |
| 95 | + print("Please double check output of 'git diff'") |
| 96 | + commit_message = f'bumped version to {new_version}' if is_dev_version else f'{new_version}: Set version' |
| 97 | + print(f"git commit -a -m {commit_message}") |
| 98 | + |
| 99 | +if len(sys.argv) != 2: |
| 100 | + print('Syntax: python3 release-set-version.py [version]') |
| 101 | + sys.exit(1) |
| 102 | + |
| 103 | +set_version(sys.argv[-1]) |
| 104 | + |
0 commit comments