Skip to content

Commit ec32e35

Browse files
committed
add files.
1 parent a5a0ccf commit ec32e35

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Update Copyright Year
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 1 1 *"
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
update-year-pr:
14+
runs-on: ubuntu-22.04
15+
steps:
16+
- name: Checkout repo
17+
uses: actions/checkout@v5
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: "3.11"
23+
24+
- name: Update copyright year
25+
run: python utils/update_copyright_year.py
26+
27+
- name: Check for changes
28+
id: check_changes
29+
run: |
30+
if git diff --quiet; then
31+
echo "has_changes=false" >> $GITHUB_OUTPUT
32+
else
33+
echo "has_changes=true" >> $GITHUB_OUTPUT
34+
fi
35+
36+
- name: Create PR
37+
if: steps.check_changes.outputs.has_changes == 'true'
38+
run: |
39+
YEAR=$(date +'%Y')
40+
BRANCH="update-copyright-$YEAR"
41+
42+
git config user.name "github-actions[bot]"
43+
git config user.email "github-actions[bot]@users.noreply.github.com"
44+
45+
git checkout -b "$BRANCH"
46+
git add .
47+
git commit -m "chore: update copyright year to $YEAR"
48+
git push --set-upstream origin "$BRANCH"
49+
50+
gh pr create \
51+
--title "chore: update copyright year to $YEAR" \
52+
--body "Automated update of copyright year to $YEAR." \
53+
--head "$BRANCH" \
54+
--base main \
55+
env:
56+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

utils/update_copyright_year.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Update copyright year in source files to the current year.
4+
"""
5+
6+
import re
7+
import sys
8+
from datetime import datetime
9+
from pathlib import Path
10+
11+
12+
# Set current year
13+
YEAR = str(datetime.now().year)
14+
15+
EXTENSIONS = {".py", ".md"}
16+
17+
# Directories to exclude
18+
EXCLUDE_DIRS = {
19+
".venv",
20+
"venv",
21+
"node_modules",
22+
".git",
23+
"__pycache__",
24+
".tox",
25+
"dist",
26+
"build",
27+
".egg-info",
28+
}
29+
30+
# Regex patterns to match copyright lines
31+
COPYRIGHT_PATTERNS = [
32+
re.compile(r"(# Copyright )(\d{4})( The HuggingFace Team\.)"),
33+
re.compile(r"(# Copyright \(c\) )(\d{4})( The HuggingFace Team\.)"),
34+
re.compile(r"(Copyright )(\d{4})( The HuggingFace Team\.)"),
35+
re.compile(r"(Copyright \(c\) )(\d{4})( The HuggingFace Team\.)"),
36+
]
37+
38+
39+
def should_exclude(path: Path) -> bool:
40+
"""Check if path should be excluded."""
41+
return any(excluded in path.parts for excluded in EXCLUDE_DIRS)
42+
43+
44+
def update_file(file_path: Path) -> int:
45+
"""Update copyright year in a single file. Returns number of updates."""
46+
try:
47+
content = file_path.read_text(encoding="utf-8")
48+
except Exception as e:
49+
print(f"Warning: Could not read {file_path}: {e}", file=sys.stderr)
50+
return 0
51+
52+
new_content = content
53+
total_count = 0
54+
55+
for pattern in COPYRIGHT_PATTERNS:
56+
new_content, count = pattern.subn(rf"\g<1>{YEAR}\g<3>", new_content)
57+
total_count += count
58+
59+
if total_count > 0:
60+
try:
61+
file_path.write_text(new_content, encoding="utf-8")
62+
print(f"✓ Updated {total_count} line(s) in {file_path}")
63+
except Exception as e:
64+
print(f"Error: Could not write {file_path}: {e}", file=sys.stderr)
65+
return 0
66+
67+
return total_count
68+
69+
70+
def main():
71+
repo_root = Path(".").resolve()
72+
73+
print(f"Updating copyright to {YEAR}...")
74+
75+
total_files = 0
76+
total_updates = 0
77+
78+
for file_path in repo_root.rglob("*"):
79+
if not file_path.is_file() or should_exclude(file_path):
80+
continue
81+
82+
if file_path.suffix in EXTENSIONS:
83+
updates = update_file(file_path)
84+
if updates > 0:
85+
total_files += 1
86+
total_updates += updates
87+
88+
print(f"\nSummary: Updated {total_updates} line(s) in {total_files} file(s)")
89+
90+
# Exit with 0 if updates were made, 1 if no updates needed
91+
return 0 if total_updates > 0 else 1
92+
93+
94+
if __name__ == "__main__":
95+
sys.exit(main())

0 commit comments

Comments
 (0)