..
This commit is contained in:
parent
f169080f59
commit
e1eefebf9d
@ -1,110 +1,185 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
# Supports CentOS 6.9 and up, untested on lower versions.
|
# Supports CentOS 6.9 and up, untested on lower versions.
|
||||||
# Lets you get a list of files for a given package name(s) without installing
|
# Lets you dump a list of installed packages for backup purposes
|
||||||
# any extra packages (such as yum-utils for repoquery).
|
# Reference: https://blog.fpmurphy.com/2011/08/programmatically-retrieve-rpm-package-details.html
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import copy
|
||||||
|
import datetime
|
||||||
|
import io
|
||||||
import re
|
import re
|
||||||
# For when CentOS/RHEL switch to python 3 by default (if EVER).
|
|
||||||
import sys
|
import sys
|
||||||
pyver = sys.version_info
|
|
||||||
try:
|
try:
|
||||||
import rpm
|
import yum
|
||||||
except ImportError:
|
except ImportError:
|
||||||
exit('This script only runs on RHEL/CentOS/other RPM-based distros.')
|
exit('This script only runs on RHEL/CentOS/other yum-based distros.')
|
||||||
|
# Detect RH version.
|
||||||
|
ver_re = re.compile('^(centos( linux)? release) ([0-9\.]+) .*$', re.IGNORECASE)
|
||||||
|
# distro module isn't stdlib, and platform.linux_distribution() (AND platform.distro()) are both deprecated in 3.7.
|
||||||
|
# So we get hacky.
|
||||||
|
with open('/etc/redhat-release', 'r') as f:
|
||||||
|
ver = [int(i) for i in ver_re.sub('\g<3>', f.read().strip()).split('.')]
|
||||||
|
import pprint
|
||||||
|
|
||||||
def all_pkgs():
|
repo_re = re.compile('^@')
|
||||||
# Gets a list of all packages.
|
|
||||||
pkgs = []
|
|
||||||
trns = rpm.TransactionSet()
|
|
||||||
for p in trns.dbMatch():
|
|
||||||
pkgs.append(p['name'])
|
|
||||||
pkgs = list(sorted(set(pkgs)))
|
|
||||||
return(pkgs)
|
|
||||||
|
|
||||||
class FileGetter(object):
|
class PkgIndexer(object):
|
||||||
def __init__(self, symlinks = True, verbose = False, *args, **kwargs):
|
def __init__(self, **args):
|
||||||
self.symlinks = symlinks
|
self.pkgs = []
|
||||||
self.verbose = verbose
|
self.args = args
|
||||||
self.trns = rpm.TransactionSet()
|
self.yb = yum.YumBase()
|
||||||
|
# Make the Yum API shut the heck up.
|
||||||
|
self.yb.preconf.debuglevel = 0
|
||||||
|
self.yb.preconf.errorlevel = 0
|
||||||
|
self._pkgs = self._pkglst()
|
||||||
|
self._build_pkginfo()
|
||||||
|
if self.args['report'] == 'csv':
|
||||||
|
self._gen_csv()
|
||||||
|
elif self.args['report'] == 'json':
|
||||||
|
self._gen_json()
|
||||||
|
elif self.args['report'] == 'xml':
|
||||||
|
self._gen_xml()
|
||||||
|
|
||||||
def getfiles(self, pkgnm):
|
def _pkglst(self):
|
||||||
files = {}
|
pkgs = []
|
||||||
for pkg in self.trns.dbMatch('name', pkgnm):
|
# Get the list of packages
|
||||||
# The canonical package name
|
if self.args['reason'] != 'all':
|
||||||
_pkgnm = pkg.sprintf('%{NAME}')
|
for p in sorted(self.yb.rpmdb.returnPackages()):
|
||||||
# Return just a list of files, or a dict of filepath:hash
|
if 'reason' not in p.yumdb_info:
|
||||||
# if verbose is enabled.
|
continue
|
||||||
if self.verbose:
|
reason = getattr(p.yumdb_info, 'reason')
|
||||||
files[_pkgnm] = {}
|
if reason == self.args['reason']:
|
||||||
|
pkgs.append(p)
|
||||||
|
else:
|
||||||
|
pkgs = sorted(self.yb.rpmdb.returnPackages())
|
||||||
|
return(pkgs)
|
||||||
|
|
||||||
|
def _build_pkginfo(self):
|
||||||
|
for p in self._pkgs:
|
||||||
|
_pkg = {'name': p.name,
|
||||||
|
'desc': p.summary,
|
||||||
|
'version': p.ver,
|
||||||
|
'release': p.release,
|
||||||
|
'arch': p.arch,
|
||||||
|
'built': datetime.datetime.fromtimestamp(p.buildtime),
|
||||||
|
'installed': datetime.datetime.fromtimestamp(p.installtime),
|
||||||
|
'repo': repo_re.sub('', p.ui_from_repo),
|
||||||
|
'sizerpm': p.packagesize,
|
||||||
|
'sizedisk': p.installedsize}
|
||||||
|
self.pkgs.append(_pkg)
|
||||||
|
|
||||||
|
def _gen_csv(self):
|
||||||
|
if self.args['plain']:
|
||||||
|
_fields = ['name']
|
||||||
|
else:
|
||||||
|
_fields = ['name', 'version', 'release', 'arch', 'desc', 'built',
|
||||||
|
'installed', 'repo', 'sizerpm', 'sizedisk']
|
||||||
|
import csv
|
||||||
|
if sys.hexversion >= 0x30000f0:
|
||||||
|
_buf = io.StringIO()
|
||||||
|
else:
|
||||||
|
_buf = io.BytesIO()
|
||||||
|
_csv = csv.writer(_buf, delimiter = self.args['sep_char'])
|
||||||
|
if self.args['header']:
|
||||||
|
if self.args['plain']:
|
||||||
|
_csv.writerow(['Name'])
|
||||||
else:
|
else:
|
||||||
files[_pkgnm] = []
|
_csv.writerow(['Name', 'Version', 'Release', 'Architecture', 'Description', 'Build Time',
|
||||||
for f in pkg.fiFromHeader():
|
'Install Time', 'Repository', 'Size (RPM)', 'Size (On-Disk)'])
|
||||||
_symlink = (True if re.search('^0+$', f[12]) else False)
|
_csv = csv.DictWriter(_buf, fieldnames = _fields, extrasaction = 'ignore', delimiter = self.args['sep_char'])
|
||||||
if self.verbose:
|
for p in self.pkgs:
|
||||||
if _symlink:
|
_csv.writerow(p)
|
||||||
if self.symlinks:
|
_buf.seek(0, 0)
|
||||||
files[_pkgnm][f[0]] = '(symbolic link)'
|
self.report = _buf.read()
|
||||||
continue
|
return()
|
||||||
files[_pkgnm][f[0]] = f[12]
|
|
||||||
else:
|
def _gen_json(self):
|
||||||
# Skip if it is a symlink but they aren't enabled
|
import json
|
||||||
if _symlink and not self.symlinks:
|
self.report = json.dumps(self.pkgs, default = str, indent = 4)
|
||||||
continue
|
return()
|
||||||
else:
|
|
||||||
files[_pkgnm].append(f[0])
|
def _gen_xml(self):
|
||||||
files[_pkgnm].sort()
|
from lxml import etree
|
||||||
return(files)
|
_xml = etree.Element('packages')
|
||||||
|
for p in self.pkgs:
|
||||||
|
_attrib = copy.deepcopy(p)
|
||||||
|
for i in ('built', 'installed', 'sizerpm', 'sizedisk'):
|
||||||
|
_attrib[i] = str(_attrib[i])
|
||||||
|
_pkg = etree.Element('package', attrib = _attrib)
|
||||||
|
_xml.append(_pkg)
|
||||||
|
#del(_attrib['name']) # I started to make it a more complex, nested structure... is that necessary?
|
||||||
|
if self.args['header']:
|
||||||
|
self.report = etree.tostring(_xml, pretty_print = True, xml_declaration = True, encoding = 'UTF-8')
|
||||||
|
else:
|
||||||
|
self.report = etree.tostring(_xml, pretty_print = True)
|
||||||
|
return()
|
||||||
|
|
||||||
|
|
||||||
def parseArgs():
|
def parseArgs():
|
||||||
args = argparse.ArgumentParser(description = (
|
args = argparse.ArgumentParser(description = ('This script lets you dump the list of installed packages'))
|
||||||
'This script allows you get a list of files for a given '
|
args.add_argument('-p', '--plain',
|
||||||
'package name(s) without installing any extra packages '
|
dest = 'plain',
|
||||||
'(such as yum-utils for repoquery).'))
|
|
||||||
args.add_argument('-l', '--ignore-symlinks',
|
|
||||||
dest = 'symlinks',
|
|
||||||
action = 'store_false',
|
|
||||||
help = ('If specified, don\'t report files that are ' +
|
|
||||||
'symlinks in the RPM'))
|
|
||||||
args.add_argument('-v', '--verbose',
|
|
||||||
dest = 'verbose',
|
|
||||||
action = 'store_true',
|
action = 'store_true',
|
||||||
help = ('If specified, include the hashes of the files'))
|
help = 'If specified, only create a list of plain package names (i.e. don\'t include extra '
|
||||||
args.add_argument('-p', '--package',
|
'information)')
|
||||||
dest = 'pkgs',
|
args.add_argument('-n', '--no-header',
|
||||||
#nargs = 1,
|
dest = 'header',
|
||||||
metavar = 'PKGNAME',
|
action = 'store_false',
|
||||||
action = 'append',
|
help = 'If specified, do not print column headers/XML headers')
|
||||||
default = [],
|
args.add_argument('-s', '--separator',
|
||||||
help = ('If specified, restrict the list of ' +
|
dest = 'sep_char',
|
||||||
'packages to check against to only this ' +
|
default = ',',
|
||||||
'package. Can be specified multiple times. ' +
|
help = 'The separator used to split fields in the output (default: ,) (only used for CSV '
|
||||||
'HIGHLY RECOMMENDED'))
|
'reports)')
|
||||||
|
rprt = args.add_mutually_exclusive_group()
|
||||||
|
rprt.add_argument('-c', '--csv',
|
||||||
|
dest = 'report',
|
||||||
|
default = 'csv',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'csv',
|
||||||
|
help = 'Generate CSV output (this is the default). See -n/--no-header, -s/--separator')
|
||||||
|
rprt.add_argument('-x', '--xml',
|
||||||
|
dest = 'report',
|
||||||
|
default = 'csv',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'xml',
|
||||||
|
help = 'Generate XML output (requires the LXML module: yum install python-lxml)')
|
||||||
|
rprt.add_argument('-j', '--json',
|
||||||
|
dest = 'report',
|
||||||
|
default = 'csv',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'json',
|
||||||
|
help = 'Generate JSON output')
|
||||||
|
rsn = args.add_mutually_exclusive_group()
|
||||||
|
rsn.add_argument('-a', '--all',
|
||||||
|
dest = 'reason',
|
||||||
|
default = 'all',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'all',
|
||||||
|
help = ('Parse/report all packages that are currently installed. '
|
||||||
|
'Conflicts with -u/--user and -d/--dep. '
|
||||||
|
'This is the default'))
|
||||||
|
rsn.add_argument('-u', '--user',
|
||||||
|
dest = 'reason',
|
||||||
|
default = 'all',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'user',
|
||||||
|
help = ('Parse/report only packages which were explicitly installed. '
|
||||||
|
'Conflicts with -a/--all and -d/--dep'))
|
||||||
|
rsn.add_argument('-d', '--dep',
|
||||||
|
dest = 'reason',
|
||||||
|
default = 'all',
|
||||||
|
action = 'store_const',
|
||||||
|
const = 'dep',
|
||||||
|
help = ('Parse/report only packages which were installed to satisfy a dependency. '
|
||||||
|
'Conflicts with -a/--all and -u/--user'))
|
||||||
return(args)
|
return(args)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = vars(parseArgs().parse_args())
|
args = vars(parseArgs().parse_args())
|
||||||
if not args['pkgs']:
|
p = PkgIndexer(**args)
|
||||||
prompt_str = (
|
print(p.report)
|
||||||
'You have not specified any package names.\nThis means we will '
|
|
||||||
'get file lists for EVERY SINGLE installed package.\nThis is a '
|
|
||||||
'LOT of output and can take a few moments.\nIf this was a '
|
|
||||||
'mistake, you can hit ctrl-c now.\nOtherwise, hit the enter key '
|
|
||||||
'to continue.\n')
|
|
||||||
sys.stderr.write(prompt_str)
|
|
||||||
if pyver.major >= 3:
|
|
||||||
input()
|
|
||||||
elif pyver.major == 2:
|
|
||||||
raw_input()
|
|
||||||
args['pkgs'] = all_pkgs()
|
|
||||||
gf = FileGetter(**args)
|
|
||||||
file_rslts = {}
|
|
||||||
for p in args['pkgs']:
|
|
||||||
if p not in file_rslts.keys():
|
|
||||||
file_rslts[p] = gf.getfiles(p)
|
|
||||||
print(json.dumps(file_rslts, indent = 4))
|
|
||||||
return()
|
return()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user