Commit 14dc12cd by Stephen Fromm

Conditionally import hashlib (issue #471)

Import hashlib if it is there, otherwise import md5.  Adds method
file_digest that wraps the logic on which module to invoke.
parent 2b1b8d21
...@@ -23,12 +23,17 @@ except ImportError: ...@@ -23,12 +23,17 @@ except ImportError:
import simplejson as json import simplejson as json
import os import os
import os.path import os.path
import md5
import sys import sys
import shlex import shlex
import shutil import shutil
import syslog import syslog
import tempfile import tempfile
try:
import hashlib
HAVE_HASHLIB=True
except ImportError:
import md5
HAVE_HASHLIB=False
# Since hashlib is only available in 2.5 and onwards, this module # Since hashlib is only available in 2.5 and onwards, this module
# uses md5 which is available in 2.4. # uses md5 which is available in 2.4.
...@@ -59,6 +64,13 @@ def write_temp_file(data): ...@@ -59,6 +64,13 @@ def write_temp_file(data):
os.close(fd) os.close(fd)
return path return path
def file_digest(path):
if HAVE_HASHLIB:
digest = hashlib.md5(file(path).read()).hexdigest()
else:
digest = md5.new(file(path).read()).hexdigest()
return digest
# =========================================== # ===========================================
if len(sys.argv) == 1: if len(sys.argv) == 1:
...@@ -99,10 +111,10 @@ if not os.path.isdir(src): ...@@ -99,10 +111,10 @@ if not os.path.isdir(src):
fail_json(msg="Source (%s) is not a directory" % src) fail_json(msg="Source (%s) is not a directory" % src)
path = write_temp_file(assemble_from_fragments(src)) path = write_temp_file(assemble_from_fragments(src))
pathmd5 = md5.new(file(path).read()).hexdigest() pathmd5 = file_digest(path)
if os.path.exists(dest): if os.path.exists(dest):
destmd5 = md5.new(file(dest).read()).hexdigest() destmd5 = file_digest(dest)
if pathmd5 != destmd5: if pathmd5 != destmd5:
shutil.copy(path, dest) shutil.copy(path, dest)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment