-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_headers.py
More file actions
67 lines (56 loc) · 2.11 KB
/
update_headers.py
File metadata and controls
67 lines (56 loc) · 2.11 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Python Script
# Licensed under CC BY 4.0
# A Set of Tutorials for the LAMMPS Simulation Package (LiveCoMS, 2025)
# By Simon Gravelle, Cecilia M. S. Alvares, Jacob R. Gissinger, and Axel Kohlmeyer
# Please cite doi.org/10.33011/livecoms.6.1.3037
# Find more on GitHub: https://github.com/lammpstutorials
import os
FILE_TYPES = {
".lmp": "LAMMPS Input File",
".py": "Python Script",
".inc": "LAMMPS Include File",
".mol": "LAMMPS Molecule File",
".rxnmap": "LAMMPS Reaction Map File",
}
HEADER_BODY = [
"# Licensed under CC BY 4.0\n",
"# A Set of Tutorials for the LAMMPS Simulation Package [Article v1.0] (LiveCoMS, 2025)\n",
"# By Simon Gravelle, Cecilia M. S. Alvares, Jacob R. Gissinger, and Axel Kohlmeyer\n",
"# Please cite doi.org/10.33011/livecoms.6.1.3037\n",
"# Find more on GitHub: https://github.com/lammpstutorials\n",
]
def build_headers():
headers = {}
for ext, title in FILE_TYPES.items():
headers[ext] = [f"# {title}\n"] + HEADER_BODY
return headers
HEADERS = build_headers()
def find_files(root, extensions):
for dirpath, _, filenames in os.walk(root):
for filename in filenames:
for ext in extensions:
if filename.endswith(ext):
yield os.path.join(dirpath, filename), ext
def process_file(filepath, ext):
desired_header = HEADERS[ext]
with open(filepath, 'r') as f:
lines = f.readlines()
# Detect existing header as a line with "#" at the top
header_end = 0
for line in lines:
if line.startswith("#"):
header_end += 1
else:
break
current_header = lines[:header_end]
# If already correct, do nothing
if current_header == desired_header:
return
# Otherwise, replace the detected header
with open(filepath, 'w') as f:
f.writelines(desired_header + lines[header_end:])
print(f"[FIXED HEADER] {filepath}")
if __name__ == "__main__":
root_dir = os.getcwd() # All file within this folder
for filepath, ext in find_files(root_dir, HEADERS.keys()):
process_file(filepath, ext)