Skip to content

Commit 8205193

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

2 files changed

Lines changed: 44 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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,47 @@ 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+
license_file = os.path.join(top_level_git_dir, 'LICENSE')
32+
with ctx.cd(top_level_git_dir):
33+
files = ctx.run("git diff --relative --diff-filter=ACMRTUXB "
34+
"--name-only {}".format(base_branch)).stdout.splitlines()
35+
if os.environ.get('FORCE_CHECK_ALL_FILES', False) == 'true' or files:
36+
# Check for the existence of a LICENSE file
37+
if not os.path.exist(license_file):
38+
raise Exception("Missing LICENSE file in {root_dir}".format(root_dir=top_level_git_dir))
39+
40+
with open(license_file) as f:
41+
lines = f.read().splitlines()
42+
43+
found_apache_license = False
44+
found_license_version = False
45+
found_apache_url = False
46+
for line in lines:
47+
if apache_license_rgx.match(line):
48+
found_apache_license = True
49+
if license_version_rgx.match(line):
50+
found_license_version = True
51+
if apache_url_rgx.match(line):
52+
found_apache_url = True
53+
if found_apache_license and found_license_version and found_apache_url:
54+
break
55+
else:
56+
raise Exception("LICENSE file doesn't contain Apache 2.0 license text")
57+
else:
58+
print("No files have changed, skipping run...")
59+
60+
2061
@task(copy.copy_pack_to_subdirectory)
2162
def configs(ctx):
2263
print("")

0 commit comments

Comments
 (0)