From 820519301ba9493daa8c1d7509fe72d5424cd43b Mon Sep 17 00:00:00 2001 From: blag Date: Thu, 12 Dec 2019 17:12:28 -0800 Subject: [PATCH] Convert license check to Invoke --- tasks/__init__.py | 5 +++-- tasks/check.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/tasks/__init__.py b/tasks/__init__.py index 87cbac40..f060f582 100644 --- a/tasks/__init__.py +++ b/tasks/__init__.py @@ -18,8 +18,9 @@ def all_(ctx): pass -@task(check.compile_, lint.flake8, lint.pylint, copy.copy_pack_to_subdirectory, - check.configs, check.metadata, tests.packs_resource_register, tests.packs_tests) +@task(check.compile_, check.license, lint.flake8, lint.pylint, + copy.copy_pack_to_subdirectory, check.configs, check.metadata, + tests.packs_resource_register, tests.packs_tests) def all_ci(ctx): pass diff --git a/tasks/check.py b/tasks/check.py index b18bcd9c..b713b419 100644 --- a/tasks/check.py +++ b/tasks/check.py @@ -17,6 +17,47 @@ def compile_(ctx): raise Exception("Could not compile all files") +@task +def license(ctx): + # Verifies repo contains LICENSE file with ASF 2.0 content + print("") + print("==================== license-check ====================") + print("") + apache_license_rgx = re.compile(r'.*Apache License.*') + license_version_rgx = re.compile(r'.*Version 2\.0.*') + apache_url_rgx = re.compile(r'.*www\.apache\.org/licenses/LICENSE-2\.0.*') + files = [] + top_level_git_dir = ctx.run('git rev-parse --show-toplevel').stdout.splitlines()[0] + license_file = os.path.join(top_level_git_dir, 'LICENSE') + with ctx.cd(top_level_git_dir): + files = ctx.run("git diff --relative --diff-filter=ACMRTUXB " + "--name-only {}".format(base_branch)).stdout.splitlines() + if os.environ.get('FORCE_CHECK_ALL_FILES', False) == 'true' or files: + # Check for the existence of a LICENSE file + if not os.path.exist(license_file): + raise Exception("Missing LICENSE file in {root_dir}".format(root_dir=top_level_git_dir)) + + with open(license_file) as f: + lines = f.read().splitlines() + + found_apache_license = False + found_license_version = False + found_apache_url = False + for line in lines: + if apache_license_rgx.match(line): + found_apache_license = True + if license_version_rgx.match(line): + found_license_version = True + if apache_url_rgx.match(line): + found_apache_url = True + if found_apache_license and found_license_version and found_apache_url: + break + else: + raise Exception("LICENSE file doesn't contain Apache 2.0 license text") + else: + print("No files have changed, skipping run...") + + @task(copy.copy_pack_to_subdirectory) def configs(ctx): print("")