switch to using hashlib.md5 or md5 (if python 2.4) instead of a os call

This commit is contained in:
Dave Hatton 2012-07-08 18:41:04 +01:00
parent 19b43cd454
commit fb51805e1b
4 changed files with 79 additions and 31 deletions

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,21 +320,18 @@ def parse_kv(args):
options[k]=v
return options
def local_md5(file):
def local_md5(filename):
''' compute local md5sum, return None if file is not present '''
#was >>> cmd = "/usr/bin/md5sum %s 2> /dev/null || /sbin/md5 -q %s" % (file,file)
md5s = [
"(/usr/bin/md5sum %s 2>/dev/null)" % (file),
"(/sbin/md5sum -q %s 2>/dev/null)" % (file),
"(/usr/bin/digest -a md5 -v %s 2>/dev/null)" % (file)
]
cmd = " || ".join(md5s)
if not os.path.exists(file):
return None
if os.path.exists(filename):
md5val=None
if os.path.exists(filename):
if HAVE_HASHLIB:
md5val=hashlib.md5(file(filename).read()).hexdigest()
else:
md5val=md5.new(file(filename).read()).hexdigest()
return md5val
else:
c = os.popen(cmd)
return c.read().split()[0]
return None
####################################################################