In order to cache large files located on remote repositories, we need to
make sure the downloaded files don't get corrupted. This patch adds a
function to get md5sums for files and an extension to unmap_url(),
unmap_url_cache() that will download the file located on a URL only if
it can't find the file on the cache directory or it detects a md5sum
mismatch (corrupted package file).
These functions were added to make it easier for other tests to cache
files and to allow the dacapo test to be accepted upstream.
Also, original unmap_url() now uses os.path.join() and a line filled
with whitespaces inside autotest utils was removed.
Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@1096 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/bin/autotest_utils.py b/client/bin/autotest_utils.py
index e171494..329a3b9 100755
--- a/client/bin/autotest_utils.py
+++ b/client/bin/autotest_utils.py
@@ -134,11 +134,47 @@
(after retrieving it)
"""
if is_url(src):
- dest = destdir + '/' + os.path.basename(src)
+ dest = os.path.join(destdir, os.path.basename(src))
get_file(src, dest)
return dest
else:
- return srcdir + '/' + src
+ return os.path.join(srcdir, src)
+
+
+def get_md5sum(file_path):
+ """Gets the md5sum of a file. You must provide a valid path to the file"""
+ if not os.path.isfile(file_path):
+ raise ValueError, 'invalid file %s to verify' % file_path
+ return system_output("md5sum " + file_path + " | awk '{print $1}'")
+
+
+def unmap_url_cache(cachedir, url, expected_md5):
+ """\
+ Downloads a file from a URL to a cache directory. If the file is already
+ at the expected position and has the expected md5 number, let's not
+ download it again.
+ """
+ # Let's convert cachedir to a canonical path, if it's not already
+ cachedir = os.path.realpath(cachedir)
+ if not os.path.isdir(cachedir):
+ try:
+ system('mkdir -p ' + cachedir)
+ except:
+ raise ValueError, 'Could not create cache directory %s' % cachedir
+ file_from_url = os.path.basename(url)
+ file_local_path = os.path.join(cachedir, file_from_url)
+ if os.path.isfile(file_local_path):
+ file_md5 = get_md5sum(file_local_path)
+ if file_md5 == expected_md5:
+ # File is already at the expected position and ready to go
+ src = file_from_url
+ else:
+ # Let's download the package again, it's corrupted...
+ src = url
+ else:
+ # File is not there, let's download it
+ src = url
+ return unmap_url(cachedir, src, cachedir)
def basename(path):
@@ -460,7 +496,7 @@
if kv_tmp[0].split('.') < ver.split('.'):
raise TestError("Kernel is too old (%s). Kernel > %s is needed." % \
(kernel_ver, ver))
-
+
def read_one_line(filename):
return open(filename, 'r').readline().strip()