Skip to content

Commit 1ca13df

Browse files
committed
Add script to automatically rewrite file headers
1 parent 981122f commit 1ca13df

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

update_headers.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
This script automatically updates the header for *.py, *.pyx, and *.pxd
6+
files in RMG-Py/rmgpy, RMG-Py/scripts, and the root RMG-Py directory.
7+
8+
Other directories (e.g., examples, documentation, etc.) are ignored
9+
along with directories containing test data.
10+
11+
The headers are automatically generated based on the LICENSE.txt file.
12+
Typical usage would involve running this script after updating the copyright
13+
date in the license file.
14+
15+
Because this script makes assumptions regarding the contents at the
16+
start of each file, be sure to double-check the results to make sure
17+
important lines aren't accidentally overwritten.
18+
"""
19+
20+
import os
21+
22+
shebang = """#!/usr/bin/env python
23+
# -*- coding: utf-8 -*-
24+
25+
"""
26+
27+
header = """###############################################################################
28+
# #
29+
# RMG - Reaction Mechanism Generator #
30+
# #
31+
"""
32+
33+
with open('LICENSE.txt', 'r') as f:
34+
for line in f:
35+
line = line.strip()
36+
newline = '# {0:<75} #\n'.format(line)
37+
header += newline
38+
39+
header += """# #
40+
###############################################################################
41+
"""
42+
43+
print header
44+
45+
def replace_header(oldfile):
46+
newfile = os.path.join('tmp', 'tempfile')
47+
48+
ext = os.path.splitext(oldfile)[1]
49+
if ext == '.py':
50+
py_file = True
51+
elif ext == '.pyx' or ext == '.pxd':
52+
py_file = False
53+
else:
54+
raise Exception('Unexpected file type: {0}'.format(oldfile))
55+
56+
with open(oldfile, 'r') as old, open(newfile, 'w+') as new:
57+
58+
# Write shebang and encoding for python files only
59+
if py_file:
60+
new.write(shebang)
61+
62+
# Read old file and copy over contents
63+
found_bar = False
64+
first_line = True
65+
start = False
66+
for i, line in enumerate(old):
67+
if i == 0 and line[0] != '#':
68+
# Assume there's no header, so start copying lines right away
69+
new.write(header)
70+
start = True
71+
if start:
72+
if first_line and line.strip() != '':
73+
new.write('\n')
74+
first_line = False
75+
new.write(line)
76+
elif line.startswith('# cython:'):
77+
# Copy over any cython directives
78+
new.write(line)
79+
new.write('\n')
80+
elif line.startswith('##########'):
81+
if found_bar:
82+
# We've reached the end of the license, so start copying lines starting with the next line
83+
new.write(header)
84+
start = True
85+
else:
86+
found_bar = True
87+
88+
# Replace old file with new file
89+
os.rename(newfile, oldfile)
90+
91+
# Create temporary directory for writing files
92+
if not os.path.exists('tmp'):
93+
os.makedirs('tmp')
94+
95+
# Compile list of files to modify
96+
filelist = ['rmg.py', 'cantherm.py', 'setup.py']
97+
98+
root_dirs = ['rmgpy', 'scripts']
99+
for root_dir in root_dirs:
100+
for root, dirs, files in os.walk(root_dir):
101+
if 'test_data' in root or 'files' in root or '/tools/data' in root or '/cantherm/data' in root:
102+
continue
103+
print root
104+
for f in files:
105+
ext = os.path.splitext(f)[1]
106+
if ext in ['.py', '.pyx', '.pxd']:
107+
filelist.append(os.path.join(root, f))
108+
109+
for f in filelist:
110+
print 'Updating {0} ...'.format(f)
111+
replace_header(f)
112+

0 commit comments

Comments
 (0)