From 19b43cd454800b9e0c4287a5cab66595375f651a Mon Sep 17 00:00:00 2001 From: Dave Hatton Date: Sun, 8 Jul 2012 17:39:57 +0100 Subject: [PATCH 1/2] further md5 fixes --- lib/ansible/runner/__init__.py | 3 ++- lib/ansible/utils.py | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 4253398b28..41d2b834c6 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -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) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index d54d00042b..0c72f8ae5f 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -314,7 +314,14 @@ def parse_kv(args): 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) + #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 else: From fb51805e1b6ef8a8582e1643691799b14d63fbc1 Mon Sep 17 00:00:00 2001 From: Dave Hatton Date: Sun, 8 Jul 2012 18:41:04 +0100 Subject: [PATCH 2/2] switch to using hashlib.md5 or md5 (if python 2.4) instead of a os call --- lib/ansible/utils.py | 31 ++++++++++++++++++------------- library/assemble | 23 +++++++++++++++-------- library/copy | 28 +++++++++++++++++++++++----- library/setup | 28 +++++++++++++++++++++++----- 4 files changed, 79 insertions(+), 31 deletions(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 0c72f8ae5f..c46ff6a342 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -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 #################################################################### diff --git a/library/assemble b/library/assemble index 23c50221b2..bf612cca65 100755 --- a/library/assemble +++ b/library/assemble @@ -28,6 +28,7 @@ import shlex import shutil import syslog import tempfile + try: import hashlib HAVE_HASHLIB=True @@ -64,12 +65,18 @@ def write_temp_file(data): os.close(fd) 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 +def local_md5(filename): + ''' compute local md5sum, return None if file is not present ''' + 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: + return None # =========================================== @@ -111,10 +118,10 @@ if not os.path.isdir(src): fail_json(msg="Source (%s) is not a directory" % src) path = write_temp_file(assemble_from_fragments(src)) -pathmd5 = file_digest(path) +pathmd5 = local_md5(path) if os.path.exists(dest): - destmd5 = file_digest(dest) + destmd5 = local_md5(dest) if pathmd5 != destmd5: shutil.copy(path, dest) diff --git a/library/copy b/library/copy index f06f779360..a6be0caa6d 100755 --- a/library/copy +++ b/library/copy @@ -24,6 +24,13 @@ import shlex import shutil import syslog +try: + import hashlib + HAVE_HASHLIB=True +except ImportError: + import md5 + HAVE_HASHLIB=False + # =========================================== # convert arguments of form a=b c=d # to a dictionary @@ -38,9 +45,20 @@ def exit_kv(rc=0, **kwargs): print dump_kv(kwargs) sys.exit(rc) -def md5_sum(f): - md5sum = os.popen("/usr/bin/md5sum %(file)s 2>/dev/null || /sbin/md5 -q %(file)s 2>/dev/null || /usr/bin/digest -a md5 -v %(file)s 2>/dev/null" % {"file": f}).read().split()[0] - return md5sum +def local_md5(filename): + ''' compute local md5sum, return None if file is not present ''' + 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: + return None + +# =========================================== if len(sys.argv) == 1: exit_kv(rc=1, failed=1, msg="incorrect number of arguments given") @@ -73,7 +91,7 @@ if not os.path.exists(src): exit_kv(rc=1, failed=1, msg="Source %s failed to transfer" % (src)) if not os.access(src, os.R_OK): exit_kv(rc=1, failed=1, msg="Source %s not readable" % (src)) -md5sum_src = md5_sum(src) +md5sum_src = local_md5(src) md5sum_dest = None # check if there is no dest file @@ -83,7 +101,7 @@ if os.path.exists(dest): exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (dest)) if not os.access(dest, os.R_OK): exit_kv(rc=1, failed=1, msg="Destination %s not readable" % (dest)) - md5sum_dest = md5_sum(dest) + md5sum_dest = local_md5(dest) else: if not os.access(os.path.dirname(dest), os.W_OK): exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (os.path.dirname(dest))) diff --git a/library/setup b/library/setup index 15f5b67fd8..c6edbfe481 100755 --- a/library/setup +++ b/library/setup @@ -33,6 +33,13 @@ import subprocess import traceback import syslog +try: + import hashlib + HAVE_HASHLIB=True +except ImportError: + import md5 + HAVE_HASHLIB=False + try: import selinux HAVE_SELINUX=True @@ -311,9 +318,20 @@ def ansible_facts(): get_service_facts(facts) return facts -def md5_sum(f): - md5sum = os.popen("/usr/bin/md5sum %(file)s 2>/dev/null || /sbin/md5 -q %(file)s 2>/dev/null || /usr/bin/digest -a md5 -v %(file)s 2>/dev/null" % {"file": f}).read().split()[0] - return md5sum +def local_md5(filename): + ''' compute local md5sum, return None if file is not present ''' + 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: + return None + +# =========================================== # load config & template variables @@ -351,7 +369,7 @@ md5sum = None if not os.path.exists(ansible_file): changed = True else: - md5sum = md5_sum(ansible_file) + md5sum = local_md5(ansible_file) # Get some basic facts in case facter or ohai are not installed for (k, v) in ansible_facts().items(): @@ -400,7 +418,7 @@ reformat = json.dumps(setup_options, sort_keys=True, indent=4) f.write(reformat) f.close() -md5sum2 = md5_sum(ansible_file) +md5sum2 = local_md5(ansible_file) if md5sum != md5sum2: changed = True