Skip to content

Commit 0e910da

Browse files
committed
Convert license check to Invoke
1 parent 653c4f5 commit 0e910da

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

tasks/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def all_(ctx):
1818
pass
1919

2020

21-
@task(check.compile_, lint.flake8, lint.pylint, copy.copy_pack_to_subdirectory,
22-
check.configs, check.metadata, tests.packs_resource_register, tests.packs_tests)
21+
@task(check.compile_, check.license, lint.flake8, lint.pylint,
22+
copy.copy_pack_to_subdirectory, check.configs, check.metadata,
23+
tests.packs_resource_register, tests.packs_tests)
2324
def all_ci(ctx):
2425
pass
2526

tasks/check.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,48 @@ def compile_(ctx):
1717
raise Exception("Could not compile all files")
1818

1919

20+
@task
21+
def license(ctx):
22+
# Verifies repo contains LICENSE file with ASF 2.0 content
23+
print("")
24+
print("==================== license-check ====================")
25+
print("")
26+
apache_license_rgx = re.compile(r'.*Apache License.*')
27+
license_version_rgx = re.compile(r'.*Version 2\.0.*')
28+
apache_url_rgx = re.compile(r'.*www\.apache\.org/licenses/LICENSE-2\.0.*')
29+
files = []
30+
top_level_git_dir = ctx.run('git rev-parse --show-toplevel').stdout.splitlines()[0]
31+
base_branch = os.environ.get('BASE_BRANCH', 'origin/master')
32+
license_file = os.path.join(top_level_git_dir, 'LICENSE')
33+
with ctx.cd(top_level_git_dir):
34+
files = ctx.run("git diff --relative --diff-filter=ACMRTUXB "
35+
"--name-only {}".format(base_branch)).stdout.splitlines()
36+
if os.environ.get('FORCE_CHECK_ALL_FILES', False) == 'true' or files:
37+
# Check for the existence of a LICENSE file
38+
if not os.path.exist(license_file):
39+
raise Exception("Missing LICENSE file in {root_dir}".format(root_dir=top_level_git_dir))
40+
41+
with open(license_file) as f:
42+
lines = f.read().splitlines()
43+
44+
found_apache_license = False
45+
found_license_version = False
46+
found_apache_url = False
47+
for line in lines:
48+
if apache_license_rgx.match(line):
49+
found_apache_license = True
50+
if license_version_rgx.match(line):
51+
found_license_version = True
52+
if apache_url_rgx.match(line):
53+
found_apache_url = True
54+
if found_apache_license and found_license_version and found_apache_url:
55+
break
56+
else:
57+
raise Exception("LICENSE file doesn't contain Apache 2.0 license text")
58+
else:
59+
print("No files have changed, skipping run...")
60+
61+
2062
@task(copy.copy_pack_to_subdirectory)
2163
def configs(ctx):
2264
print("")

0 commit comments

Comments
 (0)