|
| 1 | +import sys |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | + |
| 5 | +def update_and_verify_version(new_version): |
| 6 | + file_path = 'linebot/__about__.py' |
| 7 | + |
| 8 | + # Update version |
| 9 | + with open(file_path, 'r') as file: |
| 10 | + content = file.read() |
| 11 | + |
| 12 | + new_content = re.sub( |
| 13 | + r"__version__ = '.*?'", |
| 14 | + f"__version__ = '{new_version}'", |
| 15 | + content |
| 16 | + ) |
| 17 | + |
| 18 | + with open(file_path, 'w') as file: |
| 19 | + file.write(new_content) |
| 20 | + |
| 21 | + print(f"Updated version to {new_version} in {file_path}") |
| 22 | + |
| 23 | + # verify version |
| 24 | + match = re.search(r"__version__ = '(.*?)'", new_content) |
| 25 | + if not match: |
| 26 | + raise ValueError("Version string not found in the file.") |
| 27 | + |
| 28 | + actual_version = match.group(1) |
| 29 | + if actual_version != new_version: |
| 30 | + raise ValueError(f"Version mismatch: expected {new_version}, found {actual_version}") |
| 31 | + |
| 32 | + print(f"Version verified: {actual_version}") |
| 33 | + |
| 34 | + # diff check just in case |
| 35 | + try: |
| 36 | + result = subprocess.run(['git', 'diff', '--numstat', file_path], capture_output=True, text=True, check=True) |
| 37 | + changed_lines = result.stdout.strip().split('\n') |
| 38 | + added_lines = 0 |
| 39 | + deleted_lines = 0 |
| 40 | + |
| 41 | + for line in changed_lines: |
| 42 | + added, deleted = map(int, line.split('\t')[:2]) |
| 43 | + added_lines += added |
| 44 | + deleted_lines += deleted |
| 45 | + |
| 46 | + if added_lines != 1 or deleted_lines != 1: |
| 47 | + raise ValueError(f"Unexpected number of changed lines: expected 1 added and 1 deleted, found {added_lines} added and {deleted_lines} deleted") |
| 48 | + |
| 49 | + print('Git diff verification passed: 1 line added and 1 line deleted.') |
| 50 | + |
| 51 | + # Show diff |
| 52 | + diff_result = subprocess.run(['git', 'diff', '--color=always', file_path], capture_output=True, text=True, check=True) |
| 53 | + print('Git diff output:\n', diff_result.stdout) |
| 54 | + |
| 55 | + except subprocess.CalledProcessError as e: |
| 56 | + print(f"Error during git diff verification: {e}") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + if len(sys.argv) != 2: |
| 61 | + print("Usage: python update_version.py <new_version>") |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + new_version = sys.argv[1] |
| 65 | + |
| 66 | + try: |
| 67 | + update_and_verify_version(new_version) |
| 68 | + except ValueError as e: |
| 69 | + print(e) |
| 70 | + sys.exit(1) |
0 commit comments