Skip to content

Commit

Permalink
Convert license check to Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
blag committed Dec 13, 2019
1 parent 653c4f5 commit 8205193
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions tasks/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down

0 comments on commit 8205193

Please sign in to comment.