comparison mxtool/mx.py @ 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 cc30bd72a19b
children 89be7c4db12c
comparison
equal deleted inserted replaced
16597:fcb186b03c8b 16598:edf653f51521
487 return join(prefix, path) 487 return join(prefix, path)
488 return path 488 return path
489 489
490 def _download_file_with_sha1(name, path, urls, sha1, sha1path, resolve, mustExist, sources=False): 490 def _download_file_with_sha1(name, path, urls, sha1, sha1path, resolve, mustExist, sources=False):
491 def _download_lib(): 491 def _download_lib():
492 print 'Downloading ' + ("Sources " if sources else "") + name + ' from ' + str(urls) 492 cacheDir = get_env('MX_CACHE_DIR', join(_opts.user_home, '.mx', 'cache'))
493 download(path, urls) 493 if not exists(cacheDir):
494 os.makedirs(cacheDir)
495 base = basename(path)
496 cachePath = join(cacheDir, base + '_' + sha1)
497
498 if not exists(cachePath) or _sha1OfFile(cachePath) != sha1:
499 if exists(cachePath):
500 log('SHA1 of ' + cachePath + ' does not match expected value (' + sha1 + ') - re-downloading')
501 print 'Downloading ' + ("sources " if sources else "") + name + ' from ' + str(urls)
502 download(cachePath, urls)
503
504 d = dirname(path)
505 if d != '' and not exists(d):
506 os.makedirs(d)
507 if 'symlink' in dir(os):
508 os.symlink(cachePath, path)
509 else:
510 shutil.copy(cachePath, path)
494 511
495 def _sha1Cached(): 512 def _sha1Cached():
496 with open(sha1path, 'r') as f: 513 with open(sha1path, 'r') as f:
497 return f.read()[0:40] 514 return f.read()[0:40]
498 515
499 def _writeSha1Cached(): 516 def _writeSha1Cached():
500 with open(sha1path, 'w') as f: 517 with open(sha1path, 'w') as f:
501 f.write(_sha1OfFile()) 518 f.write(_sha1OfFile(path))
502 519
503 def _sha1OfFile(): 520 def _sha1OfFile(path):
504 with open(path, 'rb') as f: 521 with open(path, 'rb') as f:
505 d = hashlib.sha1() 522 d = hashlib.sha1()
506 while True: 523 while True:
507 buf = f.read(4096) 524 buf = f.read(4096)
508 if not buf: 525 if not buf:
517 if sha1 and not exists(sha1path): 534 if sha1 and not exists(sha1path):
518 _writeSha1Cached() 535 _writeSha1Cached()
519 536
520 if sha1 and sha1 != _sha1Cached(): 537 if sha1 and sha1 != _sha1Cached():
521 _download_lib() 538 _download_lib()
522 if sha1 != _sha1OfFile(): 539 if sha1 != _sha1OfFile(path):
523 abort("SHA1 does not match for " + name + ". Broken download? SHA1 not updated in projects file?") 540 abort("SHA1 does not match for " + name + ". Broken download? SHA1 not updated in projects file?")
524 _writeSha1Cached() 541 _writeSha1Cached()
525 542
526 return path 543 return path
527 544