Yihan Dong | 7382037 | 2022-11-30 18:06:20 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2022 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """ |
| 17 | Checks and generates a report for gts modules that should be open-sourced. |
| 18 | |
| 19 | Usage: |
| 20 | generate_gts_open_source_report.py |
| 21 | --gtsv-metalic [gts-verifier meta_lic] |
| 22 | --gts-test-metalic [android-gts meta_lic] |
| 23 | --checkshare [COMPLIANCE_CHECKSHARE] |
| 24 | --gts-test-dir [directory of android-gts] |
| 25 | --output [output file] |
| 26 | |
| 27 | Output example: |
| 28 | GTS-Verifier: PASS/FAIL |
| 29 | GTS-Modules: PASS/FAIL |
| 30 | GtsIncrementalInstallTestCases_BackgroundProcess |
| 31 | GtsUnsignedNetworkStackTestCases |
| 32 | """ |
| 33 | import sys |
| 34 | import argparse |
| 35 | import subprocess |
| 36 | import re |
| 37 | |
| 38 | def _get_args(): |
| 39 | """Parses input arguments.""" |
| 40 | parser = argparse.ArgumentParser() |
| 41 | parser.add_argument( |
| 42 | '--gtsv-metalic', required=True, |
| 43 | help='license meta_lic file path of gts-verifier.zip') |
| 44 | parser.add_argument( |
| 45 | '--gts-test-metalic', required=True, |
| 46 | help='license meta_lic file path of android-gts.zip') |
| 47 | parser.add_argument( |
| 48 | '--checkshare', required=True, |
| 49 | help='path of the COMPLIANCE_CHECKSHARE tool') |
| 50 | parser.add_argument( |
| 51 | '--gts-test-dir', required=True, |
| 52 | help='directory of android-gts') |
| 53 | parser.add_argument( |
| 54 | '-o', '--output', required=True, |
| 55 | help='file path of the output report') |
| 56 | return parser.parse_args() |
| 57 | |
| 58 | def _check_gtsv(checkshare: str, gtsv_metalic: str) -> str: |
| 59 | """Checks gts-verifier license. |
| 60 | |
| 61 | Args: |
| 62 | checkshare: path of the COMPLIANCE_CHECKSHARE tool |
| 63 | gtsv_metalic: license meta_lic file path of gts-verifier.zip |
| 64 | |
| 65 | Returns: |
| 66 | PASS when gts-verifier.zip doesn't need to be shared, and FAIL |
| 67 | when gts-verifier.zip need to be shared. |
| 68 | """ |
| 69 | cmd = f'{checkshare} {gtsv_metalic}' |
| 70 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 71 | stderr=subprocess.PIPE) |
| 72 | proc.communicate() |
| 73 | return 'PASS' if proc.returncode == 0 else 'FAIL' |
| 74 | |
| 75 | def _check_gts_test(checkshare: str, gts_test_metalic: str, |
| 76 | gts_test_dir: str) -> tuple[str, set[str]]: |
| 77 | """Checks android-gts license. |
| 78 | |
| 79 | Args: |
| 80 | checkshare: path of the COMPLIANCE_CHECKSHARE tool |
| 81 | gts_test_metalic: license meta_lic file path of android-gts.zip |
| 82 | gts_test_dir: directory of android-gts |
| 83 | |
| 84 | Returns: |
| 85 | Check result (PASS when android-gts doesn't need to be shared, |
| 86 | FAIL when some gts modules need to be shared) and gts modules |
| 87 | that need to be shared. |
| 88 | """ |
| 89 | cmd = f'{checkshare} {gts_test_metalic}' |
| 90 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 91 | stderr=subprocess.PIPE) |
| 92 | _, str_stderr = map(lambda b: b.decode(), proc.communicate()) |
| 93 | if proc.returncode == 0: |
| 94 | return 'PASS', [] |
| 95 | open_source_modules = set() |
| 96 | for error_line in str_stderr.split('\n'): |
| 97 | # Skip the empty liness |
| 98 | if not error_line: |
| 99 | continue |
| 100 | module_meta_lic = error_line.strip().split()[0] |
| 101 | groups = re.fullmatch( |
| 102 | re.compile(f'.*/{gts_test_dir}/(.*)'), module_meta_lic) |
| 103 | if groups: |
| 104 | open_source_modules.add( |
| 105 | groups[1].removesuffix('.meta_lic')) |
| 106 | return 'FAIL', open_source_modules |
| 107 | |
| 108 | |
| 109 | def main(argv): |
| 110 | args = _get_args() |
| 111 | |
| 112 | gtsv_metalic = args.gtsv_metalic |
| 113 | gts_test_metalic = args.gts_test_metalic |
| 114 | output_file = args.output |
| 115 | checkshare = args.checkshare |
| 116 | gts_test_dir = args.gts_test_dir |
| 117 | |
| 118 | with open(output_file, 'w') as file: |
| 119 | result = _check_gtsv(checkshare, gtsv_metalic) |
| 120 | file.write(f'GTS-Verifier: {result}\n') |
| 121 | result, open_source_modules = _check_gts_test( |
| 122 | checkshare, gts_test_metalic, gts_test_dir) |
| 123 | file.write(f'GTS-Modules: {result}\n') |
| 124 | for open_source_module in open_source_modules: |
| 125 | file.write(f'\t{open_source_module}\n') |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | main(sys.argv) |