Skip to content

Commit

Permalink
Add script and workflow for copyright checks in CI (rust-lang#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaco-aws authored and tedinski committed Apr 21, 2021
1 parent 93049ca commit 9027dde
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/copyright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR MIT
name: RMC CI
on: pull_request

jobs:
copyright-check:
runs-on: ubuntu-latest
steps:
- name: Checkout RMC
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Get paths for files added
id: git-diff
run: |
files=$(git diff --name-only --diff-filter=A ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | xargs)
echo "::set-output name=paths::$files"
- name: Execute copyright check
if: ${{ steps.git-diff.outputs.paths }}
run: ./scripts/ci/copyright_check.py ${{ steps.git-diff.outputs.paths }}
51 changes: 51 additions & 0 deletions scripts/ci/copyright_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR MIT
import re
import sys

def copyright_check(filename):
fo = open(filename)
lines = fo.readlines()

# The check is failed if the file is empty
if len(lines) == 0:
return False

# Scripts may include in their first line a character sequence starting with
# '#!' (also know as shebang) to indicate an interpreter for execution.
# The values for the minimum number of lines and the indices of copyright
# lines depend on whether the file has a shebang or not.
shb_re = re.compile('#!\S+')
has_shebang = shb_re.search(lines[0])
min_lines = 3 if has_shebang else 2

# The check is failed if the file does not contain enough lines
if len(lines) < min_lines:
return False

# Compile the regexes for copyright lines
fst_re = re.compile('(//|#) Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.')
snd_re = re.compile('(//|#) SPDX-License-Identifier: Apache-2.0 OR MIT')

fst_idx = min_lines - 2
snd_idx = min_lines - 1

# The copyright check succeeds if the regexes can be found
if fst_re.search(lines[fst_idx]) and snd_re.search(lines[snd_idx]):
return True
else:
return False

if __name__ == "__main__":
filenames = sys.argv[1:]
checks = [copyright_check(fname) for fname in filenames]

for i in range(len(filenames)):
print(f'Copyright check - {filenames[i]}: ', end='')
print('PASS') if checks[i] else print('FAIL')

if all(checks):
sys.exit(0)
else:
sys.exit(1)

0 comments on commit 9027dde

Please sign in to comment.