~fredg/twadeb

2d47c633f1d55dcbc8c2fbebf09ee598f2278ead — alienus 10 years ago 6277abd
argparse & formatted full output
1 files changed, 48 insertions(+), 4 deletions(-)

M twadeb.py
M twadeb.py => twadeb.py +48 -4
@@ 11,19 11,47 @@
#
#
import os
import sys
import gzip
import re
import glob
import argparse

# 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 10")

parser.add_argument('-l', '--list', action="store",
        dest='nb',
        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.*'
listFiles = []
nb = int(args.nb)
full = args.full
#
#
#




@@ 68,11 96,27 @@ def parseFile(nameOfFile):
                    yield res


def lastNpkgs(nb):
    lPkg = []
    for infos in parseFile(dpkglog):
        lPkg.append(infos)
    nbPkgs = len(lPkg)
    lPkg = lPkg[0:nb]
    for elt in lPkg:
        date, time, keyword, package, verbef, ver = elt
        if full:
            keyword = keyword.replace('install', 'I').replace('upgrade', 'U')
            print(keyword, date, time, package.ljust(24), ver)
        else:
            print(package)


def main():
    # Pour test
    # Build the dpkglog file (/tmp/dpkg.twadeb)
    dpkgFiles(alldpkg, dpkglog)
    for infos in parseFile(dpkglog):
        print(infos)
    #
    if nb:
        lastNpkgs(nb)


if __name__ == '__main__':