/
splitDepViolationLog.py
executable file
·150 lines (114 loc) · 4.44 KB
/
splitDepViolationLog.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import sys
import time
class DepViolSplitter(object):
def __init__(self, outFileIn=None, verbIn=False):
self.outFile = sys.stdout
if outFileIn:
print("Summary file:", outFileIn)
self.outFile = open(outFileIn, "w")
self.verbose = verbIn
return
def __del__(self):
self.outFile.close()
return
def setVerbose(self, verbIn=False):
self.verbose = verbIn
return
# --------------------------------------------------------------------------------
def split(self, logFile):
self.outFile.write("going to check " + logFile + "\n")
pkgStartRe = re.compile(r"^>> Checking dependency for (.*)\s*$")
pkgEndRe = re.compile(r"^>> Done Checking dependency for (.*)\s*$")
depViolRe = re.compile(r"\s*\*+ERROR: Dependency violation")
logDirs = os.path.join(os.path.split(logFile)[0], "depViolationLogs")
print("logDirs ", logDirs)
if not os.path.exists(logDirs):
os.makedirs(logDirs)
lf = open(logFile, "r")
lines = lf
startTime = time.time()
nLines = 0
pkgViol = {}
actPkg = "None"
actTstLines = 0
actPkgLines = 0
actLogLines = []
startFound = False
for line in lines:
# write out log to individual log file ...
if startFound and ">> Done Checking dependency " not in line:
actLogLines.append(line)
nLines += 1
actTstLines += 1
actPkgLines += 1
pkgStartMatch = pkgStartRe.match(line)
if pkgStartMatch:
pkg = pkgStartMatch.group(1)
actPkg = pkg
actPkgLines = 0
startFound = True
pkgEndMatch = pkgEndRe.match(line)
if pkgEndMatch:
pkg = pkgEndMatch.group(1)
if actPkg != pkg:
self.outFile.write(
"pkgEndMatch> package mismatch: pkg found "
+ pkg
+ " actPkg="
+ actPkg
+ "\n"
)
if len(actLogLines) > 2:
pkgViol[pkg] = len(depViolRe.findall("".join(actLogLines)))
actLogDir = os.path.join(logDirs, pkg)
################################################
if not os.path.exists(actLogDir):
os.makedirs(actLogDir)
# os.makedirs(actLogDir)
###############################################
actLogFile = open(os.path.join(actLogDir, "depViolation.log"), "w")
actLogFile.write("".join(actLogLines))
actLogFile.close()
actLogLines = []
startFound = False
stopTime = time.time()
lf.close()
self.outFile.write("found a total of " + str(nLines) + " lines in logfile.\n")
self.outFile.write("analysis took " + str(stopTime - startTime) + " sec.\n")
self.outFile.write(
"total number of packages with violations: " + str(len(list(pkgViol.keys()))) + "\n"
)
import pprint
pprint.pprint(pkgViol)
try:
from pickle import Pickler
resFile = open("depViolationSummary.pkl", "wb")
pklr = Pickler(resFile, protocol=2)
pklr.dump(pkgViol)
resFile.close()
print("Successfully pickled results for dependency violations ! ")
except Exception as e:
print("ERROR during pickling results for dependency violations:", str(e))
return
# ================================================================================
def main():
try:
import argparse
except ImportError:
import archived_argparse as argparse
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--logFile", default=None, required=True)
parser.add_argument("-v", "--verbose", action="store_true", default=False)
parser.add_argument("-s", "--outFile", default=None)
args = parser.parse_args()
logFile = args.logFile
verb = args.verbose
outFile = args.outFile
tls = DepViolSplitter(outFileIn=outFile, verbIn=verb)
tls.split(logFile)
if __name__ == "__main__":
main()