-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright.py
41 lines (32 loc) · 1.24 KB
/
copyright.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright 2024 Cisco Systems, Inc. and its affiliates
import re
from datetime import datetime
from pathlib import Path
COPYRIGHT_NOTICE = f"# Copyright {datetime.now().year} Cisco Systems, Inc. and its affiliates"
COPYRIGHT_PATTERN = r"^#.*copyright.*cisco.*$"
SUBDIRS = ["catalystwan", "examples"]
def check_if_copyright_present(file_path):
try:
with file_path.open("r", encoding="utf-8") as file:
first_line = file.readline().strip()
if re.search(COPYRIGHT_PATTERN, first_line, re.IGNORECASE):
return True
except Exception as e:
print(f"Error reading {file_path}: {e}")
return False
def scan_and_update(dir_path):
directory = Path(dir_path)
if not directory.is_dir():
print(f"The provided path '{dir_path}' is not a valid directory.")
return
for file_path in directory.rglob("*.py"):
if file_path.stat().st_size == 0:
continue
if not check_if_copyright_present(file_path):
with open(file_path, "r+") as file:
content = file.read()
file.seek(0)
file.write(COPYRIGHT_NOTICE + "\n" + content)
if __name__ == "__main__":
for dir in SUBDIRS:
scan_and_update(dir)