diff options
author | Bryn M. Reeves <bmr@redhat.com> | 2015-11-19 19:19:42 +0000 |
---|---|---|
committer | Bryn M. Reeves <bmr@redhat.com> | 2015-12-04 15:43:15 +0000 |
commit | 19e2bbccb6a86d6ea94f5c82860bed4d2276bbf3 (patch) | |
tree | 2bb39d9021ad61aec7412beda03a352f11343d09 | |
parent | 7f2727749d0c37095a20c5d4cf6f9a2e086a2375 (diff) | |
download | sos-19e2bbccb6a86d6ea94f5c82860bed4d2276bbf3.tar.gz |
[sosreport] move archive checksumming to sosreport
Although the digest algorithm is policy controlled the actual
mechanism to checksum the archive does not belong in the policies
module: historically this was done to keep the code that calculates
the checksum close to the UI code that reports it.
Move the calculation to the main SoSReport class's final_work()
method and add a 'checksum' argument to the display_results()
method so that the value can be reported.
In future it may make sense to push the checksum code directly into
the archive class.
Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r-- | sos/policies/__init__.py | 22 | ||||
-rw-r--r-- | sos/sosreport.py | 26 |
2 files changed, 26 insertions, 22 deletions
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index 5b0b7063..20e0e3b6 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -13,7 +13,6 @@ from sos.utilities import (ImporterHelper, shell_out) from sos.plugins import IndependentPlugin, ExperimentalPlugin from sos import _sos as _ -import hashlib from textwrap import fill from six import print_ from six.moves import input @@ -285,22 +284,12 @@ No changes will be made to system configuration. considered to be a superuser""" return (os.getuid() == 0) - def _create_checksum(self, hash_name, archive=None): - if not archive: - return False - - archive_fp = open(archive, 'rb') - digest = hashlib.new(hash_name) - digest.update(archive_fp.read()) - archive_fp.close() - return digest.hexdigest() - def get_preferred_hash_name(self): """Returns the string name of the hashlib-supported checksum algorithm to use""" return "md5" - def display_results(self, archive, directory): + def display_results(self, archive, directory, checksum): # Display results is called from the tail of SoSReport.final_work() # # Logging is already shutdown and all terminal output must use the @@ -312,19 +301,10 @@ No changes will be made to system configuration. self._print() - hash_name = self.get_preferred_hash_name() if archive: - # store checksum into file - fp = open(archive + "." + hash_name, "w") - checksum = self._create_checksum(hash_name, archive) - if checksum: - fp.write(checksum + "\n") - fp.close() - self._print(_("Your sosreport has been generated and saved " "in:\n %s") % archive) else: - checksum = None self._print(_("sosreport build tree is located at : %s" % directory)) diff --git a/sos/sosreport.py b/sos/sosreport.py index f3e0f346..f7a5f11a 100644 --- a/sos/sosreport.py +++ b/sos/sosreport.py @@ -33,6 +33,7 @@ from stat import ST_UID, ST_GID, ST_MODE, ST_CTIME, ST_ATIME, ST_MTIME, S_IMODE from time import strftime, localtime from collections import deque import tempfile +import hashlib from sos import _sos as _ from sos import __version__ @@ -1432,6 +1433,18 @@ class SoSReport(object): raise self._log_plugin_exception(plugname, "postproc") + def _create_checksum(self, archive=None): + if not archive: + return False + + hash_name = self.policy.get_preferred_hash_name() + + archive_fp = open(archive, 'rb') + digest = hashlib.new(hash_name) + digest.update(archive_fp.read()) + archive_fp.close() + return digest.hexdigest() + def final_work(self): # this must come before archive creation to ensure that log # files are closed and cleaned up at exit. @@ -1466,7 +1479,18 @@ class SoSReport(object): else: directory = self.archive.get_archive_path() - self.policy.display_results(archive, directory) + hash_name = self.policy.get_preferred_hash_name() + checksum = None + + if hash_name and not self.opts.build: + # store checksum into file + fp = open(archive + "." + hash_name, "w") + checksum = self._create_checksum(archive) + if checksum: + fp.write(checksum + "\n") + fp.close() + + self.policy.display_results(archive, directory, checksum) self.tempfile_util.clean() return True |