Skip to content

Commit

Permalink
Reduce false positives on copyright check to make it less "smart" (#74)
Browse files Browse the repository at this point in the history
Don't use git to determine when file was created. Instead, allow
anything that matches a <month> <year> signature.
  • Loading branch information
kiransingh99 authored Jun 3, 2023
1 parent b4d6357 commit b1e6293
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 50 deletions.
34 changes: 18 additions & 16 deletions tools/cmn.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
import sys


MONTHS = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]


class ReturnCodes(enum.IntEnum):
"""Subclass for all return code enums."""

Expand Down Expand Up @@ -179,22 +195,8 @@ def month_name_from_num(index: int) -> str:
:param index: number between 1 and 12.
:return: month name corresponding to input.
"""
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]

return months[index - 1]

return MONTHS[index - 1]


def which_python() -> str:
Expand Down
49 changes: 15 additions & 34 deletions tools/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import argparse
import os
import re
import subprocess
import sys

import cmn
Expand Down Expand Up @@ -67,39 +66,21 @@ def _generate_expected(file_path: str) -> Generator[str, None, None]:
notice_start_end = r"^# [-]{78}$"
blank_line = r"^#$"

edit_dates = (
subprocess.run(
["git", "log", "--format=%ci", "./" + file_path],
capture_output=True,
check=True,
)
.stdout.decode("utf-8")
.strip()
.split("\n")
)

if edit_dates[0]:
created_month = cmn.month_name_from_num(int(edit_dates[-1][5:7]))
created_year = edit_dates[-1][:4]
last_edited_year = edit_dates[0][:4]
if created_year == last_edited_year:
date_range = created_year
else:
date_range = created_year + " - " + last_edited_year

exp = [
notice_start_end,
rf"^# {file_name} - .+$",
blank_line,
rf"^# {created_month} {created_year}, [A-Za-z -]+$",
blank_line,
rf"^# Copyright \(c\) {date_range}$",
r"^# All rights reserved.$",
notice_start_end,
]

for line in exp:
yield line
months = f"({'|'.join(cmn.MONTHS)})"

exp = [
notice_start_end,
rf"^# {file_name} - .+$",
blank_line,
rf"^# {months} 20[\d]{{2}}, [A-Za-z -]+$",
blank_line,
r"^# Copyright \(c\) 20[0-9]{2}( \- 20\d{2})?$",
r"^# All rights reserved.$",
notice_start_end,
]

for line in exp:
yield line


def _run_check(args: argparse.Namespace) -> int:
Expand Down

0 comments on commit b1e6293

Please sign in to comment.