~fredg/twadeb

113dfa4aa39fe3df4077b43812480b6e963d3db0 — alienus 10 years ago 4050f12
let's begin
1 files changed, 81 insertions(+), 0 deletions(-)

A twadeb.py
A twadeb.py => twadeb.py +81 -0
@@ 0,0 1,81 @@
#!/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 gzip
import re


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

# vars
dpkglog = '/var/log/dpkg.log.2.gz'
#


class Package():
    def __init__(self):
        """
        Initialize a new package.
        """
        self.name = ''
        self.date = ''
        self.hour = ''
        self.status = ''
        self.verbef = ''
        self.ver = ''

    def parse(self, dpkglog):
        """
        Parse the dpkg.log.2.gz file and build the dpkg dictionnary.
        """
        self.dpkglog = dpkglog
        self.pkg = Package()
        self.dpkgd = {}
        with gzip.open(dpkglog, 'rt') as f:
            for line in f:
                pkgline = re.match(
                    r'(\d{4}-\d{2}-\d{2})(\s)(\d{2}:\d{2}:\d{2})(\s)\
                    (upgrade|install)(\s)(\S*)(\s)(\S*)(\s)(\S*)', line
                )
                if pkgline:
                    self.pkg.date = pkgline.group(1)
                    self.pkg.hour = pkgline.group(3)
                    self.pkg.status = pkgline.group(5)
                    self.pkg.name = pkgline.group(7)
                    self.pkg.name = self.pkg.name.replace(':i386', '').\
                        replace(':all', '').\
                        replace(':x64', '')  # FIXME
        # http://hastebin.com/qawaxoketi.css pour 64b
                    self.pkg.verbef = pkgline.group(9)
                    self.pkg.ver = pkgline.group(11)
                if pkgline and self.pkg.name:
                    self.dpkgd.setdefault(
                        self.pkg.name, []).append(self.pkg.date)
                    self.dpkgd.setdefault(
                        self.pkg.name, []).append(self.pkg.hour)
                    self.dpkgd.setdefault(
                        self.pkg.name, []).append(self.pkg.ver)
                    self.dpkgd.setdefault(
                        self.pkg.name, []).append(self.pkg.status)
                    self.pkg = Package()
            return self.dpkgd


def pkgdic():
    return Package.parse(Package(), dpkglog)

#for k, v in pkgdic().items(): print(k, v[0])
print(pkgdic())