Merge branch 'weblate-gnu-mailman-postorius' into 'master'
[mailman-postorious.git] / copybump.py
blob84f0c9278d7d5b6cf4ab22541364e2a9a648e811
1 #! /usr/bin/env python3
3 import datetime
4 import os
5 import re
6 import stat
7 import sys
10 FSF = 'by the Free Software Foundation, Inc.'
11 this_year = datetime.date.today().year
12 pyre_c = re.compile(r'# Copyright \(C\) ((?P<start>\d{4})-)?(?P<end>\d{4})')
13 pyre_n = re.compile(r'# Copyright ((?P<start>\d{4})-)?(?P<end>\d{4})')
14 new_c = '# Copyright (C) {}-{} {}'
15 new_n = '# Copyright {}-{} {}'
17 MODE = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
20 if '--noc' in sys.argv:
21 pyre = pyre_n
22 new = new_n
23 sys.argv.remove('--noc')
24 else:
25 pyre = pyre_c
26 new = new_c
29 def do_file(path, owner):
30 permissions = os.stat(path).st_mode & MODE
31 with open(path) as in_file, open(path + '.out', 'w') as out_file:
32 try:
33 for line in in_file:
34 mo_c = pyre_c.match(line)
35 mo_n = pyre_n.match(line)
36 if mo_c is None and mo_n is None:
37 out_file.write(line)
38 continue
39 mo = mo_n if mo_c is None else mo_c
40 start = (
41 mo.group('end')
42 if mo.group('start') is None
43 else mo.group('start')
45 if int(start) == this_year:
46 out_file.write(line)
47 continue
48 print(
49 new.format(start, this_year, owner), file=out_file
50 ) # noqa
51 print('=>', path)
52 for line in in_file:
53 out_file.write(line)
54 except UnicodeDecodeError:
55 print('Cannot convert path:', path)
56 os.remove(path + '.out')
57 return
58 os.rename(path + '.out', path)
59 os.chmod(path, permissions)
62 def remove(dirs, path):
63 try:
64 dirs.remove(path)
65 except ValueError:
66 pass
69 def do_walk():
70 try:
71 owner = sys.argv[1]
72 except IndexError:
73 owner = FSF
74 for root, dirs, files in os.walk('.'):
75 if root == '.':
76 remove(dirs, '.git')
77 remove(dirs, '.tox')
78 remove(dirs, 'bin')
79 remove(dirs, 'contrib')
80 remove(dirs, 'develop-eggs')
81 remove(dirs, 'eggs')
82 remove(dirs, 'parts')
83 remove(dirs, 'gnu-COPYING-GPL')
84 remove(dirs, '.installed.cfg')
85 remove(dirs, '.bzrignore')
86 remove(dirs, 'distribute_setup.py')
87 if root == './src':
88 remove(dirs, 'postorius.egg-info')
89 if root == './src/postorius':
90 remove(dirs, 'messages')
91 for file_name in files:
92 if os.path.splitext(file_name)[1] in ('.pyc', '.gz', '.egg'):
93 continue
94 path = os.path.join(root, file_name)
95 if os.path.isfile(path):
96 do_file(path, owner)
99 if __name__ == '__main__':
100 do_walk()