~fredg/twadeb

1c702b34406755d61c94120338fe16bec6f0d02b — alienus 10 years ago 113dfa4
list instead of dict
1 files changed, 22 insertions(+), 54 deletions(-)

M twadeb.py
M twadeb.py => twadeb.py +22 -54
@@ 25,57 25,25 @@ 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())
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 gzip.open(nameOfFile, 'rt') 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


# Pour test
for infos in parseFile(dpkglog):
    print(infos)