|
6 | 6 | import sys |
7 | 7 |
|
8 | 8 |
|
9 | | -def create_settings_template(product_location: str, include_comments: bool, verbose: bool) -> None: |
| 9 | +def remove_duplicate_properties(prop_files): |
| 10 | + unique = {} |
| 11 | + for path in prop_files: |
| 12 | + filename = os.path.basename(path) |
| 13 | + if filename not in unique: |
| 14 | + unique[filename] = path |
| 15 | + return list(unique.values()) |
| 16 | + |
| 17 | + |
| 18 | +def get_settings_from_properties( |
| 19 | + out_f, tmp_zip_dir: str, include_comments: bool, verbose: bool |
| 20 | +) -> None: |
| 21 | + # find all *preferences.properties files |
| 22 | + pattern = tmp_zip_dir + "/**/*preferences.properties" |
| 23 | + prop_files = glob.glob(pattern, recursive=True) |
| 24 | + prop_files_unique = remove_duplicate_properties(prop_files) |
| 25 | + |
| 26 | + package_str = "" |
| 27 | + for prop_file in prop_files_unique: |
| 28 | + if verbose: |
| 29 | + print("+ {}".format(prop_file).replace("/./tmp-zip/", "/")) |
| 30 | + with open(prop_file, "r") as file: |
| 31 | + lines = file.readlines() |
| 32 | + for line in lines: |
| 33 | + line = line.strip() |
| 34 | + if line.startswith("# Package "): |
| 35 | + package_str = line[10:].strip() |
| 36 | + # print package name with number signs above and below |
| 37 | + if include_comments: |
| 38 | + out_f.write("\n{0}\n{1}\n{0}\n".format("#" * (len(line)), line)) |
| 39 | + else: |
| 40 | + out_f.write("\n{0}\n{1}\n{0}\n\n".format("#" * (len(line)), line)) |
| 41 | + # if verbose: |
| 42 | + # print("| {} ({})".format(" " * len(jar_file), package_str)) |
| 43 | + elif "--------" in line: |
| 44 | + continue |
| 45 | + # assume equal sign means this is a property |
| 46 | + elif "=" in line: |
| 47 | + if line[0] == "#": |
| 48 | + if include_comments: |
| 49 | + out_f.write("# {}/{}\n".format(package_str, line[1:].strip())) |
| 50 | + else: |
| 51 | + out_f.write("# {}/{}\n".format(package_str, line)) |
| 52 | + # a few pva properties don't have equal sign so this covers those |
| 53 | + elif line != "" and line[0] != "#": |
| 54 | + out_f.write("# {}/{}\n".format(package_str, line)) |
| 55 | + else: |
| 56 | + if include_comments: |
| 57 | + out_f.write(line + "\n") |
| 58 | + |
| 59 | + |
| 60 | +def create_settings_template( |
| 61 | + product_location: str, |
| 62 | + include_comments: bool, |
| 63 | + verbose: bool, |
| 64 | + properties_directory: str, |
| 65 | +) -> None: |
10 | 66 | """ |
11 | 67 | Create complete list of settings for settings.ini file |
12 | 68 |
|
13 | 69 | :param product_location: Location of the product jar files to check for *preferences.properties files |
14 | 70 | :param include_comments: Option to include comments for each setting in output file |
15 | 71 | :param verbose: Verbose operation |
| 72 | + :param properties_directory: Using src directory instead of jar files |
16 | 73 | """ |
17 | | - # find all jar files so we can unzip jar and find preference files |
18 | | - jar_file_list = glob.glob(product_location + "/*.jar") |
19 | | - if len(jar_file_list) <= 0: |
20 | | - sys.stderr.write("No *.jar files found in '{}'\n".format(product_location)) |
21 | | - sys.stderr.write("Need to build sources?\n") |
22 | | - sys.exit(-1) |
23 | | - jar_file_list = sorted(jar_file_list, key=str.lower) |
24 | | - |
25 | | - # temp directory to hold unzipped jar file contents (deleted at end of script) |
26 | | - tmp_zip_dir = "./tmp-zip" |
27 | 74 |
|
28 | | - output_file = "settings_template.ini" |
29 | | - out_f = open(output_file, 'w') |
30 | 75 | print("Creating settings_template.ini file...") |
| 76 | + output_file = "settings_template.ini" |
| 77 | + out_f = open(output_file, "w") |
| 78 | + out_f.write( |
| 79 | + "# Complete List of Available Preference Properties (Created by create_settings_template.py)\n" |
| 80 | + ) |
31 | 81 |
|
32 | | - out_f.write("# Complete List of Available Preference Properties (Created by create_settings_template.py)\n") |
| 82 | + if not properties_directory: |
| 83 | + print("Parsing properties in jar files") |
| 84 | + # find all jar files so we can unzip jar and find preference files |
| 85 | + jar_file_list = glob.glob(product_location + "/*.jar") |
| 86 | + if len(jar_file_list) <= 0: |
| 87 | + sys.stderr.write("No *.jar files found in '{}'\n".format(product_location)) |
| 88 | + sys.stderr.write("Need to build sources?\n") |
| 89 | + sys.exit(-1) |
| 90 | + jar_file_list = sorted(jar_file_list, key=str.lower) |
33 | 91 |
|
34 | | - for jar_file in jar_file_list: |
35 | | - if verbose: |
36 | | - print("| {}".format(jar_file)) |
37 | | - if not os.path.isdir(tmp_zip_dir): |
38 | | - os.makedirs(tmp_zip_dir) |
39 | | - with zipfile.ZipFile(jar_file, 'r') as zip_ref: |
40 | | - zip_ref.extractall(tmp_zip_dir) |
41 | | - # find all *preference.properties files |
42 | | - prop_files = glob.glob(tmp_zip_dir + "/*preferences.properties") |
43 | | - |
44 | | - package_str = "" |
45 | | - for prop_file in prop_files: |
| 92 | + # temp directory to hold unzipped jar file contents (deleted at end of script) |
| 93 | + tmp_zip_dir = "./tmp-zip" |
| 94 | + |
| 95 | + for jar_file in jar_file_list: |
46 | 96 | if verbose: |
47 | | - print("+ {}/{}".format(jar_file, prop_file).replace("/./tmp-zip/", "/")) |
48 | | - with open(prop_file, 'r') as file: |
49 | | - lines = file.readlines() |
50 | | - for line in lines: |
51 | | - line = line.strip() |
52 | | - if line.startswith("# Package "): |
53 | | - package_str = line[10:].strip() |
54 | | - # print package name with number signs above and below |
55 | | - if include_comments: |
56 | | - out_f.write("\n{0}\n{1}\n{0}\n".format("#"*(len(line)), line)) |
57 | | - else: |
58 | | - out_f.write("\n{0}\n{1}\n{0}\n\n".format("#"*(len(line)), line)) |
59 | | - if verbose: |
60 | | - print("| {} ({})".format(" " * len(jar_file), package_str)) |
61 | | - elif "--------" in line: |
62 | | - continue |
63 | | - # assume equal sign means this is a property |
64 | | - elif "=" in line: |
65 | | - if line[0] == "#": |
66 | | - if include_comments: |
67 | | - out_f.write("# {}/{}\n".format(package_str, line[1:].strip())) |
68 | | - else: |
69 | | - out_f.write("# {}/{}\n".format(package_str, line)) |
70 | | - # a few pva properties don't have equal sign so this covers those |
71 | | - elif line != "" and line[0] != "#": |
72 | | - out_f.write("# {}/{}\n".format(package_str, line)) |
73 | | - else: |
74 | | - if include_comments: |
75 | | - out_f.write(line + "\n") |
| 97 | + print("| {}".format(jar_file)) |
| 98 | + if not os.path.isdir(tmp_zip_dir): |
| 99 | + os.makedirs(tmp_zip_dir) |
| 100 | + with zipfile.ZipFile(jar_file, "r") as zip_ref: |
| 101 | + zip_ref.extractall(tmp_zip_dir) |
| 102 | + |
| 103 | + get_settings_from_properties(out_f, tmp_zip_dir, include_comments, verbose) |
| 104 | + |
| 105 | + # remove temp directory |
| 106 | + shutil.rmtree(tmp_zip_dir) |
| 107 | + else: |
| 108 | + print(f"Parsing properties in : {args.directory}") |
| 109 | + get_settings_from_properties( |
| 110 | + out_f, properties_directory, include_comments, verbose |
| 111 | + ) |
76 | 112 |
|
77 | | - # remove temp directory |
78 | | - shutil.rmtree(tmp_zip_dir) |
79 | 113 | print("Creation complete") |
80 | 114 |
|
81 | 115 |
|
82 | | -parser = argparse.ArgumentParser(description="Create template of settings.ini with all available settings") |
83 | | -parser.add_argument("product", type=str, nargs='?', default="./target/lib", help="Location of product jars. Defaults to ./target/lib") |
84 | | -parser.add_argument("-c", "--comments", action="store_true", help="Include setting comments in file") |
| 116 | +parser = argparse.ArgumentParser( |
| 117 | + description="Create template of settings.ini with all available settings" |
| 118 | +) |
| 119 | +parser.add_argument( |
| 120 | + "product", |
| 121 | + type=str, |
| 122 | + nargs="?", |
| 123 | + default="./target/lib", |
| 124 | + help="Location of product jars. Defaults to ./target/lib", |
| 125 | +) |
| 126 | +parser.add_argument( |
| 127 | + "-c", "--comments", action="store_true", help="Include setting comments in file" |
| 128 | +) |
85 | 129 | parser.add_argument("-v", "--verbose", action="store_true", help="Verbose operation") |
86 | | - |
| 130 | +parser.add_argument( |
| 131 | + "-d", |
| 132 | + "--directory", |
| 133 | + type=str, |
| 134 | + help="Specify the source directory instead of using compiled jar", |
| 135 | +) |
87 | 136 | args = parser.parse_args() |
88 | 137 |
|
89 | | -create_settings_template(args.product, args.comments, args.verbose) |
| 138 | +print(args.directory) |
| 139 | +create_settings_template(args.product, args.comments, args.verbose, args.directory) |
0 commit comments