changeset 16598:edf653f51521

added per-user cache for mx downloads
author Doug Simon <doug.simon@oracle.com>
date Mon, 28 Jul 2014 11:35:17 +0200
parents fcb186b03c8b
children 89be7c4db12c
files mxtool/mx.py
diffstat 1 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mxtool/mx.py	Fri Jul 25 17:38:00 2014 -0700
+++ b/mxtool/mx.py	Mon Jul 28 11:35:17 2014 +0200
@@ -489,8 +489,25 @@
 
 def _download_file_with_sha1(name, path, urls, sha1, sha1path, resolve, mustExist, sources=False):
     def _download_lib():
-        print 'Downloading ' + ("Sources " if sources else "") + name + ' from ' + str(urls)
-        download(path, urls)
+        cacheDir = get_env('MX_CACHE_DIR', join(_opts.user_home, '.mx', 'cache'))
+        if not exists(cacheDir):
+            os.makedirs(cacheDir)
+        base = basename(path)
+        cachePath = join(cacheDir, base + '_' + sha1)
+
+        if not exists(cachePath) or _sha1OfFile(cachePath) != sha1:
+            if exists(cachePath):
+                log('SHA1 of ' + cachePath + ' does not match expected value (' + sha1 + ') - re-downloading')
+            print 'Downloading ' + ("sources " if sources else "") + name + ' from ' + str(urls)
+            download(cachePath, urls)
+
+        d = dirname(path)
+        if d != '' and not exists(d):
+            os.makedirs(d)
+        if 'symlink' in dir(os):
+            os.symlink(cachePath, path)
+        else:
+            shutil.copy(cachePath, path)
 
     def _sha1Cached():
         with open(sha1path, 'r') as f:
@@ -498,9 +515,9 @@
 
     def _writeSha1Cached():
         with open(sha1path, 'w') as f:
-            f.write(_sha1OfFile())
-
-    def _sha1OfFile():
+            f.write(_sha1OfFile(path))
+
+    def _sha1OfFile(path):
         with open(path, 'rb') as f:
             d = hashlib.sha1()
             while True:
@@ -519,7 +536,7 @@
 
     if sha1 and sha1 != _sha1Cached():
         _download_lib()
-        if sha1 != _sha1OfFile():
+        if sha1 != _sha1OfFile(path):
             abort("SHA1 does not match for " + name + ". Broken download? SHA1 not updated in projects file?")
         _writeSha1Cached()