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:
Ozan Çağlayan
2011-06-08 07:26:54 +00:00
parent 95660d6891
commit 2ea697da6f
2 changed files with 61 additions and 25 deletions
+29 -11
View File
@@ -1,6 +1,6 @@
# -*- coding:utf-8 -*- # -*- 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 # 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 # 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') component = ctx.get_option('component')
if component: if component:
#FIXME: pisi api is insufficient to do this
installed = pisi.api.list_installed() installed = pisi.api.list_installed()
component_pkgs = self.componentdb.get_union_packages(component, walk=True) component_pkgs = self.componentdb.get_union_packages(component, walk=True)
pkgs = list(set(installed) & set(component_pkgs)) pkgs = list(set(installed) & set(component_pkgs))
elif self.args: elif self.args:
pkgs = self.args pkgs = self.args
else: else:
ctx.ui.info(_('Checking all installed packages')) ctx.ui.info(_('Checking all installed packages') + '\n')
pkgs = pisi.api.list_installed() pkgs = pisi.api.list_installed()
# True if we should also check the configuration files
check_config = ctx.get_option('config') 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: for pkg in pkgs:
ctx.ui.info(_('Checking %s... ') % pkg, noln=True)
if self.installdb.has_package(pkg): if self.installdb.has_package(pkg):
check_results = pisi.api.check(pkg, check_config) check_results = pisi.api.check(pkg, check_config)
corrupted = check_results['missing'] or check_results['corrupted'] ctx.ui.info("%s %s" % ((prefix % pkg), ' ' * (maxpkglen - len(pkg))), noln=True)
if corrupted:
if check_config: if check_results['missing'] or check_results['corrupted'] \
ctx.ui.info(_('\nPackage %s has changed config files.') % pkg) or check_results['config']:
else: ctx.ui.info(pisi.util.colorize(_("Broken"), 'brightred'))
ctx.ui.info(_('\nPackage %s is corrupt.') % pkg)
# 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: else:
ctx.ui.info(_("OK"), verbose=False) # Show OK packages only in verbose
ctx.ui.info(pisi.util.colorize(_("OK"), 'green'))
else: else:
# Package is not installed
ctx.ui.info(_('Package %s not installed') % pkg) ctx.ui.info(_('Package %s not installed') % pkg)
+32 -14
View File
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- 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 # 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 # 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: if pisi.util.sha1_data(pisi.util.read_link(path)) != pfile.hash:
return True return True
else: else:
if pisi.util.sha1_file(path) != pfile.hash: try:
return True if pisi.util.sha1_file(path) != pfile.hash:
return True
except pisi.util.FilePermissionDeniedError, e:
raise e
return False return False
def check_files(files, check_config=False): def check_files(files, check_config=False):
results = {'missing':[], 'corrupted':[]} results = {
'missing' : [],
'corrupted' : [],
'denied' : [],
'config' : [],
}
for f in files: for f in files:
if not check_config and f.type == "config": if not check_config and f.type == "config":
continue continue
if not f.hash: if not f.hash:
continue 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) path = os.path.join(ctx.config.dest_dir(), f.path)
if os.path.lexists(path): if os.path.lexists(path):
if file_corrupted(f): try:
if f.type == "config": is_file_corrupted = file_corrupted(f)
msg = _("\nChanged config file: %s")
else: except pisi.util.FilePermissionDeniedError, e:
msg = _("\nCorrupt file: %s") # Can't read file, probably because of permissions, skip
ctx.ui.error(msg % ("/%s" %f.path)) results['denied'].append(f.path)
results['corrupted'].append(f.path)
else: 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: else:
ctx.ui.error(_("\nMissing file: /%s") % f.path) # Shipped file doesn't exist on the system
results['missing'].append(f.path) results['missing'].append(f.path)
return results return results
def check_config_files(package): def check_config_files(package):