Allow setting Postfix transport_file_type: regex without setting something for postma...
[mailman.git] / copybump.py
blob30cf06b31006a6025eed18e4de4a81095a3d82fb
1 #! /usr/bin/env python3
3 import os
4 import re
5 import sys
6 import stat
7 import datetime
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 = (mo.group('end')
41 if mo.group('start') is None
42 else mo.group('start'))
43 if int(start) == this_year:
44 out_file.write(line)
45 continue
46 print(new.format(start, this_year, owner), file=out_file)
47 print('=>', path)
48 for line in in_file:
49 out_file.write(line)
50 except UnicodeDecodeError:
51 print('Cannot convert path:', path)
52 os.remove(path + '.out')
53 return
54 os.rename(path + '.out', path)
55 os.chmod(path, permissions)
58 def remove(dirs, path):
59 try:
60 dirs.remove(path)
61 except ValueError:
62 pass
65 def do_walk():
66 try:
67 owner = sys.argv[1]
68 except IndexError:
69 owner = FSF
70 for root, dirs, files in os.walk('.'):
71 if root == '.':
72 remove(dirs, '.git')
73 remove(dirs, '.tox')
74 remove(dirs, 'bin')
75 remove(dirs, 'contrib')
76 remove(dirs, 'develop-eggs')
77 remove(dirs, 'eggs')
78 remove(dirs, 'parts')
79 remove(dirs, 'gnu-COPYING-GPL')
80 remove(dirs, '.installed.cfg')
81 remove(dirs, '.bzrignore')
82 remove(dirs, 'distribute_setup.py')
83 if root == './src':
84 remove(dirs, 'mailman.egg-info')
85 if root == './src/mailman':
86 remove(dirs, 'messages')
87 for file_name in files:
88 if os.path.splitext(file_name)[1] in ('.pyc', '.gz', '.egg'):
89 continue
90 path = os.path.join(root, file_name)
91 if os.path.isfile(path):
92 do_file(path, owner)
95 if __name__ == '__main__':
96 do_walk()