Skip to content

Commit fa6ad85

Browse files
author
MarcoFalke
committed
[devtools] Rewrite fix-copyright-headers.py
1 parent 3cd836c commit fa6ad85

File tree

2 files changed

+35
-42
lines changed

2 files changed

+35
-42
lines changed

contrib/devtools/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ fix-copyright-headers.py
1111
========================
1212

1313
Every year newly updated files need to have its copyright headers updated to reflect the current year.
14-
If you run this script from src/ it will automatically update the year on the copyright header for all
15-
.cpp and .h files if these have a git commit from the current year.
14+
If you run this script from the root folder it will automatically update the year on the copyright header for all
15+
source files if these have a git commit from the current year.
1616

17-
For example a file changed in 2014 (with 2014 being the current year):
17+
For example a file changed in 2015 (with 2015 being the current year):
1818

1919
```// Copyright (c) 2009-2013 The Bitcoin Core developers```
2020

2121
would be changed to:
2222

23-
```// Copyright (c) 2009-2014 The Bitcoin Core developers```
23+
```// Copyright (c) 2009-2015 The Bitcoin Core developers```
2424

2525
git-subtree-check.sh
2626
====================
Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
11
#!/usr/bin/env python
22
'''
3-
Run this script inside of src/ and it will look for all the files
4-
that were changed this year that still have the last year in the
5-
copyright headers, and it will fix the headers on that file using
6-
a perl regex one liner.
3+
Run this script to update all the copyright headers of files
4+
that were changed this year.
75
8-
For example: if it finds something like this and we're in 2014
6+
For example:
97
10-
// Copyright (c) 2009-2013 The Bitcoin Core developers
8+
// Copyright (c) 2009-2012 The Bitcoin Core developers
119
1210
it will change it to
1311
14-
// Copyright (c) 2009-2014 The Bitcoin Core developers
15-
16-
It will do this for all the files in the folder and its children.
17-
18-
Author: @gubatron
12+
// Copyright (c) 2009-2015 The Bitcoin Core developers
1913
'''
2014
import os
2115
import time
16+
import re
2217

2318
year = time.gmtime()[0]
24-
last_year = year - 1
25-
command = "perl -pi -e 's/%s The Bitcoin/%s The Bitcoin/' %s"
26-
listFilesCommand = "find . | grep %s"
27-
28-
extensions = [".cpp",".h"]
29-
30-
def getLastGitModifiedDate(filePath):
31-
gitGetLastCommitDateCommand = "git log " + filePath +" | grep Date | head -n 1"
32-
p = os.popen(gitGetLastCommitDateCommand)
33-
result = ""
34-
for l in p:
35-
result = l
36-
break
37-
result = result.replace("\n","")
38-
return result
19+
CMD_GIT_DATE = "git log %s | grep Date | head -n 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"
3923

40-
n=1
41-
for extension in extensions:
42-
foundFiles = os.popen(listFilesCommand % extension)
43-
for filePath in foundFiles:
44-
filePath = filePath[1:-1]
45-
if filePath.endswith(extension):
46-
filePath = os.getcwd() + filePath
47-
modifiedTime = getLastGitModifiedDate(filePath)
48-
if len(modifiedTime) > 0 and str(year) in modifiedTime:
49-
print n,"Last Git Modified: ", modifiedTime, " - ", filePath
50-
os.popen(command % (last_year,year,filePath))
51-
n = n + 1
24+
FOLDERS = ["./qa", "./src"]
25+
EXTENSIONS = [".cpp",".h", ".py"]
5226

27+
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 ""
5333

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 len(git_date) > 0 and str(year) in 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))
46+
n = n + 1

0 commit comments

Comments
 (0)