~fredg/twadeb

ref: 9289bc8617cba4853873ce7e8b5ac50cc07759ad twadeb/twadeb.py -rwxr-xr-x 3.7 KiB
9289bc86 — alienus better print format 8 years ago
                                                                                
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 python3
#
#       twadeb.py
#
#       Copyright 2014 Frédéric Galusik <alienus~at~riseup~dot~net>
#
#
# License: BSD Revised
#
# Use the dpkg.log file to show last installed packages
#
#
import os
import sys
import gzip
import re
import glob
import argparse
from time import gmtime, strftime
import socket
import subprocess

# Program information
my_url = 'https://gitlab.com/alienus/twadeb'
my_name = 'twadeb'
my_version = '0.0.0'

# Parse the CLI options
parser = argparse.ArgumentParser(
        prog='twadeb',
        description='Manage your last installed packages',
        epilog="i.e. twadeb -l 20")

parser.add_argument('-t', '--today', action="store_true",
        default=False,
        help='show today installed/upgraded packages')

parser.add_argument('-l', '--list', action="store",
        dest='nb', default='10000',
        help='list last NB installed/upgraded packages')

parser.add_argument('-f', '--full', action="store_true",
        default=False,
        help='detailed list last NB installed/upgraded packages')

parser.add_argument('--version', action='version',
        version='%(prog)s ' + my_version)

if len(sys.argv) == 1:
    sys.exit('Wrong usage, see twadeb --help')

args = parser.parse_args()


# vars
dpkglog = '/tmp/dpkg.twadeb'
alldpkg = '/var/log/dpkg.*'
nb = int(args.nb)
full = args.full
today = args.today
#
#
#


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):
    try:
        date, time, keyword, package, verbef, ver = line.split()
        res = (date, time, keyword, package, verbef, ver)
    except:
        res = None
    return res


def parseFile(nameOfFile):
    with open(nameOfFile, 'r') as f:
        for line in f:
            res = parseLine(line)
            if res:
                date, time, keyword, package, verbef, ver = res
                if keyword == 'upgrade' or keyword == 'install':
                    yield res


def lastNpkgs(nb):
    lPkg = []
    global rPkg
    rPkg = []
    for infos in parseFile(dpkglog):
        lPkg.append(infos)
    nbPkgs = len(lPkg)
    if full:
        print(socket.gethostname(), ':', \
                  strftime("%Y-%m-%d %H:%M:%S"), '--', nbPkgs, 'packages')
        print('-' * 72)
    lPkg = lPkg[0:nb]
    for elt in lPkg:
        date, time, keyword, package, verbef, ver = elt
        if today:
            nb = nbPkgs
            if date == strftime("%Y-%m-%d"):
                rPkg.append([package, ver])
        elif full:
            keyword = keyword.replace('install', 'I').replace('upgrade', 'U')
            rPkg.append([keyword, date, time, package, ver])
        else:
            rPkg.append(package)


def printPkg():
    i = 1
    for p in rPkg:
        if today:
            print(p[0], p[1])
        else:
            print(i, p)
            i += 1


if __name__ == '__main__':
    # Build the dpkglog file (/tmp/dpkg.twadeb)
    dpkgFiles(alldpkg, dpkglog)
    #
    if full and today:
        sys.exit(\
            'full option is useless with the today one, aborting...')
    lastNpkgs(nb)
    printPkg()