commit 1405f633e4ae3bd3988afb09792ef1fb4ed79e87
Author: Radek Pazdera <rpazdera(a)redhat.com>
Date: Thu Nov 8 16:17:44 2012 +0100
Utils: Addding dir_md5sum and create_tar_archive
This commit adds the above mentioned functions to Common/Utils.
The former will compute md5hash of a directory by taring the
whole dir into an archive and then computing its hash.
The latter function can be used for creating tar archives with
or without bzip2 compression.
Signed-off-by: Radek Pazdera <rpazdera(a)redhat.com>
Common/Utils.py | 32 ++++++++++++++++++++++++++++++++
1 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/Common/Utils.py b/Common/Utils.py
index 8d00a00..0170920 100644
--- a/Common/Utils.py
+++ b/Common/Utils.py
@@ -12,7 +12,10 @@ jzupka(a)redhat.com (Jiri Zupka)
import logging
import time
import re
+import os
import hashlib
+import tempfile
+from ExecCmd import exec_cmd
def die_when_parent_die():
try:
@@ -97,3 +100,32 @@ def md5sum(file_path, block_size=2**20):
md5.update(data)
return md5.hexdigest()
+
+def create_tar_archive(input_path, target_path, compression=False):
+ if compression:
+ args = "cfj"
+ else:
+ args = "cf"
+
+ if os.path.isdir(target_path):
+ target_path += "/%s.tar.bz" %
os.path.basename(input_file.rstrip("/"))
+
+ input_path = input_path.rstrip("/")
+ input_file = os.path.basename(input_path)
+ parent = os.path.dirname(input_path)
+
+ exec_cmd("cd \"%s\" && tar %s \"%s\"
\"%s\"" % \
+ (parent, args, target_path, input_file))
+
+ return target_path
+
+def dir_md5sum(dir_path):
+ tmp_file = tempfile.NamedTemporaryFile(delete=False)
+ tmp_file.close()
+
+ tar_filepath = create_tar_archive(dir_path, tmp_file.name)
+ md5_digest = md5sum(tar_filepath)
+
+ os.unlink(tar_filepath)
+
+ return md5_digest
Show replies by date