check: Refactor and improve whole check command
Warn about denied accesses (pb#18308) Fix bad formatting issues BUG:FIXED:18308
This commit is contained in:
+29
-11
@@ -1,6 +1,6 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2005 - 2007, TUBITAK/UEKAE
|
||||
# Copyright (C) 2005 - 2011, TUBITAK/UEKAE
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free
|
||||
@@ -56,28 +56,46 @@ If no packages are given, checks all installed packages.
|
||||
|
||||
component = ctx.get_option('component')
|
||||
if component:
|
||||
#FIXME: pisi api is insufficient to do this
|
||||
installed = pisi.api.list_installed()
|
||||
component_pkgs = self.componentdb.get_union_packages(component, walk=True)
|
||||
pkgs = list(set(installed) & set(component_pkgs))
|
||||
elif self.args:
|
||||
pkgs = self.args
|
||||
else:
|
||||
ctx.ui.info(_('Checking all installed packages'))
|
||||
ctx.ui.info(_('Checking all installed packages') + '\n')
|
||||
pkgs = pisi.api.list_installed()
|
||||
|
||||
# True if we should also check the configuration files
|
||||
check_config = ctx.get_option('config')
|
||||
|
||||
# Line prefix
|
||||
prefix = _('Checking integrity of %s')
|
||||
|
||||
# Determine maximum length of messages for proper formatting
|
||||
maxpkglen = max([len(_p) for _p in pkgs])
|
||||
|
||||
for pkg in pkgs:
|
||||
ctx.ui.info(_('Checking %s... ') % pkg, noln=True)
|
||||
if self.installdb.has_package(pkg):
|
||||
check_results = pisi.api.check(pkg, check_config)
|
||||
corrupted = check_results['missing'] or check_results['corrupted']
|
||||
if corrupted:
|
||||
if check_config:
|
||||
ctx.ui.info(_('\nPackage %s has changed config files.') % pkg)
|
||||
else:
|
||||
ctx.ui.info(_('\nPackage %s is corrupt.') % pkg)
|
||||
ctx.ui.info("%s %s" % ((prefix % pkg), ' ' * (maxpkglen - len(pkg))), noln=True)
|
||||
|
||||
if check_results['missing'] or check_results['corrupted'] \
|
||||
or check_results['config']:
|
||||
ctx.ui.info(pisi.util.colorize(_("Broken"), 'brightred'))
|
||||
|
||||
# Dump per file stuff
|
||||
for fpath in check_results['missing']:
|
||||
ctx.ui.info(" %s" % pisi.util.colorize(_("Missing file: /%s") % fpath, 'brightred'))
|
||||
for fpath in check_results['denied']:
|
||||
ctx.ui.info(" %s" % pisi.util.colorize(_("Access denied: /%s") % fpath, 'yellow'))
|
||||
for fpath in check_results['corrupted']:
|
||||
ctx.ui.info(" %s" % pisi.util.colorize(_("Corrupted file: /%s") % fpath, 'brightyellow'))
|
||||
for fpath in check_results['config']:
|
||||
ctx.ui.info(" %s" % pisi.util.colorize(_("Modified configuration file: /%s") % fpath, 'brightyellow'))
|
||||
|
||||
else:
|
||||
ctx.ui.info(_("OK"), verbose=False)
|
||||
# Show OK packages only in verbose
|
||||
ctx.ui.info(pisi.util.colorize(_("OK"), 'green'))
|
||||
else:
|
||||
# Package is not installed
|
||||
ctx.ui.info(_('Package %s not installed') % pkg)
|
||||
|
||||
+32
-14
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2008 - 2010, TUBITAK/UEKAE
|
||||
# Copyright (C) 2008 - 2011, TUBITAK/UEKAE
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free
|
||||
@@ -23,32 +23,50 @@ def file_corrupted(pfile):
|
||||
if pisi.util.sha1_data(pisi.util.read_link(path)) != pfile.hash:
|
||||
return True
|
||||
else:
|
||||
if pisi.util.sha1_file(path) != pfile.hash:
|
||||
return True
|
||||
try:
|
||||
if pisi.util.sha1_file(path) != pfile.hash:
|
||||
return True
|
||||
except pisi.util.FilePermissionDeniedError, e:
|
||||
raise e
|
||||
return False
|
||||
|
||||
def check_files(files, check_config=False):
|
||||
results = {'missing':[], 'corrupted':[]}
|
||||
results = {
|
||||
'missing' : [],
|
||||
'corrupted' : [],
|
||||
'denied' : [],
|
||||
'config' : [],
|
||||
}
|
||||
|
||||
for f in files:
|
||||
if not check_config and f.type == "config":
|
||||
continue
|
||||
if not f.hash:
|
||||
continue
|
||||
ctx.ui.info(_("Checking /%s ") % f.path, noln=True, verbose=True)
|
||||
|
||||
is_file_corrupted = False
|
||||
|
||||
path = os.path.join(ctx.config.dest_dir(), f.path)
|
||||
if os.path.lexists(path):
|
||||
if file_corrupted(f):
|
||||
if f.type == "config":
|
||||
msg = _("\nChanged config file: %s")
|
||||
else:
|
||||
msg = _("\nCorrupt file: %s")
|
||||
ctx.ui.error(msg % ("/%s" %f.path))
|
||||
results['corrupted'].append(f.path)
|
||||
try:
|
||||
is_file_corrupted = file_corrupted(f)
|
||||
|
||||
except pisi.util.FilePermissionDeniedError, e:
|
||||
# Can't read file, probably because of permissions, skip
|
||||
results['denied'].append(f.path)
|
||||
|
||||
else:
|
||||
ctx.ui.info(_("OK"), verbose=True)
|
||||
if is_file_corrupted:
|
||||
# Detect file type
|
||||
if f.type == "config":
|
||||
results['config'].append(f.path)
|
||||
else:
|
||||
results['corrupted'].append(f.path)
|
||||
|
||||
else:
|
||||
ctx.ui.error(_("\nMissing file: /%s") % f.path)
|
||||
# Shipped file doesn't exist on the system
|
||||
results['missing'].append(f.path)
|
||||
|
||||
return results
|
||||
|
||||
def check_config_files(package):
|
||||
|
||||
Reference in New Issue
Block a user