Skip to content

Commit fa994a7

Browse files
author
MarcoFalke
committed
contrib: Don't use shell=True
1 parent fafe7b3 commit fa994a7

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed
Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/usr/bin/env python
2-
'''
1+
#!/usr/bin/env python3
2+
"""
33
Run this script to update all the copyright headers of files
44
that were changed this year.
55
@@ -10,37 +10,58 @@
1010
it will change it to
1111
1212
// Copyright (c) 2009-2015 The Bitcoin Core developers
13-
'''
14-
import os
13+
"""
14+
import subprocess
1515
import time
1616
import re
1717

18-
year = time.gmtime()[0]
19-
CMD_GIT_DATE = 'git log --format=%%ad --date=short -1 %s | cut -d"-" -f 1'
20-
CMD_REGEX= "perl -pi -e 's/(20\d\d)(?:-20\d\d)? The Bitcoin/$1-%s The Bitcoin/' %s"
21-
REGEX_CURRENT= re.compile("%s The Bitcoin" % year)
22-
CMD_LIST_FILES= "find %s | grep %s"
18+
CMD_GIT_LIST_FILES = ['git', 'ls-files']
19+
CMD_GIT_DATE = ['git', 'log', '--format=%ad', '--date=short', '-1']
20+
CMD_PERL_REGEX = ['perl', '-pi', '-e']
21+
REGEX_TEMPLATE = 's/(20\\d\\d)(?:-20\\d\\d)? The Bitcoin/$1-%s The Bitcoin/'
2322

24-
FOLDERS = ["./qa", "./src"]
23+
FOLDERS = ["qa/", "src/"]
2524
EXTENSIONS = [".cpp",".h", ".py"]
2625

26+
2727
def get_git_date(file_path):
28-
r = os.popen(CMD_GIT_DATE % file_path)
29-
for l in r:
30-
# Result is one line, so just return
31-
return l.replace("\n","")
32-
return ""
33-
34-
n=1
35-
for folder in FOLDERS:
36-
for extension in EXTENSIONS:
37-
for file_path in os.popen(CMD_LIST_FILES % (folder, extension)):
38-
file_path = os.getcwd() + file_path[1:-1]
39-
if file_path.endswith(extension):
40-
git_date = get_git_date(file_path)
41-
if str(year) == git_date:
42-
# Only update if current year is not found
43-
if REGEX_CURRENT.search(open(file_path, "r").read()) is None:
44-
print n,"Last git edit", git_date, "-", file_path
45-
os.popen(CMD_REGEX % (year,file_path))
28+
d = subprocess.run(CMD_GIT_DATE + [file_path],
29+
stdout=subprocess.PIPE,
30+
check=True,
31+
universal_newlines=True).stdout
32+
# yyyy-mm-dd
33+
return d.split('-')[0]
34+
35+
36+
def skip_file(file_path):
37+
for ext in EXTENSIONS:
38+
if file_path.endswith(ext):
39+
return False
40+
else:
41+
return True
42+
43+
if __name__ == "__main__":
44+
year = str(time.gmtime()[0])
45+
regex_current = re.compile("%s The Bitcoin" % year)
46+
n = 1
47+
for folder in FOLDERS:
48+
for file_path in subprocess.run(
49+
CMD_GIT_LIST_FILES + [folder],
50+
stdout=subprocess.PIPE,
51+
check=True,
52+
universal_newlines=True
53+
).stdout.split("\n"):
54+
if skip_file(file_path):
55+
# print(file_path, "(skip)")
56+
continue
57+
git_date = get_git_date(file_path)
58+
if not year == git_date:
59+
# print(file_path, year, "(skip)")
60+
continue
61+
if regex_current.search(open(file_path, "r").read()) is not None:
62+
# already up to date
63+
# print(file_path, year, "(skip)")
64+
continue
65+
print(n, file_path, "(update to %s)" % year)
66+
subprocess.run(CMD_PERL_REGEX + [REGEX_TEMPLATE % year, file_path], check=True)
4667
n = n + 1

0 commit comments

Comments
 (0)