|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | | -import argparse, os |
| 3 | +import argparse, os, re |
4 | 4 |
|
5 | | -if __name__ == "__main__": |
| 5 | +if __name__ == '__main__': |
6 | 6 | # Parse CLI arguments |
7 | 7 | parser = argparse.ArgumentParser(description = 'FlexSecure-Applets version patching tool') |
8 | 8 | parser.add_argument('-s', '--source', nargs='?', dest='source', type=str, |
|
13 | 13 | const='255.255.255', default='255.255.255', help='Build version to encode') |
14 | 14 | args = parser.parse_args() |
15 | 15 |
|
16 | | - version = args.version.strip("v") |
| 16 | + version = args.version.strip('v') |
17 | 17 |
|
18 | | - print("Patching file " + args.patch + " using the applet version from " + args.source + " and the additional build version " + version) |
| 18 | + print('Patching file ' + args.patch + ' using the applet version from ' + args.source + ' and the additional build version ' + version) |
19 | 19 |
|
20 | 20 | if(not os.path.isfile(args.source)): |
21 | | - print("error: source file " + args.source + "does not exist") |
| 21 | + print('error: source file ' + args.source + 'does not exist') |
22 | 22 | exit(1) |
| 23 | + |
| 24 | + # Parse version source file |
| 25 | + _, ext = os.path.splitext(args.source) |
| 26 | + with open(args.source, 'r') as f: |
| 27 | + config = f.read() |
| 28 | + if(ext == '.xml'): |
| 29 | + # Ant XML file |
| 30 | + matches = re.findall("cap.*version[\s='\"]*([^\s'\"]*)", config, flags=re.DOTALL) |
| 31 | + elif(ext == '.gradle'): |
| 32 | + # Gradle config |
| 33 | + matches = re.findall("javacard {.*version[\s=']*([^\s']*)", config, flags=re.DOTALL) |
| 34 | + else: |
| 35 | + print('error: Unknown source file format: ' + ext) |
| 36 | + |
| 37 | + if(len(matches) == 0): |
| 38 | + print("error: Cannot find version in config file") |
| 39 | + exit(1) |
| 40 | + appversion = matches[0] |
| 41 | + print("info: Found source version: " + appversion) |
23 | 42 |
|
24 | 43 | if(not os.path.isfile(args.patch)): |
25 | | - print("error: patch file " + args.patch + "does not exist") |
| 44 | + print('error: patch file ' + args.patch + 'does not exist') |
26 | 45 | exit(1) |
0 commit comments