Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ repos:
hooks:
- id: rst-backticks
- id: rst-inline-touching-normal

- repo: local
hooks:
- id: validate-created
name: "Lint Created: dates"
language: system
entry: python3 scripts/validate-created.py
files: ^pep-\d+\.(rst|txt)$
types: [text]
52 changes: 52 additions & 0 deletions scripts/validate-created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Validates the 'Created: ' field of PEPs match %d-%b-%Y as specified in PEP 1.

Requires Python 3.6+

Usage: python3 validate-created.py filename1 [filename2 [...]]
"""
import sys
from datetime import datetime as dt

COL_NUMBER = len("Created: ")


def validate_file(filename: str) -> int:
errors = 0
found = False
line_number = 0

with open(filename, encoding="utf8") as f:
for line in f:
line_number += 1
if line.startswith("Created: "):
found = True
break

if not found:
errors += 1
print(f"{filename}: 'Created:' not found")
return errors

date = line.split()[1]
try:
# Validate
dt.strptime(date, "%d-%b-%Y")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "%d-%b-%Y" format depends on the current locale; it might be different across machines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, your suggestion at #1805 (comment) for something like %Y-%m-%d (2021-02-08) would help with this.

except ValueError as e:
print(f"{filename}:{line_number}:{COL_NUMBER}: {e}")
errors += 1

return errors


def main(argv: list) -> None:
assert len(argv) >= 2, "Missing required filename parameter(s)"
total_errors = 0
for filename in argv[1:]:
total_errors += validate_file(filename)

sys.exit(total_errors)


if __name__ == "__main__":
main(sys.argv)