@@ 21,30 21,32 @@ my_name = 'twadeb'
my_version = '0.0.0'
# vars
-dpkglog = '/var/log/dpkg.log.2.gz'
+dpkglog = '/tmp/dpkg.twadeb'
alldpkg = '/var/log/dpkg.*'
listFiles = []
-fout = open('/tmp/dpkg.twadeb', 'a')
#
-def dpkgFiles(alldpkg):
-# os.remove('/tmp/dpkg.twadeb')
- listFiles = glob.glob(alldpkg)
- sortFiles = sorted(listFiles)
- for l in sortFiles:
- # reverse files to well format files concatenation
- if l.endswith(".gz"):
- with gzip.open(l, 'rt') as s:
- for li in reversed(list(s)):
- fout.write(li)
- else:
- with open(l, 'r') as s:
- for li in reversed(list(s)):
- fout.write(li)
-
-
-dpkgFiles(alldpkg)
+def dpkgFiles(sumOfDpkgFiles, targetFile):
+ try:
+ os.remove(targetFile)
+ except:
+ print("No", targetFile)
+ finally:
+ fout = open(targetFile, 'a')
+ # list all dpkg.log files (usually 1 per month)
+ listFiles = sorted(glob.glob(sumOfDpkgFiles))
+ # concatenate all dpkg.log files
+ for l in listFiles:
+ # reverse files content to well format files concatenation
+ if l.endswith(".gz"):
+ with gzip.open(l, 'rt') as s:
+ for li in reversed(list(s)):
+ fout.write(li)
+ else:
+ with open(l, 'r') as s:
+ for li in reversed(list(s)):
+ fout.write(li)
def parseLine(line):
@@ 57,7 59,7 @@ def parseLine(line):
def parseFile(nameOfFile):
- with gzip.open(nameOfFile, 'rt') as f:
+ with open(nameOfFile, 'r') as f:
for line in f:
res = parseLine(line)
if res:
@@ 66,6 68,12 @@ def parseFile(nameOfFile):
yield res
-# Pour test
-for infos in parseFile(dpkglog):
- print(infos)
+def main():
+ # Pour test
+ dpkgFiles(alldpkg, dpkglog)
+ for infos in parseFile(dpkglog):
+ print(infos)
+
+
+if __name__ == '__main__':
+ main()