-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertCustomHeaderNotice.py
More file actions
95 lines (83 loc) · 3.26 KB
/
InsertCustomHeaderNotice.py
File metadata and controls
95 lines (83 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
#
# This script inserts custom file headers in to every source file in a directory
# If there's an existing file header, it is removed, i.e. replaced
# © Lorenz Bucher, 2020. All rights reserved
# https://github.com/Sidelobe/HeaderNotice
from glob import glob
import sys
import os
import re
custom_header = r"""
// __ ___ ___ _ __
// / |/ /_ __ / _ \_______ (_)__ ____/ /_
// / /|_/ / // / / ___/ __/ _ \ / / -_) __/ __/
// /_/ /_/\_, / /_/ /_/ \___/_/ /\__/\__/\__/
// /___/ |___/
//
// © 2020 - all rights reserved
"""
def strip_lines_and_add_header(file, num_lines, header):
lines = None
with open(file, "r") as input:
lines = input.readlines()
with open(file, "w") as output:
output.writelines(header)
output.writelines("\n")
output.writelines(lines[num_lines:])
def main():
##########
header = custom_header
header = header.strip("\n\r") # remove first newline
##########
path = sys.argv[1]
if os.path.exists(sys.argv[1]):
path = os.path.abspath(sys.argv[1])
file_list = []
for root, dirs, files in os.walk(path):
for filename in files:
if filename.endswith(('.hpp', '.cpp', '.h', '.c')):
file_list.append(os.path.join(root, filename))
# Matches C++ Style // (multiline) comments, including leading whitespace
pattern_cpp = re.compile(r'(\s*\/\/.*)')
# Matches C Style /* */ and doxygen /** */ (multiline) comments
pattern_c = re.compile(r'(\s?\/\*(?:[^*]|[\r\n]|(?:\*+(?:[^*\/]|[\r\n])))*\*+\/)|(\s?\/\/.*)')
for file_name in file_list:
print "\n========================\nScanning file %s " % file_name
code = None
with open(file_name) as f:
code = f.read()
m = pattern_c.match(code);
if m and m.group(1):
headerless_code = re.sub(pattern_c, r'', code, 1) # replace first occurence
print "Found C style header"
new_code = header + headerless_code
with open(file_name, "w") as output:
output.write(new_code)
continue
else:
lines = None
with open(file_name) as f:
lines = f.readlines()
existing_header_begin = 0
existing_header_end = 0
inside_cpp_comment = False
for index, line in enumerate(lines):
if pattern_cpp.match(line):
if inside_cpp_comment:
#print "inside C++ comment"
continue
else:
inside_cpp_comment = True
existing_header_begin = index
#print "found beginning of C++ comment"
continue
else:
existing_header_end = index
print "Found C++ style header: ", existing_header_end - existing_header_begin, " lines \n",
break
strip_lines = existing_header_end - existing_header_begin
strip_lines_and_add_header(file_name, strip_lines, header)
if __name__ == "__main__":
# execute only if run as a script
main()