|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from glob import glob |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import sys |
| 20 | +import xml.etree.ElementTree |
| 21 | + |
| 22 | +logger = logging.getLogger() |
| 23 | +logger.setLevel(logging.DEBUG) |
| 24 | +handler = logging.StreamHandler(sys.stderr) |
| 25 | +handler.setLevel(logging.DEBUG) |
| 26 | +logger.addHandler(handler) |
| 27 | + |
| 28 | + |
| 29 | +def get_env(key: str) -> str: |
| 30 | + value = os.getenv(key) |
| 31 | + logger.debug(f"Read env {key}: {value}") |
| 32 | + return value |
| 33 | + |
| 34 | + |
| 35 | +def parse_rat_report(fp): |
| 36 | + file_count = 0 |
| 37 | + unapproved_licenses = [] |
| 38 | + approved_count = 0 |
| 39 | + root = xml.etree.ElementTree.parse(fp).getroot() |
| 40 | + |
| 41 | + for resource in root.findall(".//resource"): |
| 42 | + file_name = resource.get("name") |
| 43 | + license_approval_elem = resource.find("license-approval") |
| 44 | + |
| 45 | + if license_approval_elem is not None and license_approval_elem.get("name") == "false": |
| 46 | + file_count += 1 |
| 47 | + unapproved_licenses.append(file_name) |
| 48 | + else: |
| 49 | + approved_count += 1 |
| 50 | + |
| 51 | + return approved_count, file_count, unapproved_licenses |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + """ |
| 56 | + Parse Apache Rat reports and generate GitHub annotations. |
| 57 | + """ |
| 58 | + if not os.getenv("GITHUB_WORKSPACE"): |
| 59 | + print("This script is intended to by run by GitHub Actions.") |
| 60 | + exit(1) |
| 61 | + |
| 62 | + reports = glob(pathname="**/rat/*.xml", recursive=True) |
| 63 | + logger.debug(f"Found {len(reports)} Rat reports") |
| 64 | + |
| 65 | + total_unapproved_licenses = 0 |
| 66 | + total_approved_licenses = 0 |
| 67 | + all_unapproved_files = [] |
| 68 | + |
| 69 | + workspace_path = get_env("GITHUB_WORKSPACE") |
| 70 | + |
| 71 | + for report in reports: |
| 72 | + with open(report, "r") as fp: |
| 73 | + logger.debug(f"Parsing Rat report file: {report}") |
| 74 | + approved_count, unapproved_count, unapproved_files = parse_rat_report(fp) |
| 75 | + |
| 76 | + total_approved_licenses += approved_count |
| 77 | + total_unapproved_licenses += unapproved_count |
| 78 | + all_unapproved_files.extend(unapproved_files) |
| 79 | + |
| 80 | + if total_unapproved_licenses == 0: |
| 81 | + print(f"All {total_approved_licenses} files have approved licenses. No unapproved licenses found.") |
| 82 | + exit(0) |
| 83 | + else: |
| 84 | + print(f"{total_approved_licenses} approved licenses") |
| 85 | + print(f"{total_unapproved_licenses} unapproved licenses") |
| 86 | + |
| 87 | + print("Files with unapproved licenses:") |
| 88 | + for file in all_unapproved_files: |
| 89 | + rel_path = os.path.relpath(file, workspace_path) |
| 90 | + message = f"File with unapproved license: {rel_path}" |
| 91 | + print(f"::notice file={rel_path},title=Unapproved License::{message}") |
| 92 | + |
| 93 | + exit(1) |
0 commit comments