Skip to content

Commit

Permalink
Add copyright check
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Oct 27, 2022
1 parent bc3a082 commit a28890d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
97 changes: 96 additions & 1 deletion setup_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 - 2020 Avram Lubkin, All Rights Reserved
# Copyright 2017 - 2022 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,9 +9,11 @@
"""

import contextlib
import datetime
import io
import os
import re
import subprocess
import sys


Expand Down Expand Up @@ -106,6 +108,95 @@ def check_rst2html(path):
return 0


def check_copyrights():
"""
Check files recursively to ensure year of last change is in copyright line
"""

this_year = str(datetime.date.today().year)
changed_now = []

# Get list of changed files
process = subprocess.run(
('git', 'status', '--porcelain=1'), stdout=subprocess.PIPE, check=True, text=True
)
for entry in process.stdout.splitlines():
filename = entry[3:].strip()

# Get changes for file
process = subprocess.run(
('git', 'diff', '-U0', filename), stdout=subprocess.PIPE, check=True, text=True
)

# Find files with changes that aren't only for copyright
for line in process.stdout.splitlines():
if line[0] != '+' or line[:3] == '+++': # Ignore anything but the new contents
continue

if re.search(r'copyright.*20\d\d', line, re.IGNORECASE): # Ignore copyright line
continue

changed_now.append(filename)
break

# Look for copyright lines
process = subprocess.run(
('git', 'grep', '-i', 'copyright'), stdout=subprocess.PIPE, check=True, text=True
)

rtn = 0
for entry in process.stdout.splitlines():

modified = None

# Get the year in the copyright line
match = re.match(r'([^:]+):.*(20\d\d)', entry)
if match:
filename, year = match.groups()

# If files is in current changes, use this year
if filename in changed_now:
modified = this_year

# Otherwise, try to get the year of last commit that wasn't only updating copyright
else:
git_log = subprocess.run(
('git', '--no-pager', 'log', '-U0', filename),
stdout=subprocess.PIPE, check=True, text=True
)

for line in git_log.stdout.splitlines():

# Get year
if line.startswith('Date: '):
modified = line.split()[5]

# Skip blank line and lines that aren't changes
if not line.strip() or line[0] != '+' or line[:3] == '+++':
continue

# Stop looking on the first line we hit that isn't a copyright
if re.search(r'copyright.*20\d\d', line, re.IGNORECASE) is None:
break

# Special case for Sphinx configuration
if filename == 'doc/conf.py' and modified != this_year:

# Get the latest change date for docs
process = subprocess.run(
('git', 'log', '-1', '--pretty=format:%cs', 'doc/*.rst'),
stdout=subprocess.PIPE, check=True, text=True
)
modified = process.stdout[:4]

# Compare modified date to copyright year
if modified and modified != year:
rtn = 1
print('%s [%s]' % (entry, modified))

return rtn


if __name__ == '__main__':

# Do nothing if no arguments were given
Expand All @@ -130,6 +221,10 @@ def check_rst2html(path):
sys.exit('Missing filename for ReST to HTML check')
sys.exit(check_rst2html(sys.argv[2]))

# Check copyrights
if sys.argv[1] == 'copyright':
sys.exit(check_copyrights())

# Unknown option
else:
sys.stderr.write('Unknown option: %s' % sys.argv[1])
Expand Down
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ignore_basepython_conflict = tRUE
envlist =
lint
copyright
coverage
docs
PY3{9,8,7,6,5}
Expand Down Expand Up @@ -46,6 +47,13 @@ deps =
commands =
{envpython} -m specialist --output {toxinidir}/.specialist --targets prefixed/*.py -m unittest discover -s {toxinidir}/tests {posargs}

[testenv:copyright]
skip_install = True
ignore_errors = True

commands =
{envpython} setup_helpers.py copyright

[testenv:lint]
skip_install = True
ignore_errors=True
Expand Down

0 comments on commit a28890d

Please sign in to comment.