forked from HypothesisWorks/hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforce_header.py
48 lines (40 loc) · 1.12 KB
/
enforce_header.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
import subprocess
import os
HEADER_FILE = "scripts/header.py"
HEADER_SOURCE = open(HEADER_FILE).read().strip()
def all_python_files():
lines = subprocess.check_output([
"git", "ls-tree", "--full-tree", "-r", "HEAD",
]).decode('utf-8').split("\n")
files = [
l.split()[-1]
for l in lines
if l
]
return [
f for f in files
if f[-3:] == ".py"
]
def main():
rootdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
print("cd %r" % (rootdir,))
os.chdir(rootdir)
files = all_python_files()
files.remove("scripts/enforce_header.py")
for f in files:
print(f)
lines = []
with open(f, encoding="utf-8") as o:
for l in o.readlines():
if 'END HEADER' in l:
lines = []
else:
lines.append(l)
source = ''.join(lines).strip()
with open(f, "w", encoding="utf-8") as o:
o.write(HEADER_SOURCE)
o.write("\n\n")
o.write(source)
o.write("\n")
if __name__ == '__main__':
main()