Constant used
[opentx.git] / tools / copyright.py
blob8c1853c63490819f36898f178f5b452ce880cc5d
1 #!/usr/bin/env python
3 from __future__ import print_function
5 import os
6 import sys
9 def writeheader(filename, header):
10 """
11 write a header to filename,
12 skip files where first line after optional shebang matches the skip regex
13 filename should be the name of the file to write to
14 header should be a list of strings
15 skip should be a regex
16 """
17 with open(filename, "r") as f:
18 inpt = f.readlines()
19 output = []
21 # comment out the next 3 lines if you don't wish to preserve shebangs
22 if len(inpt) > 0 and inpt[0].startswith("#!"):
23 output.append(inpt[0])
24 inpt = inpt[1:]
26 if inpt[0].strip().startswith("/*"):
27 for i, line in enumerate(inpt):
28 if line.strip().endswith("*/"):
29 inpt = inpt[i + 1:]
30 break
32 while not inpt[0].strip():
33 inpt = inpt[1:]
35 output.extend(header) # add the header
36 for line in inpt:
37 output.append(line)
38 try:
39 with open(filename, 'w') as f:
40 f.writelines(output)
41 print("added header to %s" % filename)
42 except IOError as err:
43 print("something went wrong trying to add header to %s: %s" % (filename, err))
46 def main(args=sys.argv):
47 with open(os.path.dirname(os.path.realpath(__file__)) + "/copyright-header.txt") as headerfile:
48 header = headerfile.readlines()
49 for filename in args[1:]:
50 writeheader(filename, header)
53 if __name__ == '__main__':
54 # call the main method
55 main()