|
1 | 1 | #! /bin/env python3
|
2 | 2 |
|
3 | 3 | from argparse import ArgumentParser
|
4 |
| -from subprocess import check_call |
| 4 | +from subprocess import check_call, check_output |
5 | 5 | from pathlib import Path
|
6 | 6 |
|
7 | 7 | import logging
|
|
11 | 11 | API_LIST_FILE = 'high_entropy_list.csv'
|
12 | 12 | API_LIST_TARGET_FILE = 'chromium_api_list.csv'
|
13 | 13 |
|
| 14 | +COMMIT_POSITION_HEADER = 'Cr-Commit-Position: ' |
| 15 | + |
14 | 16 |
|
15 | 17 | def Main():
|
16 | 18 | parser = ArgumentParser()
|
17 | 19 | parser.add_argument('--build_path', '-C', type=Path, required=True)
|
18 | 20 | parser.add_argument('--build',
|
19 | 21 | '-B',
|
20 |
| - type=bool, |
21 |
| - help='Whether the API list should be rebuilt', |
22 |
| - default=False) |
| 22 | + action='store_true', |
| 23 | + help='Whether the API list should be rebuilt') |
23 | 24 | parser.add_argument('--target_path', '-t', type=Path, default=Path.cwd())
|
| 25 | + parser.add_argument('--verbose', '-v', action='store_true') |
24 | 26 | parser.add_argument(
|
25 |
| - '--commmit', |
26 |
| - type=bool, |
27 |
| - default=False, |
| 27 | + '--commit', |
| 28 | + action='store_true', |
28 | 29 | help='Git commit after a successful extraction of the API list')
|
29 | 30 |
|
30 | 31 | args = parser.parse_args()
|
31 | 32 |
|
| 33 | + if args.verbose: |
| 34 | + logging.basicConfig(level=logging.DEBUG) |
| 35 | + |
32 | 36 | if not args.build_path.exists():
|
33 |
| - logging.critical('Build directory does not exist') |
| 37 | + logging.critical('Build directory does not exist. Checked {}'.format( |
| 38 | + args.build_path)) |
34 | 39 | sys.exit(1)
|
35 | 40 |
|
36 | 41 | if not args.target_path.exists():
|
@@ -60,6 +65,35 @@ def Main():
|
60 | 65 | with target_file.open('w') as w:
|
61 | 66 | w.writelines(contents)
|
62 | 67 |
|
| 68 | + if args.commit: |
| 69 | + commit_position = check_output(['git', 'rev-parse', 'HEAD'], |
| 70 | + cwd=args.build_path) |
| 71 | + commit_message = check_output(['git', 'cat-file', '-p', 'HEAD'], |
| 72 | + cwd=args.build_path).splitlines() |
| 73 | + for l in commit_message: |
| 74 | + ls = l.decode() |
| 75 | + if ls.startswith(COMMIT_POSITION_HEADER): |
| 76 | + commit_position = ls[len(COMMIT_POSITION_HEADER):].trim() |
| 77 | + break |
| 78 | + |
| 79 | + git_status = check_output(['git', 'status', '--porcelain=v1'], |
| 80 | + cwd=args.target_path).splitlines() |
| 81 | + if len(git_status) == 0: |
| 82 | + logging.info('No change to API list') |
| 83 | + elif len(git_status) != 1: |
| 84 | + logging.error( |
| 85 | + 'There is more than one changed file in the repository. ' + |
| 86 | + 'Can\'t commit changes.') |
| 87 | + elif git_status[0] != 'M\t{}'.format(API_LIST_TARGET_FILE): |
| 88 | + logging.error('Unexpected changes found in the repository') |
| 89 | + else: |
| 90 | + check_call([ |
| 91 | + 'git', 'commit', '-m', |
| 92 | + '\'API list update from {}\''.format(commit_position), '--', |
| 93 | + API_LIST_TARGET_FILE |
| 94 | + ], |
| 95 | + cwd=args.target_path.parent()) |
| 96 | + |
63 | 97 |
|
64 | 98 | if __name__ == "__main__":
|
65 | 99 | Main()
|
0 commit comments