-
Notifications
You must be signed in to change notification settings - Fork 46
/
update_license.py
executable file
·130 lines (105 loc) · 3.79 KB
/
update_license.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -------------------------------------------------------------------------------
# Support script for license header management.
# -------------------------------------------------------------------------------
import argparse
import logging
import glob
import os
import sys
import re
import filecmp
import shutil
begDelim = "######bl$"
endDelim = "######el$"
maxHeaderLines = 200
def cacheLicenseFile(infile, comment="#"):
if not os.path.isfile(infile):
logging.error("Unable to access license file - >%s" % infile)
sys.exit(1)
license = ""
with open(infile, "r") as file_in:
for line in file_in:
license += comment
if line.strip() != "":
license += " "
license += line
return license
parser = argparse.ArgumentParser()
parser.add_argument("--license", required=True, help="License File")
parser.add_argument("--source", required=True, help="Source directory")
parser.add_argument("--dryrun", help="enable dryrun mode", action="store_true")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--extension", help="file extension to parse")
group.add_argument("--files", help="specific file(s) to parse")
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
args = parser.parse_args()
srcDir = args.source
fileExtension = None
specificFiles = None
if args.extension:
fileExtension = args.extension
if args.files:
specificFiles = args.files.split(",")
print("")
logging.info("Source directory = %s" % srcDir)
if fileExtension:
logging.info("File extension = %s" % fileExtension)
if specificFiles:
logging.info("Specific files = %s" % specificFiles)
# cache license file
license = cacheLicenseFile(args.license)
# Scan files in provided source directory...
for filename in glob.iglob(srcDir + "/**", recursive=True):
# skip directories
if os.path.isdir(filename):
continue
# File matching options:
# (1) filter non-matching extensions
if fileExtension:
if not filename.endswith(fileExtension):
continue
# or, (2) filter for specific filename
if specificFiles:
found = False
for file in specificFiles:
fullPath = os.path.join(srcDir, file)
if fullPath == filename:
found = True
break
if not found:
continue
logging.debug("Examining %s for license..." % filename)
# Update license header contents if delimiters are found
with open(filename, "r") as file_in:
baseName = os.path.basename(filename)
dirName = os.path.dirname(filename)
tmpFile = dirName + "/." + baseName + ".tmp"
file_out = open(tmpFile, "w")
for line in file_in:
if re.search(begDelim, line):
logging.debug("Found beginning delimiter")
file_out.write(line)
file_out.write(license)
foundEnd = False
for i in range(maxHeaderLines):
line = file_in.readline()
if re.search(endDelim, line):
logging.debug("Found ending delimiter")
file_out.write(line)
foundEnd = True
break
if not foundEnd:
logging.error("Unable to find end of delimited header")
sys.exit(1)
else:
file_out.write(line)
file_out.close()
# Check if file changed and update
if not filecmp.cmp(filename, tmpFile, shallow=False):
logging.info("%s changed" % filename)
shutil.copystat(filename, tmpFile)
if not args.dryrun:
os.rename(tmpFile, filename)
else:
os.unlink(tmpFile)