11#!/usr/bin/env python3
2- """ Driver script to run all code generation """
2+ """Driver script to run all code generation"""
33
44import argparse
55import logging
99import typing
1010import shlex
1111
12- if ' BUILD_WORKSPACE_DIRECTORY' not in os .environ :
12+ if " BUILD_WORKSPACE_DIRECTORY" not in os .environ :
1313 # we are not running with `bazel run`, set up module search path
1414 _repo_root = pathlib .Path (__file__ ).resolve ().parents [2 ]
1515 sys .path .append (str (_repo_root ))
@@ -29,57 +29,105 @@ def _parse_args() -> argparse.Namespace:
2929 conf = None
3030
3131 p = argparse .ArgumentParser (description = "Code generation suite" )
32- p .add_argument ("--generate" , type = lambda x : x .split ("," ),
33- help = "specify what targets to generate as a comma separated list, choosing among dbscheme, ql, "
34- "trap, cpp and rust" )
35- p .add_argument ("--verbose" , "-v" , action = "store_true" , help = "print more information" )
32+ p .add_argument (
33+ "--generate" ,
34+ type = lambda x : x .split ("," ),
35+ help = "specify what targets to generate as a comma separated list, choosing among dbscheme, ql, "
36+ "trap, cpp and rust" ,
37+ )
38+ p .add_argument (
39+ "--verbose" , "-v" , action = "store_true" , help = "print more information"
40+ )
3641 p .add_argument ("--quiet" , "-q" , action = "store_true" , help = "only print errors" )
37- p .add_argument ("--configuration-file" , "-c" , type = _abspath , default = conf ,
38- help = "A configuration file to load options from. By default, the first codegen.conf file found by "
39- "going up directories from the current location. If present all paths provided in options are "
40- "considered relative to its directory" )
41- p .add_argument ("--root-dir" , type = _abspath ,
42- help = "the directory that should be regarded as the root of the language pack codebase. Used to "
43- "compute QL imports and in some comments and as root for relative paths provided as options. "
44- "If not provided it defaults to the directory of the configuration file, if any" )
42+ p .add_argument (
43+ "--configuration-file" ,
44+ "-c" ,
45+ type = _abspath ,
46+ default = conf ,
47+ help = "A configuration file to load options from. By default, the first codegen.conf file found by "
48+ "going up directories from the current location. If present all paths provided in options are "
49+ "considered relative to its directory" ,
50+ )
51+ p .add_argument (
52+ "--root-dir" ,
53+ type = _abspath ,
54+ help = "the directory that should be regarded as the root of the language pack codebase. Used to "
55+ "compute QL imports and in some comments and as root for relative paths provided as options. "
56+ "If not provided it defaults to the directory of the configuration file, if any" ,
57+ )
4558 path_arguments = [
46- p .add_argument ("--schema" ,
47- help = "input schema file (default schema.py)" ),
48- p .add_argument ("--dbscheme" ,
49- help = "output file for dbscheme generation, input file for trap generation" ),
50- p .add_argument ("--ql-output" ,
51- help = "output directory for generated QL files" ),
52- p .add_argument ("--ql-stub-output" ,
53- help = "output directory for QL stub/customization files. Defines also the "
54- "generated qll file importing every class file" ),
55- p .add_argument ("--ql-test-output" ,
56- help = "output directory for QL generated extractor test files" ),
57- p .add_argument ("--ql-cfg-output" ,
58- help = "output directory for QL CFG layer (optional)." ),
59- p .add_argument ("--cpp-output" ,
60- help = "output directory for generated C++ files, required if trap or cpp is provided to "
61- "--generate" ),
62- p .add_argument ("--rust-output" ,
63- help = "output directory for generated Rust files, required if rust is provided to "
64- "--generate" ),
65- p .add_argument ("--generated-registry" ,
66- help = "registry file containing information about checked-in generated code. A .gitattributes"
67- "file is generated besides it to mark those files with linguist-generated=true. Must"
68- "be in a directory containing all generated code." ),
59+ p .add_argument ("--schema" , help = "input schema file (default schema.py)" ),
60+ p .add_argument (
61+ "--dbscheme" ,
62+ help = "output file for dbscheme generation, input file for trap generation" ,
63+ ),
64+ p .add_argument ("--ql-output" , help = "output directory for generated QL files" ),
65+ p .add_argument (
66+ "--ql-stub-output" ,
67+ help = "output directory for QL stub/customization files. Defines also the "
68+ "generated qll file importing every class file" ,
69+ ),
70+ p .add_argument (
71+ "--ql-test-output" ,
72+ help = "output directory for QL generated extractor test files" ,
73+ ),
74+ p .add_argument (
75+ "--ql-cfg-output" , help = "output directory for QL CFG layer (optional)."
76+ ),
77+ p .add_argument (
78+ "--cpp-output" ,
79+ help = "output directory for generated C++ files, required if trap or cpp is provided to "
80+ "--generate" ,
81+ ),
82+ p .add_argument (
83+ "--rust-output" ,
84+ help = "output directory for generated Rust files, required if rust is provided to "
85+ "--generate" ,
86+ ),
87+ p .add_argument (
88+ "--generated-registry" ,
89+ help = "registry file containing information about checked-in generated code. A .gitattributes"
90+ "file is generated besides it to mark those files with linguist-generated=true. Must"
91+ "be in a directory containing all generated code." ,
92+ ),
6993 ]
70- p .add_argument ("--script-name" ,
71- help = "script name to put in header comments of generated files. By default, the path of this "
72- "script relative to the root directory" )
73- p .add_argument ("--trap-library" ,
74- help = "path to the trap library from an include directory, required if generating C++ trap bindings" ),
75- p .add_argument ("--ql-format" , action = "store_true" , default = True ,
76- help = "use codeql to autoformat QL files (which is the default)" )
77- p .add_argument ("--no-ql-format" , action = "store_false" , dest = "ql_format" , help = "do not format QL files" )
78- p .add_argument ("--codeql-binary" , default = "codeql" , help = "command to use for QL formatting (default %(default)s)" )
79- p .add_argument ("--force" , "-f" , action = "store_true" ,
80- help = "generate all files without skipping unchanged files and overwriting modified ones" )
81- p .add_argument ("--use-current-directory" , action = "store_true" ,
82- help = "do not consider paths as relative to --root-dir or the configuration directory" )
94+ p .add_argument (
95+ "--script-name" ,
96+ help = "script name to put in header comments of generated files. By default, the path of this "
97+ "script relative to the root directory" ,
98+ )
99+ p .add_argument (
100+ "--trap-library" ,
101+ help = "path to the trap library from an include directory, required if generating C++ trap bindings" ,
102+ ),
103+ p .add_argument (
104+ "--ql-format" ,
105+ action = "store_true" ,
106+ default = True ,
107+ help = "use codeql to autoformat QL files (which is the default)" ,
108+ )
109+ p .add_argument (
110+ "--no-ql-format" ,
111+ action = "store_false" ,
112+ dest = "ql_format" ,
113+ help = "do not format QL files" ,
114+ )
115+ p .add_argument (
116+ "--codeql-binary" ,
117+ default = "codeql" ,
118+ help = "command to use for QL formatting (default %(default)s)" ,
119+ )
120+ p .add_argument (
121+ "--force" ,
122+ "-f" ,
123+ action = "store_true" ,
124+ help = "generate all files without skipping unchanged files and overwriting modified ones" ,
125+ )
126+ p .add_argument (
127+ "--use-current-directory" ,
128+ action = "store_true" ,
129+ help = "do not consider paths as relative to --root-dir or the configuration directory" ,
130+ )
83131 opts = p .parse_args ()
84132 if opts .configuration_file is not None :
85133 with open (opts .configuration_file ) as config :
@@ -97,7 +145,15 @@ def _parse_args() -> argparse.Namespace:
97145 for arg in path_arguments :
98146 path = getattr (opts , arg .dest )
99147 if path is not None :
100- setattr (opts , arg .dest , _abspath (path ) if opts .use_current_directory else (opts .root_dir / path ))
148+ setattr (
149+ opts ,
150+ arg .dest ,
151+ (
152+ _abspath (path )
153+ if opts .use_current_directory
154+ else (opts .root_dir / path )
155+ ),
156+ )
101157 if not opts .script_name :
102158 opts .script_name = paths .exe_file .relative_to (opts .root_dir )
103159 return opts
@@ -115,7 +171,7 @@ def run():
115171 log_level = logging .ERROR
116172 else :
117173 log_level = logging .INFO
118- logging .basicConfig (format = "{levelname} {message}" , style = '{' , level = log_level )
174+ logging .basicConfig (format = "{levelname} {message}" , style = "{" , level = log_level )
119175 for target in opts .generate :
120176 generate (target , opts , render .Renderer (opts .script_name ))
121177
0 commit comments