Skip to content

Commit dd4873d

Browse files
Re-introducing whitelisting argument to generate.py
1 parent 77c0441 commit dd4873d

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

betterproto/tests/generate.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
11
#!/usr/bin/env python
22
import os
3+
import sys
4+
from typing import Set
5+
6+
from betterproto.tests.util import get_directories, inputs_path, output_path_betterproto, output_path_reference, \
7+
protoc_plugin, protoc_reference
38

49
# Force pure-python implementation instead of C++, otherwise imports
510
# break things because we can't properly reset the symbol database.
611
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
712

813

9-
from betterproto.tests.util import get_directories, protoc_plugin, protoc_reference, root_path
14+
def generate(whitelist: Set[str]):
15+
path_whitelist = {os.path.realpath(e) for e in whitelist if os.path.exists(e)}
16+
name_whitelist = {e for e in whitelist if not os.path.exists(e)}
1017

11-
def main():
12-
os.chdir(root_path)
13-
test_cases_directory = os.path.join(root_path, 'inputs')
14-
test_case = get_directories(test_cases_directory)
18+
test_case_names = set(get_directories(inputs_path))
19+
20+
for test_case_name in test_case_names:
21+
test_case_path = os.path.join(inputs_path, test_case_name)
1522

16-
for test_case_name in test_case:
17-
test_case_path = os.path.join(test_cases_directory, test_case_name)
23+
is_path_whitelisted = path_whitelist and os.path.realpath(test_case_path) in path_whitelist
24+
is_name_whitelisted = name_whitelist and test_case_name in name_whitelist
1825

19-
case_reference_output_dir = os.path.join(root_path, 'output_reference', test_case_name)
20-
case_plugin_output_dir = os.path.join(root_path, 'output_betterproto', test_case_name)
26+
if whitelist and not is_path_whitelisted and not is_name_whitelisted:
27+
continue
28+
29+
case_output_dir_reference = os.path.join(output_path_reference, test_case_name)
30+
case_output_dir_betterproto = os.path.join(output_path_betterproto, test_case_name)
2131

2232
print(f'Generating output for {test_case_name}')
23-
os.makedirs(case_reference_output_dir, exist_ok=True)
24-
os.makedirs(case_plugin_output_dir, exist_ok=True)
33+
os.makedirs(case_output_dir_reference, exist_ok=True)
34+
os.makedirs(case_output_dir_betterproto, exist_ok=True)
35+
36+
protoc_reference(test_case_path, case_output_dir_reference)
37+
protoc_plugin(test_case_path, case_output_dir_betterproto)
38+
39+
40+
HELP = "\n".join([
41+
'Usage: python generate.py',
42+
' python generate.py [DIRECTORIES or NAMES]',
43+
'Generate python classes for standard tests.',
44+
'',
45+
'DIRECTORIES One or more relative or absolute directories of test-cases to generate classes for.',
46+
' python generate.py inputs/bool inputs/double inputs/enum',
47+
'',
48+
'NAMES One or more test-case names to generate classes for.',
49+
' python generate.py bool double enums'
50+
])
51+
52+
53+
def main():
54+
if sys.argv[1] in ('-h', '--help'):
55+
print(HELP)
56+
return
57+
whitelist = set(sys.argv[1:])
2558

26-
protoc_reference(test_case_path, case_reference_output_dir)
27-
protoc_plugin(test_case_path, case_plugin_output_dir)
59+
generate(whitelist)
2860

2961

3062
if __name__ == "__main__":

betterproto/tests/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
root_path = os.path.dirname(os.path.realpath(__file__))
88
inputs_path = os.path.join(root_path, 'inputs')
9+
output_path_reference = os.path.join(root_path, 'output_reference')
10+
output_path_betterproto = os.path.join(root_path, 'output_betterproto')
911

1012
if os.name == 'nt':
1113
plugin_path = os.path.join(root_path, '..', 'plugin.bat')

0 commit comments

Comments
 (0)