diff options
| author | 2022-10-24 18:36:22 -0700 | |
|---|---|---|
| committer | 2022-10-24 18:46:03 -0700 | |
| commit | e0fab08a33cf9d1aa9e3be531974ed1c9e74a5e6 (patch) | |
| tree | 0b00aa8357efd4a2fe5e106ed2a652652c027529 /tools/fileslist_util.py | |
| parent | 3946ea03181421c9d4a2ad0ff494e415540c0ee6 (diff) | |
Convert fileslist_util.py to python 3
Also update it from getopt to argparse.
Bug: 203436762
Test: m $OUT/installed-files-root.txt
Change-Id: I006a36feab01cbbd76dc7d7c1c0675197848212a
Diffstat (limited to 'tools/fileslist_util.py')
| -rwxr-xr-x | tools/fileslist_util.py | 63 |
1 files changed, 24 insertions, 39 deletions
diff --git a/tools/fileslist_util.py b/tools/fileslist_util.py index ff40d51a02..a1b1197cb2 100755 --- a/tools/fileslist_util.py +++ b/tools/fileslist_util.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright (C) 2016 The Android Open Source Project # @@ -15,7 +15,9 @@ # limitations under the License. # -import getopt, json, sys +import argparse +import json +import sys def PrintFileNames(path): with open(path) as jf: @@ -27,42 +29,25 @@ def PrintCanonicalList(path): with open(path) as jf: data = json.load(jf) for line in data: - print "{0:12d} {1}".format(line["Size"], line["Name"]) - -def PrintUsage(name): - print(""" -Usage: %s -[nc] json_files_list - -n produces list of files only - -c produces classic installed-files.txt -""" % (name)) - -def main(argv): - try: - opts, args = getopt.getopt(argv[1:], "nc", "") - except getopt.GetoptError, err: - print(err) - PrintUsage(argv[0]) - sys.exit(2) - - if len(opts) == 0: - print("No conversion option specified") - PrintUsage(argv[0]) - sys.exit(2) - - if len(args) == 0: - print("No input file specified") - PrintUsage(argv[0]) - sys.exit(2) - - for o, a in opts: - if o == ("-n"): - PrintFileNames(args[0]) - sys.exit() - elif o == ("-c"): - PrintCanonicalList(args[0]) - sys.exit() - else: - assert False, "Unsupported option" + print(f"{line['Size']:12d} {line['Name']}") + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-n", action="store_true", + help="produces list of files only") + parser.add_argument("-c", action="store_true", + help="produces classic installed-files.txt") + parser.add_argument("json_files_list") + args = parser.parse_args() + + if args.n and args.c: + sys.exit("Cannot specify both -n and -c") + elif args.n: + PrintFileNames(args.json_files_list) + elif args.c: + PrintCanonicalList(args.json_files_list) + else: + sys.exit("No conversion option specified") if __name__ == '__main__': - main(sys.argv) + main() |