Skip to content

Commit 55eb9fb

Browse files
authored
Merge pull request #19712 from github/redsun82/python-black
Add `black` pre-commit hook
2 parents 22c490c + 2a160e0 commit 55eb9fb

29 files changed

+3487
-1651
lines changed

.github/workflows/codegen.yml renamed to .github/workflows/python-tooling.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
name: Codegen
1+
name: Python tooling
22

33
on:
44
pull_request:
55
paths:
66
- "misc/bazel/**"
77
- "misc/codegen/**"
8+
- "misc/scripts/models-as-data/bulk_generate_mad.py"
89
- "*.bazel*"
910
- .github/workflows/codegen.yml
1011
- .pre-commit-config.yaml
@@ -17,17 +18,14 @@ permissions:
1718
contents: read
1819

1920
jobs:
20-
codegen:
21+
check-python-tooling:
2122
runs-on: ubuntu-latest
2223
steps:
2324
- uses: actions/checkout@v4
24-
- uses: actions/setup-python@v4
25-
with:
26-
python-version-file: 'misc/codegen/.python-version'
2725
- uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507
2826
name: Check that python code is properly formatted
2927
with:
30-
extra_args: autopep8 --all-files
28+
extra_args: black --all-files
3129
- name: Run codegen tests
3230
shell: bash
3331
run: |

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ repos:
1414
hooks:
1515
- id: clang-format
1616

17-
- repo: https://github.com/pre-commit/mirrors-autopep8
18-
rev: v2.0.4
17+
- repo: https://github.com/psf/black
18+
rev: 25.1.0
1919
hooks:
20-
- id: autopep8
21-
files: ^misc/codegen/.*\.py
20+
- id: black
21+
files: ^(misc/codegen/.*|misc/scripts/models-as-data/bulk_generate_mad)\.py$
2222

2323
- repo: local
2424
hooks:

misc/codegen/codegen.py

Lines changed: 108 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
""" Driver script to run all code generation """
2+
"""Driver script to run all code generation"""
33

44
import argparse
55
import logging
@@ -9,7 +9,7 @@
99
import typing
1010
import 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

misc/codegen/generators/cppgen.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ def _get_trap_name(cls: schema.Class, p: schema.Property) -> str | None:
4949
return inflection.pluralize(trap_name)
5050

5151

52-
def _get_field(cls: schema.Class, p: schema.Property, add_or_none_except: typing.Optional[str] = None) -> cpp.Field:
52+
def _get_field(
53+
cls: schema.Class,
54+
p: schema.Property,
55+
add_or_none_except: typing.Optional[str] = None,
56+
) -> cpp.Field:
5357
args = dict(
5458
field_name=p.name + ("_" if p.name in cpp.cpp_keywords else ""),
5559
base_type=_get_type(p.type, add_or_none_except),
@@ -83,14 +87,15 @@ def _get_class(self, name: str) -> cpp.Class:
8387
bases=[self._get_class(b) for b in cls.bases],
8488
fields=[
8589
_get_field(cls, p, self._add_or_none_except)
86-
for p in cls.properties if "cpp_skip" not in p.pragmas and not p.synth
90+
for p in cls.properties
91+
if "cpp_skip" not in p.pragmas and not p.synth
8792
],
8893
final=not cls.derived,
8994
trap_name=trap_name,
9095
)
9196

9297
def get_classes(self):
93-
ret = {'': []}
98+
ret = {"": []}
9499
for k, cls in self._classmap.items():
95100
if not cls.synth:
96101
ret.setdefault(cls.group, []).append(self._get_class(cls.name))
@@ -102,6 +107,12 @@ def generate(opts, renderer):
102107
processor = Processor(schemaloader.load_file(opts.schema))
103108
out = opts.cpp_output
104109
for dir, classes in processor.get_classes().items():
105-
renderer.render(cpp.ClassList(classes, opts.schema,
106-
include_parent=bool(dir),
107-
trap_library=opts.trap_library), out / dir / "TrapClasses")
110+
renderer.render(
111+
cpp.ClassList(
112+
classes,
113+
opts.schema,
114+
include_parent=bool(dir),
115+
trap_library=opts.trap_library,
116+
),
117+
out / dir / "TrapClasses",
118+
)

0 commit comments

Comments
 (0)