Blame
Date:
Wed Mar 12 15:04:10 2014 UTC
Message:
add virtualenv
001
2014-02-05
alienus
#!/usr/bin/env python3
002
2014-02-05
alienus
#
003
2014-02-05
alienus
# twadeb.py
004
2014-02-05
alienus
#
005
2014-02-05
alienus
# Copyright 2014 Frédéric Galusik <alienus~at~riseup~dot~net>
006
2014-02-05
alienus
#
007
2014-02-05
alienus
#
008
2014-02-05
alienus
# License: BSD Revised
009
2014-02-05
alienus
#
010
2014-02-05
alienus
# Use the dpkg.log file to show last installed packages
011
2014-02-05
alienus
#
012
2014-02-05
alienus
#
013
2014-02-08
alienus
import os
014
2014-02-16
alienus
import sys
015
2014-02-05
alienus
import gzip
016
2014-02-05
alienus
import re
017
2014-02-08
alienus
import glob
018
2014-02-16
alienus
import argparse
019
2014-02-16
alienus
from time import gmtime, strftime
020
2014-02-16
alienus
import socket
021
2014-02-16
alienus
import subprocess
022
2014-02-05
alienus
023
2014-02-05
alienus
# Program information
024
2014-02-05
alienus
my_url = 'https://gitlab.com/alienus/twadeb'
025
2014-02-05
alienus
my_name = 'twadeb'
026
2014-02-05
alienus
my_version = '0.0.0'
027
2014-02-05
alienus
028
2014-02-16
alienus
# Parse the CLI options
029
2014-02-16
alienus
parser = argparse.ArgumentParser(
030
2014-02-16
alienus
prog='twadeb',
031
2014-02-16
alienus
description='Manage your last installed packages',
032
2014-02-16
alienus
epilog="i.e. twadeb -l 20")
033
2014-02-16
alienus
034
2014-02-16
alienus
parser.add_argument('-t', '--today', action="store_true",
035
2014-02-16
alienus
default=False,
036
2014-02-16
alienus
help='show today installed/upgraded packages')
037
2014-02-16
alienus
038
2014-02-16
alienus
parser.add_argument('-l', '--list', action="store",
039
2014-02-18
alienus
dest='nb', default='10000',
040
2014-02-16
alienus
help='list last NB installed/upgraded packages')
041
2014-02-16
alienus
042
2014-02-16
alienus
parser.add_argument('-f', '--full', action="store_true",
043
2014-02-16
alienus
default=False,
044
2014-02-16
alienus
help='detailed list last NB installed/upgraded packages')
045
2014-02-16
alienus
046
2014-02-16
alienus
parser.add_argument('--version', action='version',
047
2014-02-16
alienus
version='%(prog)s ' + my_version)
048
2014-02-16
alienus
049
2014-02-16
alienus
if len(sys.argv) == 1:
050
2014-02-16
alienus
sys.exit('Wrong usage, see twadeb --help')
051
2014-02-16
alienus
052
2014-02-16
alienus
args = parser.parse_args()
053
2014-02-16
alienus
054
2014-02-16
alienus
055
2014-02-05
alienus
# vars
056
2014-02-10
alienus
dpkglog = '/tmp/dpkg.twadeb'
057
2014-02-08
alienus
alldpkg = '/var/log/dpkg.*'
058
2014-02-16
alienus
nb = int(args.nb)
059
2014-02-16
alienus
full = args.full
060
2014-02-16
alienus
today = args.today
061
2014-02-05
alienus
#
062
2014-02-16
alienus
#
063
2014-02-16
alienus
#
064
2014-02-05
alienus
065
2014-02-05
alienus
066
2014-02-10
alienus
def dpkgFiles(sumOfDpkgFiles, targetFile):
067
2014-02-10
alienus
try:
068
2014-02-10
alienus
os.remove(targetFile)
069
2014-02-10
alienus
except:
070
2014-02-10
alienus
print("No", targetFile)
071
2014-02-10
alienus
finally:
072
2014-02-10
alienus
fout = open(targetFile, 'a')
073
2014-02-10
alienus
# list all dpkg.log files (usually 1 per month)
074
2014-02-10
alienus
listFiles = sorted(glob.glob(sumOfDpkgFiles))
075
2014-02-10
alienus
# concatenate all dpkg.log files
076
2014-02-10
alienus
for l in listFiles:
077
2014-02-10
alienus
# reverse files content to well format files concatenation
078
2014-02-10
alienus
if l.endswith(".gz"):
079
2014-02-10
alienus
with gzip.open(l, 'rt') as s:
080
2014-02-10
alienus
for li in reversed(list(s)):
081
2014-02-10
alienus
fout.write(li)
082
2014-02-10
alienus
else:
083
2014-02-10
alienus
with open(l, 'r') as s:
084
2014-02-10
alienus
for li in reversed(list(s)):
085
2014-02-10
alienus
fout.write(li)
086
2014-02-08
alienus
087
2014-02-08
alienus
088
2014-02-08
alienus
def parseLine(line):
089
2014-02-08
alienus
try:
090
2014-02-08
alienus
date, time, keyword, package, verbef, ver = line.split()
091
2014-02-08
alienus
res = (date, time, keyword, package, verbef, ver)
092
2014-02-08
alienus
except:
093
2014-02-08
alienus
res = None
094
2014-02-08
alienus
return res
095
2014-02-05
alienus
096
2014-02-05
alienus
097
2014-02-08
alienus
def parseFile(nameOfFile):
098
2014-02-10
alienus
with open(nameOfFile, 'r') as f:
099
2014-02-08
alienus
for line in f:
100
2014-02-08
alienus
res = parseLine(line)
101
2014-02-08
alienus
if res:
102
2014-02-08
alienus
date, time, keyword, package, verbef, ver = res
103
2014-02-08
alienus
if keyword == 'upgrade' or keyword == 'install':
104
2014-02-08
alienus
yield res
105
2014-02-08
alienus
106
2014-02-08
alienus
107
2014-02-16
alienus
def lastNpkgs(nb):
108
2014-02-16
alienus
lPkg = []
109
2014-02-28
alienus
global rPkg
110
2014-02-28
alienus
rPkg = []
111
2014-02-10
alienus
for infos in parseFile(dpkglog):
112
2014-02-16
alienus
lPkg.append(infos)
113
2014-02-16
alienus
nbPkgs = len(lPkg)
114
2014-03-10
alienus
if full:
115
2014-03-10
alienus
print(socket.gethostname(), ':', \
116
2014-03-10
alienus
strftime("%Y-%m-%d %H:%M:%S"), '--', nbPkgs, 'packages')
117
2014-03-10
alienus
print('-' * 72)
118
2014-02-28
alienus
lPkg = lPkg[0:nb]
119
2014-02-16
alienus
for elt in lPkg:
120
2014-02-16
alienus
date, time, keyword, package, verbef, ver = elt
121
2014-02-16
alienus
if today:
122
2014-02-16
alienus
nb = nbPkgs
123
2014-02-28
alienus
if date == strftime("%Y-%m-%d"):
124
2014-03-10
alienus
rPkg.append([package, ver])
125
2014-02-16
alienus
elif full:
126
2014-02-16
alienus
keyword = keyword.replace('install', 'I').replace('upgrade', 'U')
127
2014-02-28
alienus
rPkg.append([keyword, date, time, package, ver])
128
2014-02-16
alienus
else:
129
2014-02-28
alienus
rPkg.append(package)
130
2014-02-10
alienus
131
2014-02-10
alienus
132
2014-03-10
alienus
def printPkg():
133
2014-03-10
alienus
i = 1
134
2014-03-10
alienus
for p in rPkg:
135
2014-03-10
alienus
if today:
136
2014-03-10
alienus
print(p[0], p[1])
137
2014-03-10
alienus
else:
138
2014-03-10
alienus
print(i, p)
139
2014-03-10
alienus
i += 1
140
2014-03-10
alienus
141
2014-03-10
alienus
142
2014-02-18
alienus
if __name__ == '__main__':
143
2014-02-16
alienus
# Build the dpkglog file (/tmp/dpkg.twadeb)
144
2014-02-16
alienus
dpkgFiles(alldpkg, dpkglog)
145
2014-02-16
alienus
#
146
2014-02-18
alienus
if full and today:
147
2014-02-18
alienus
sys.exit(\
148
2014-02-18
alienus
'full option is useless with the today one, aborting...')
149
2014-02-16
alienus
lastNpkgs(nb)
150
2014-03-10
alienus
printPkg()
Frédéric Galusik