Skip to content

Commit

Permalink
[sosreport] move archive checksumming to sosreport
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bmr-cymru committed Dec 4, 2015
1 parent 7f27277 commit 19e2bbc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
22 changes: 1 addition & 21 deletions sos/policies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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
Expand Down Expand Up @@ -285,22 +284,12 @@ def is_root(self):
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
Expand All @@ -312,19 +301,10 @@ def display_results(self, archive, directory):

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))

Expand Down
26 changes: 25 additions & 1 deletion sos/sosreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from time import strftime, localtime
from collections import deque
import tempfile
import hashlib

from sos import _sos as _
from sos import __version__
Expand Down Expand Up @@ -1432,6 +1433,18 @@ def postproc(self):
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.
Expand Down Expand Up @@ -1466,7 +1479,18 @@ def final_work(self):
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
Expand Down

0 comments on commit 19e2bbc

Please sign in to comment.