switch to hashlib.md5 or md5 instead of OS md5 commands

This commit is contained in:
Dave Hatton 2012-07-09 08:52:00 +01:00
commit 55694db7c3
5 changed files with 68 additions and 28 deletions

View file

@ -438,7 +438,7 @@ class Runner(object):
source = utils.template(source, inject, self.setup_cache)
source = utils.path_dwim(self.basedir, source)
local_md5 = utils.local_md5(source)
local_md5 = utils.md5(source)
if local_md5 is None:
result=dict(failed=True, msg="could not find src=%s" % source)
return ReturnData(host=conn.host, result=result)
@ -495,7 +495,7 @@ class Runner(object):
dest = dest.replace("//","/")
# compare old and new md5 for support of change hooks
local_md5 = utils.local_md5(dest)
local_md5 = utils.md5(dest)
remote_md5 = self._remote_md5(conn, tmp, source)
if remote_md5 == '0':
@ -508,7 +508,7 @@ class Runner(object):
# fetch the file and check for changes
conn.fetch_file(source, dest)
new_md5 = utils.local_md5(dest)
new_md5 = utils.md5(dest)
if new_md5 != remote_md5:
result = dict(failed=True, msg="md5 mismatch", md5sum=new_md5)
return ReturnData(host=conn.host, result=result)
@ -746,7 +746,8 @@ class Runner(object):
test = "[[ -r %s ]]" % path
md5s = [
"(%s && /usr/bin/md5sum %s 2>/dev/null)" % (test,path),
"(%s && /sbin/md5sum -q %s 2>/dev/null)" % (test,path)
"(%s && /sbin/md5sum -q %s 2>/dev/null)" % (test,path),
"(%s && /usr/bin/digest -a md5 -v %s 2>/dev/null)" % (test,path)
]
cmd = " || ".join(md5s)
cmd = "%s || (echo \"0 %s\")" % (cmd, path)

View file

@ -26,11 +26,19 @@ import jinja2
import yaml
import optparse
from operator import methodcaller
try:
import json
except ImportError:
import simplejson as json
try:
import hashlib
HAVE_HASHLIB=True
except ImportError:
import md5
HAVE_HASHLIB=False
from ansible import errors
import ansible.constants as C
@ -312,14 +320,14 @@ def parse_kv(args):
options[k]=v
return options
def local_md5(file):
''' compute local md5sum, return None if file is not present '''
cmd = "/usr/bin/md5sum %s 2> /dev/null || /sbin/md5 -q %s" % (file,file)
if not os.path.exists(file):
def md5(filename):
''' compute md5sum, return None if file is not present '''
if not os.path.exists(filename):
return None
if HAVE_HASHLIB:
return hashlib.md5(file(filename).read()).hexdigest()
else:
c = os.popen(cmd)
return c.read().split()[0]
return md5.new(file(filename).read()).hexdigest()
####################################################################