forked from TeXworks/texworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateCopyrights.py
executable file
·113 lines (90 loc) · 3.77 KB
/
updateCopyrights.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
#!/usr/bin/env python2
#
# System requirements:
# - GitPython: https://pypi.python.org/pypi/GitPython/
#
from __future__ import print_function
from git import Repo
import datetime, os, re
# Global variable for the git repository
gitrepo = None
class CopyrightedFile:
"""A thin wrapper for a real file on the filesystem. This class assists
in updating the file's embedded copyright information.
Attributes
----------
RE_PART_OF_TEXWORKS: string
A regular expression to match the embedded copyright
line in files.
REPLACE_EXTENSIONS: list[string]
Files ending in any of these extensions will be
considered when updating copyrighted files.
"""
RE_PART_OF_TEXWORKS = "(This is part of TeXworks, an environment for working with TeX documents\s*\n\s*Copyright \(C\)) [-0-9]+ ([^\n]+)"
REPLACE_EXTENSIONS = ['.cpp', '.h']
def __init__(self, filename):
"""Construct from filename.
Parameters
----------
filename: str
Path to a file. This file's copyright information will be
examined and possibly updated by using the helper methods
below.
"""
self.filename = filename
@property
def year_range_str(self):
"""Returns the year (range) in which this file was modified"""
global gitrepo
timestamp_str = gitrepo.log('--diff-filter=A', '--follow', '--format="%at"', '--', self.filename)
timestamp = float(timestamp_str.replace('"', ''))
begin = datetime.datetime.fromtimestamp(timestamp).year
timestamp_str = gitrepo.log('-1', '--format="%at"', '--', self.filename)
timestamp = float(timestamp_str.replace('"', ''))
end = datetime.datetime.fromtimestamp(timestamp).year
if begin == end:
return str(end)
else:
return "{0}-{1}".format(begin, end)
def one_of_replace_extensions(self):
"""Returns True if the file extension is allowed to be updated, False otherwise."""
for ext in self.REPLACE_EXTENSIONS:
if self.filename.endswith(ext):
return True
return False
def needs_update(self):
"""Returns True if the file needs updating, False otherwise."""
if not self.one_of_replace_extensions():
return False
self.content = open(self.filename).read()
self.matches = re.search(self.RE_PART_OF_TEXWORKS, self.content)
return self.matches
def update_copyright(self):
"""Updates the copyright line in the file."""
# Replace the year(s)
orig = self.matches.group(0)
subst = "{0} {1} {2}".format(self.matches.group(1), self.year_range_str, self.matches.group(2))
self.content = self.content.replace(orig, subst)
# Write the contents to disk
f = open(self.filename, 'w')
f.write(self.content)
f.close()
print("Updated {0}".format(self.filename))
def manual_update_notice():
"""Reminder for places where the copyright information must be updated manually"""
print("")
print("Don't forget to manually update the copyright information in the following files:")
for f in ["README.md", "TeXworks.plist.in", "man/texworks.1", "CMake/Modules/COPYING-CMAKE-MODULES", "res/TeXworks.rc", "src/main.cpp", "src/TWApp.cpp"]:
print(" {0}".format(f))
def main():
"""Main"""
global gitrepo
gitrepo = Repo(".").git
for root, dirs, files in os.walk('.'):
for f in files:
the_file = CopyrightedFile(os.path.join(root, f))
if the_file.needs_update():
the_file.update_copyright()
manual_update_notice()
if __name__ == '__main__':
main()