29
|
1 #! /usr/bin/env python3
|
|
2
|
|
3 from __future__ import print_function
|
|
4 import os, sys, glob,re, codecs
|
|
5
|
|
6 ##############################################################################
|
|
7
|
|
8 OSTC4=os.getcwd()
|
|
9 if not os.path.exists( os.path.join(OSTC4, 'Discovery') ):
|
|
10 raise 'Wrong start directory...'
|
|
11
|
|
12 ##############################################################################
|
|
13
|
|
14 def walk(OSTC4):
|
|
15 for dir in ['BootLoader', 'Common', 'Discovery', 'FontPack', 'Small_CPU']:
|
|
16 # Make sure we have the top directory...
|
|
17 if not os.path.exists( os.path.join(OSTC4, dir) ):
|
|
18 raise 'Missing ' + dir
|
|
19
|
|
20 # Then walk in all its existing source sub-directories...
|
|
21 for sub in ['.', 'Inc', 'Src']:
|
|
22 path = os.path.join(OSTC4, dir, sub)
|
|
23 if not os.path.exists(path):
|
|
24 continue
|
|
25 print(path + ':')
|
35
|
26 for file in sorted( glob.iglob( os.path.join(path, '*.[chs]') ) ):
|
29
|
27 try:
|
|
28 work(file)
|
|
29 except Exception as e:
|
|
30 raise RuntimeError('Cannot process ' + file + ':\n\t' + str(e))
|
|
31
|
|
32 ##############################################################################
|
|
33
|
|
34 with open(os.path.join(OSTC4, 'Documentations', 'GPL_template.txt')) as f:
|
|
35 template = f.read()
|
|
36
|
|
37 def work(file):
|
|
38
|
|
39 # Unclear what is done if encoding is not yet UTF-8...
|
|
40 with open(file, 'rt', encoding='cp1252') as f:
|
|
41 lines = f.read().splitlines()
|
|
42
|
|
43 # Set defaults
|
|
44 kw = {}
|
|
45 kw['file'] = file.replace(OSTC4+'/', '')
|
|
46 kw['brief'] = ''
|
|
47 kw['date'] = '2018'
|
|
48
|
|
49 # Try to gather info from existing header, if any:
|
|
50 for line in lines:
|
|
51 # Skip files that already have a GNU license:
|
|
52 if 'GNU General Public License' in line:
|
|
53 print('(done)\t' + file)
|
|
54 return
|
|
55
|
|
56 get(line, 'brief', kw)
|
|
57 get(line, 'date', kw)
|
|
58
|
|
59 # Replace kw in header
|
|
60 header = template
|
|
61 for k,v in kw.items():
|
|
62 header = header.replace('$'+k, v)
|
|
63
|
|
64 # Overwrite resulting file, normalizing EOL
|
|
65 with open(file, 'wt', encoding='utf-8') as f:
|
|
66 for line in header.splitlines():
|
|
67 f.write(line.rstrip() + '\n')
|
|
68 for line in lines:
|
|
69 f.write(line.rstrip() + '\n')
|
|
70 print('+\t' + file)
|
|
71
|
|
72 ##############################################################################
|
|
73
|
|
74 def get(line, word, kw):
|
|
75 m = re.search('(@|\\\\)' + word + '\s*(.*)', line)
|
|
76 if m:
|
|
77 kw[word] = m.group(2)
|
|
78
|
|
79 ##############################################################################
|
|
80
|
|
81 walk(OSTC4)
|