forked from molmod/molmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateheaders.py
executable file
·116 lines (101 loc) · 3.34 KB
/
updateheaders.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# MolMod is a collection of molecular modelling tools for python.
# Copyright (C) 2007 - 2012 Toon Verstraelen <Toon.Verstraelen@UGent.be>, Center
# for Molecular Modeling (CMM), Ghent University, Ghent, Belgium; all rights
# reserved unless otherwise stated.
#
# This file is part of MolMod.
#
# MolMod is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# MolMod is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
#--
from glob import glob
import os, sys
def strip_header(lines, closing):
# search for the header closing line, e.g. '#--'
counter = 0
found = 0
for line in lines:
counter += 1
if line == closing+'\n':
found = 1
break
if found:
del lines[:counter]
# If the header closing is not found, no headers are removed
# add a header closing line
lines.insert(0, closing+'\n')
def fix_python(lines, header_lines):
# check if a shebang is present
do_shebang = lines[0].startswith('#!')
# remove the current header
strip_header(lines, '#--')
# add new header (insert must be in reverse order)
for hline in header_lines[::-1]:
lines.insert(0, ('# '+hline).strip() + '\n')
# add a source code encoding line
lines.insert(0, '# -*- coding: utf-8 -*-\n')
if do_shebang:
lines.insert(0, '#!/usr/bin/env python\n')
def fix_c(lines, header_lines):
# check for an exception line
for line in lines:
if 'no_update_headers' in line:
return
# remove the current header
strip_header(lines, '//--')
# add new header (insert must be in reverse order)
for hline in header_lines[::-1]:
lines.insert(0, ('// '+hline).strip() + '\n')
def fix_f77(lines, header_lines):
# check for an exception line
for line in lines:
if 'no_update_headers' in line:
return
# remove the current header
strip_header(lines, '!--')
# add new header (insert must be in reverse order)
for hline in header_lines[::-1]:
lines.insert(0, ('! '+hline).strip() + '\n')
def main(fns):
fixers = [
('.py', fix_python),
('.pxd', fix_python),
('.pyx', fix_python),
('.c', fix_c),
('.cpp', fix_c),
('.h', fix_c),
('.pyf', fix_f77),
]
f = open('HEADER')
header_lines = f.readlines()
f.close()
for fn in fns:
if not os.path.isfile(fn):
continue
for ext, fixer in fixers:
if fn.endswith(ext):
print 'Fixing ', fn
f = file(fn)
lines = f.readlines()
f.close()
fixer(lines, header_lines)
f = file(fn, 'w')
f.writelines(lines)
f.close()
break
if __name__ == '__main__':
args = sys.argv[1:]
main(args)