forked from theochem/Selector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateheaders.py
134 lines (115 loc) · 3.96 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
# The DiverseSelector library provides a set of tools to select molecule
# subset with maximum molecular diversity.
#
# Copyright (C) 2022 The QC-Devs Community
#
# This file is part of DiverseSelector.
#
# DiverseSelector 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.
#
# DiverseSelector 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
from fnmatch import fnmatch
import os
def strip_header(lines, closing):
# search for the header closing line, i.e. '# --\n'
counter = 0
found = False
for line in lines:
counter += 1
if line == closing:
found = True
break
if found:
del lines[:counter]
# If the header closing is not found, we assume it is not present.
# add a header closing line
lines.insert(0, closing)
def fix_python(fn, lines, header_lines):
# check if a shebang is present
do_shebang = lines[0].startswith('#!')
# remove the current header
strip_header(lines, '# --\n')
# add a pylint line for test files:
# if os.path.basename(fn).startswith('test_'):
# if not lines[1].startswith('#pylint: skip-file'):
# lines.insert(1, '#pylint: skip-file\n')
# add new header (insert 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(fn, 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, '//--\n')
# add new header (insert must be in reverse order)
for hline in header_lines[::-1]:
lines.insert(0, ('// ' + hline).strip() + '\n')
def fix_rst(fn, 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, ' : --\n')
# add an empty line after header if needed
if len(lines[1].strip()) > 0:
lines.insert(1, '\n')
# add new header (insert must be in reverse order)
for hline in header_lines[::-1]:
lines.insert(0, (' : ' + hline).rstrip() + '\n')
# add comment instruction
lines.insert(0, '..\n')
def iter_subdirs(root):
for dn, _, _ in os.walk(root):
yield dn
def main():
source_dirs = ['.', 'doc', 'scripts', 'tools'] + \
list(iter_subdirs('procrustes'))
fixers = [
('*.py', fix_python),
('*.pxd', fix_python),
('*.pyx', fix_python),
('*.txt', fix_python),
('*.c', fix_c),
('*.cpp', fix_c),
('*.h', fix_c),
('*.rst', fix_rst),
]
f = open('HEADER')
header_lines = f.readlines()
f.close()
for sdir in source_dirs:
print('Scanning:', sdir)
for fn in glob(sdir + '/*.*'):
if not os.path.isfile(fn):
continue
for pattern, fixer in fixers:
if fnmatch(fn, pattern):
print(' Fixing:', fn)
with open(fn) as f:
lines = f.readlines()
fixer(fn, lines, header_lines)
with open(fn, 'w') as f:
f.writelines(lines)
break
if __name__ == '__main__':
main()