Thursday, December 15, 2011

RPM Changelogs for Recent Updates

Note, in a more recent post I've sped up this code ten times.
In my previous post I showed you a script that could report recent changelogs for OpenSUSE packages. Overnight I realised I could generalise this to all RPM based distros. Here is a new generalised version:
% python rpmChangeLogs.py -h
Usage: rpmChangeLogs.py [options]

Report change log entries for recent rpm installs.

Options:
  -h, --help            show this help message and exit
  -i INSTALLDAYS, --installed-since=INSTALLDAYS
                        Include anything installed up to INSTALLDAYS days ago.
  -c CHANGEDAYS, --changedSince=CHANGEDAYS
                        Report change log entries from up to CHANGEDAYS days
                        ago.


The output is the same as the previous OpenSUSE only script.

I've also cleaned up the code around python sub-processes.

The code


#!/usr/bin/env python
#
# rpmChangelogs.py 
#
# Copyright (C) 2011: Michael Hamilton
# The code is GPL 3.0(GNU General Public License) ( http://www.gnu.org/copyleft/gpl.html )
#
import subprocess
from datetime import date, datetime,  timedelta
from optparse import OptionParser

optParser = OptionParser(description='Report change log entries for recent rpm installs.')
optParser.add_option('-i',  '--installed-since',  dest='INSTALLDAYS', type='int', default=1,  help='Include anything installed up to INSTALLDAYS days ago.')
optParser.add_option('-c',  '--changedSince',  dest='CHANGEDAYS', type='int', default=60,  help='Report change log entries from up to CHANGEDAYS days ago.')
(options, args) = optParser.parse_args()

installedSince = datetime.now() - timedelta(days=options.INSTALLDAYS)
changedSince = datetime.now() - timedelta(days=options.CHANGEDAYS)

queryProcess = subprocess.Popen(['rpm', '-q', '-a', '--last'], shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
for queryLine in queryProcess.stdout:
    historyRec = str.split(queryLine, ' ', 1)
    installDatetime = datetime.strptime(str.strip(historyRec[1])[4:24], '%d %b %Y %H:%M:%S')
    if installDatetime < installedSince:
        break
    packageName = historyRec[0]
    print '=================================================='
    print '+Package: ',  installDatetime, packageName
    print '------------------------------'
    rpmProcess = subprocess.Popen(['rpm', '-q', '--changelog',  packageName], shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
    for line in rpmProcess.stdout:
        try:
            if line[0] == '*' and line[1] == ' ' and len(line) > 17:
                changeDate = datetime.strptime(line[6:17], '%b %d %Y')
                if changeDate < changedSince:
                    break
        except ValueError:
            pass # not a date - move on
        print line, 
    rpmProcess.stdout.close()
    rpmProcess.wait()
    if rpmProcess.returncode != 0:
        print '*** ERROR (return code was ', rpmProcess.returncode,  ')'
    for line in rpmProcess.stderr:
        print line, 
    rpmProcess.stderr.close()
queryProcess.stdout.close()
queryProcess.wait()
if queryProcess.returncode != 0:
    print '*** ERROR (return code was ', queryProcess.returncode,  ')'
for line in queryProcess.stderr:
   print line, 

No comments:

Post a Comment

These days we're only getting spam, so comments are now disabled.

Note: Only a member of this blog may post a comment.