-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenSources.py
executable file
·159 lines (135 loc) · 5 KB
/
genSources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os.path
import argparse
from pathlib import Path
import semver
from generator_code.target import Target
# comments mostly generated by ChatGPT from openai and tweaked by me
# This block of code will only be executed if this script is run directly,
# rather than being imported by another script.
if __name__ == "__main__":
# define the message that will be displayed when the user runs the script with the -h flag
msg = "A code generator for the unit system library.\n"
msg += "This script generates all units and contains all of the unit definitions."
# create an ArgumentParser object to parse command line arguments
parser = argparse.ArgumentParser(
description=msg
)
# define the 'outDir' argument, which is optional, should be a string, and has a default value of ''
parser.add_argument(
"--outDir",
"-o",
help="Put all files in the same given directory. This does not work with the 'all' target.",
required=False,
default='',
dest='outDir',
type=str,
)
parser.add_argument(
"-v", "--verbose",
help="increase output verbosity",
required=False,
default=False,
dest='verbose',
action='store_true',
)
parser.add_argument(
"--printOutFiles",
help="set to true if the generated file should be print (enabled when --verbose is set)",
required=False,
default=False,
dest='printOutFiles',
action='store_true',
)
parser.add_argument(
"--no_format",
help="set this flag to disable running the formatter on the generated files",
required=False,
default=True,
dest='format_sources',
action='store_false',
)
# create an archive flag, which is a flag to directly produce a release archive
parser.add_argument(
"--archive",
help="give this flag to generate a tar.zstd archive of the output directory",
required=False,
default=False,
dest='archive',
action='store_true',
)
parser.add_argument(
"--clean",
help="set this flag to clean the output directory before generating the files",
required=False,
default=False,
dest='clean',
action='store_true',
)
parser.add_argument(
"--test",
help="set this flag to run the test commands after generating the files. Warning: this might require external "
"programs to be installed in your system.",
required=False,
default=False,
dest='test',
action='store_true',
)
parser.add_argument(
"--set_version",
help="use this to pass a version to the generator. It has to be parsable by semver.",
required=False,
default='0.8.0',
dest='version',
type=str,
)
parser.add_argument(
"--no-post-gen",
help="set this flag to disable running the post generation commands",
required=False,
default=True,
dest='post_gen',
action='store_false',
)
# add a subparser to select the target for the code generator
subparser_manager = parser.add_subparsers(help="generator target", dest="target")
Target.init_subparser(subparser_manager)
# parse the command line arguments
args = vars(parser.parse_args())
# get the directory containing the script
main_script_dir = Path(os.path.dirname(__file__)).absolute().expanduser()
if args['outDir'] != '':
if args['target'] == 'all':
raise ValueError('Cannot specify an output directory when generating all targets')
if args['version'].startswith('v'):
args['version'] = args['version'][1:]
# get the generator target(s) from the command line arguments
generator_targets = Target.get_targets(
version=semver.VersionInfo.parse(args['version']),
main_script_dir=main_script_dir,
output_dir=args['outDir'],
print_files=args['printOutFiles'] or args['verbose'],
target_name=args['target'],
clean_output_dir=args['clean'],
verbose=args['verbose'],
)
for generator_target in generator_targets:
print(f'Current target: {generator_target.target_name}')
print('Generating...')
generator_target.generate()
print('Generation done without errors.')
if args['format_sources']:
print('Formatting...')
fmt = generator_target.format()
print('Formatting done by', fmt, 'without errors.')
if args['post_gen']:
print('running post generation commands...')
generator_target.post_generate()
print('post generation commands done without errors.')
if args['test']:
print('Testing...')
generator_target.test()
print('Testing done without errors.')
if args['archive']:
print('Archiving...')
generator_target.archive()
print('Archiving done without errors.')