forked from ddsmt/ddSMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_license_headers.py
executable file
·89 lines (66 loc) · 2.51 KB
/
update_license_headers.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
#! /usr/bin/env python3
import argparse
import glob
import os
import textwrap
HEADER_TEMPLATE = """
ddSMT: A delta debugger for SMT benchmarks in SMT-Lib v2 format.
This file is part of ddSMT.
Copyright (C) 2013-2021 by the authors listed in AUTHORS file.
ddSMT 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.
ddSMT 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 ddSMT. If not, see <https://www.gnu.org/licenses/>.
"""
def parse_options():
"""Parse command line options into ``global ARGS``."""
global ARGS
ap = argparse.ArgumentParser(
usage='update_license_headers.py [<options>] <basedir>')
ap.add_argument('basedir', help='base directory of the ddSMT directory')
ARGS = ap.parse_args()
def prepare_header():
"""Return new header as list of strings."""
return [
s.strip() for s in textwrap.indent(HEADER_TEMPLATE, '# ',
lambda line: True).split('\n')
]
def list_files():
"""Iterable over all files that should get the copyright header."""
yield f'{ARGS.basedir}/bin/ddsmt'
yield f'{ARGS.basedir}/bin/ddsmt-profile'
yield from glob.iglob(f'{ARGS.basedir}/ddsmt/*.py')
def update_file(filename, new_header):
"""Read the filename and add or update the copyright header.
Keep an existing shebang in place
"""
stat = os.stat(filename)
cur = open(filename).read().split('\n')
start = 0
try:
start = next(x for x in enumerate(cur)
if x[1].strip() != '' and not x[1].startswith('#'))[0]
except StopIteration:
# No actual code was found, probably an __init__.py
pass
new = []
if cur[0].startswith('#!'):
new.append(cur[0])
new.extend(new_header)
new.extend(cur[start:])
open(filename, 'w').write('\n'.join(new))
os.utime(filename, ns=(stat.st_atime_ns, stat.st_mtime_ns))
if __name__ == '__main__':
parse_options()
header = prepare_header()
for filename in list_files():
if filename.endswith('__init__.py'):
continue
print(filename)
update_file(filename, header)