Debian Codegraph @

bin/updateCopyright.py

raw
1 """
2 A little python script that changes/updates the copyright notices
3 in all python files in the distribution.
4
5 The copyright notices are identified by being comments opened
6 with #c at the start of a line before the first statement in a python
7 file. The current copyright text is embedded in this file.
8
9 This file should be started in the directory containing the sources
10 and will traverse the subdirectories.
11 """
12
13 #c Copyright 2008-2025, the GAVO project <[email protected]>
14 #c
15 #c This program is free software, covered by the GNU GPL. See the
16 #c COPYING file in the source distribution.
17
18
19 import fnmatch
20 import os
21 import sys
22 import tokenize
23
24
25 CUR_TEXT = \
26 """#c Copyright 2008-2025, the GAVO project <[email protected]>
27 #c
28 #c This program is free software, covered by the GNU GPL. See the
29 #c COPYING file in the source distribution.
30
31 """
32
33
34 class NoComment(Exception):
35 pass
36
37
38 def makeReadline(source):
39 """returns a function that, on each call, returns one more item from
40 source.
41 """
42 iterator = iter(source)
43
44 def readline():
45 try:
46 return next(iterator)
47 except StopIteration:
48 return ""
49
50 return readline
51
52
53 def findCopyrightComment(lineSource):
54 """returns the start end end indices of the comment block in
55 the readline-like thing lineSource.
56
57 Trailing newlines are included.
58
59 This raises a NoComment exception if the copyright comment is not
60 found.
61 """
62 tokens = tokenize.generate_tokens(lineSource)
63 # find the first #c comment or give up if there's an instruction before
64 for tok in tokens:
65 if tok[0] not in set([tokenize.STRING, tokenize.NEWLINE,
66 tokenize.COMMENT, tokenize.NL]):
67 raise NoComment("Instruction found")
68 if tok[0]==tokenize.COMMENT and tok[1].startswith("#c"):
69 break
70 else:
71 raise NoComment("EOF encountered")
72
73 startLine = tok[2][0]-1
74 # no need to check ranges as there's an ENDMARKER token at the end
75 while (tok[0]==tokenize.NL or tok[0]==tokenize.NEWLINE
76 or (
77 tok[0]==tokenize.COMMENT and tok[1].startswith("#c"))):
78 endLine = tok[3][0]-1
79 tok = next(tokens)
80 return startLine, endLine
81
82
83 def processOne(fName):
84 """fixes the copyright comment in fName if it's there.
85
86 The function raises a NoComment exception if fName has no copyright
87 comment.
88 """
89 with open(fName) as f:
90 source = f.readlines()
91
92 startLine, endLine = findCopyrightComment(makeReadline(source))
93
94 # work on lines rather than tokens to preserve whitespace choices
95 source[startLine:endLine] = [CUR_TEXT]
96 with open(fName, "w") as f:
97 f.write("".join(source))
98
99
100 def main():
101 for dirpath, dirnames, filenames in os.walk("."):
102 for filename in filenames:
103 if fnmatch.fnmatch(filename, "*.py"):
104 try:
105 processOne(os.path.join(dirpath, filename))
106 except NoComment:
107 sys.stdout.write("Skipping %s\n"%os.path.join(dirpath, filename))
108
109
110 if __name__=="__main__":
111 main()