|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | 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 |
3 | 8 |
|
4 | 9 | # Force pure-python implementation instead of C++, otherwise imports
|
5 | 10 | # break things because we can't properly reset the symbol database.
|
6 | 11 | os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
7 | 12 |
|
8 | 13 |
|
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)} |
10 | 17 |
|
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) |
15 | 22 |
|
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 |
18 | 25 |
|
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) |
21 | 31 |
|
22 | 32 | 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:]) |
25 | 58 |
|
26 |
| - protoc_reference(test_case_path, case_reference_output_dir) |
27 |
| - protoc_plugin(test_case_path, case_plugin_output_dir) |
| 59 | + generate(whitelist) |
28 | 60 |
|
29 | 61 |
|
30 | 62 | if __name__ == "__main__":
|
|
0 commit comments