Mercurial > public > ostc4
comparison Documentations/setGPLHeader.py @ 29:3a98f9e7ca58
ADD script to set GPL headers
| author | jDG |
|---|---|
| date | Tue, 23 Jan 2018 13:36:25 +0100 |
| parents | |
| children | 6237372f76a4 |
comparison
equal
deleted
inserted
replaced
| 28:7eabbb5b30ac | 29:3a98f9e7ca58 |
|---|---|
| 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 + ':') | |
| 26 for file in glob.iglob( os.path.join(path, '*.[chs]') ): | |
| 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['author'] = 'Heinrichs Weikamp' | |
| 48 kw['date'] = '2018' | |
| 49 | |
| 50 # Try to gather info from existing header, if any: | |
| 51 for line in lines: | |
| 52 # Skip files that already have a GNU license: | |
| 53 if 'GNU General Public License' in line: | |
| 54 print('(done)\t' + file) | |
| 55 return | |
| 56 | |
| 57 get(line, 'brief', kw) | |
| 58 get(line, 'author', kw) | |
| 59 get(line, 'date', kw) | |
| 60 | |
| 61 # Replace kw in header | |
| 62 header = template | |
| 63 for k,v in kw.items(): | |
| 64 header = header.replace('$'+k, v) | |
| 65 | |
| 66 # Overwrite resulting file, normalizing EOL | |
| 67 with open(file, 'wt', encoding='utf-8') as f: | |
| 68 for line in header.splitlines(): | |
| 69 f.write(line.rstrip() + '\n') | |
| 70 for line in lines: | |
| 71 f.write(line.rstrip() + '\n') | |
| 72 print('+\t' + file) | |
| 73 | |
| 74 ############################################################################## | |
| 75 | |
| 76 def get(line, word, kw): | |
| 77 m = re.search('(@|\\\\)' + word + '\s*(.*)', line) | |
| 78 if m: | |
| 79 kw[word] = m.group(2) | |
| 80 | |
| 81 ############################################################################## | |
| 82 | |
| 83 walk(OSTC4) |
