comparison mxtool/mx.py @ 16599:89be7c4db12c

made sha1 signatures mandatory for libraries; made libraries for all downloading performed by commands in mx_graal
author Doug Simon <doug.simon@oracle.com>
date Mon, 28 Jul 2014 13:55:33 +0200
parents edf653f51521
children addc0564e5b5
comparison
equal deleted inserted replaced
16598:edf653f51521 16599:89be7c4db12c
485 """ 485 """
486 if not isabs(path): 486 if not isabs(path):
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 sha1OfFile(path):
491 with open(path, 'rb') as f:
492 d = hashlib.sha1()
493 while True:
494 buf = f.read(4096)
495 if not buf:
496 break
497 d.update(buf)
498 return d.hexdigest()
499
500 def download_file_with_sha1(name, path, urls, sha1, sha1path, resolve, mustExist, sources=False):
491 def _download_lib(): 501 def _download_lib():
492 cacheDir = get_env('MX_CACHE_DIR', join(_opts.user_home, '.mx', 'cache')) 502 cacheDir = get_env('MX_CACHE_DIR', join(_opts.user_home, '.mx', 'cache'))
493 if not exists(cacheDir): 503 if not exists(cacheDir):
494 os.makedirs(cacheDir) 504 os.makedirs(cacheDir)
495 base = basename(path) 505 base = basename(path)
496 cachePath = join(cacheDir, base + '_' + sha1) 506 cachePath = join(cacheDir, base + '_' + sha1)
497 507
498 if not exists(cachePath) or _sha1OfFile(cachePath) != sha1: 508 if not exists(cachePath) or sha1OfFile(cachePath) != sha1:
499 if exists(cachePath): 509 if exists(cachePath):
500 log('SHA1 of ' + cachePath + ' does not match expected value (' + sha1 + ') - re-downloading') 510 log('SHA1 of ' + cachePath + ' does not match expected value (' + sha1 + ') - re-downloading')
501 print 'Downloading ' + ("sources " if sources else "") + name + ' from ' + str(urls) 511 print 'Downloading ' + ("sources " if sources else "") + name + ' from ' + str(urls)
502 download(cachePath, urls) 512 download(cachePath, urls)
503 513
504 d = dirname(path) 514 d = dirname(path)
505 if d != '' and not exists(d): 515 if d != '' and not exists(d):
506 os.makedirs(d) 516 os.makedirs(d)
507 if 'symlink' in dir(os): 517 if 'symlink' in dir(os):
518 if exists(path):
519 os.unlink(path)
508 os.symlink(cachePath, path) 520 os.symlink(cachePath, path)
509 else: 521 else:
510 shutil.copy(cachePath, path) 522 shutil.copy(cachePath, path)
511 523
512 def _sha1Cached(): 524 def _sha1Cached():
513 with open(sha1path, 'r') as f: 525 with open(sha1path, 'r') as f:
514 return f.read()[0:40] 526 return f.read()[0:40]
515 527
516 def _writeSha1Cached(): 528 def _writeSha1Cached():
517 with open(sha1path, 'w') as f: 529 with open(sha1path, 'w') as f:
518 f.write(_sha1OfFile(path)) 530 f.write(sha1OfFile(path))
519
520 def _sha1OfFile(path):
521 with open(path, 'rb') as f:
522 d = hashlib.sha1()
523 while True:
524 buf = f.read(4096)
525 if not buf:
526 break
527 d.update(buf)
528 return d.hexdigest()
529 531
530 if resolve and mustExist and not exists(path): 532 if resolve and mustExist and not exists(path):
531 assert not len(urls) == 0, 'cannot find required library ' + name + ' ' + path 533 assert not len(urls) == 0, 'cannot find required library ' + name + ' ' + path
532 _download_lib() 534 _download_lib()
533 535
534 if sha1 and not exists(sha1path): 536 if exists(path):
535 _writeSha1Cached() 537 if sha1 and not exists(sha1path):
536 538 _writeSha1Cached()
537 if sha1 and sha1 != _sha1Cached(): 539
538 _download_lib() 540 if sha1 and sha1 != _sha1Cached():
539 if sha1 != _sha1OfFile(path): 541 _download_lib()
540 abort("SHA1 does not match for " + name + ". Broken download? SHA1 not updated in projects file?") 542 if sha1 != sha1OfFile(path):
541 _writeSha1Cached() 543 abort("SHA1 does not match for " + name + ". Broken download? SHA1 not updated in projects file?")
544 _writeSha1Cached()
542 545
543 return path 546 return path
544 547
545 class BaseLibrary(Dependency): 548 class BaseLibrary(Dependency):
546 def __init__(self, suite, name, optional): 549 def __init__(self, suite, name, optional):
602 self.path = path.replace('/', os.sep) 605 self.path = path.replace('/', os.sep)
603 self.urls = urls 606 self.urls = urls
604 self.sha1 = sha1 607 self.sha1 = sha1
605 self.sourcePath = sourcePath 608 self.sourcePath = sourcePath
606 self.sourceUrls = sourceUrls 609 self.sourceUrls = sourceUrls
610 if sourcePath == path:
611 assert sourceSha1 is None or sourceSha1 == sha1
612 sourceSha1 = sha1
607 self.sourceSha1 = sourceSha1 613 self.sourceSha1 = sourceSha1
608 self.deps = deps 614 self.deps = deps
609 abspath = _make_absolute(self.path, self.suite.dir) 615 abspath = _make_absolute(path, self.suite.dir)
610 if not optional and not exists(abspath): 616 if not optional and not exists(abspath):
611 if not len(urls): 617 if not len(urls):
612 abort('Non-optional library {} must either exist at {} or specify one or more URLs from which it can be retrieved'.format(name, abspath)) 618 abort('Non-optional library {} must either exist at {} or specify one or more URLs from which it can be retrieved'.format(name, abspath))
619
620 def _checkSha1PropertyCondition(propName, cond, inputPath):
621 if not cond:
622 absInputPath = _make_absolute(inputPath, self.suite.dir)
623 if exists(absInputPath):
624 abort('Missing "{}" property for library {}. Add the following line to projects file:\nlibrary@{}@{}={}'.format(propName, name, name, propName, sha1OfFile(absInputPath)))
625 abort('Missing "{}" property for library {}'.format(propName, name))
626
627 _checkSha1PropertyCondition('sha1', sha1, path)
628 _checkSha1PropertyCondition('sourceSha1', not sourcePath or sourceSha1, sourcePath)
629
613 for url in urls: 630 for url in urls:
614 if url.endswith('/') != self.path.endswith(os.sep): 631 if url.endswith('/') != self.path.endswith(os.sep):
615 abort('Path for dependency directory must have a URL ending with "/": path=' + self.path + ' url=' + url) 632 abort('Path for dependency directory must have a URL ending with "/": path=' + self.path + ' url=' + url)
616 633
617 def __eq__(self, other): 634 def __eq__(self, other):
629 646
630 includedInJDK = getattr(self, 'includedInJDK', None) 647 includedInJDK = getattr(self, 'includedInJDK', None)
631 if includedInJDK and java().javaCompliance >= JavaCompliance(includedInJDK): 648 if includedInJDK and java().javaCompliance >= JavaCompliance(includedInJDK):
632 return None 649 return None
633 650
634 return _download_file_with_sha1(self.name, path, self.urls, self.sha1, sha1path, resolve, not self.optional) 651 return download_file_with_sha1(self.name, path, self.urls, self.sha1, sha1path, resolve, not self.optional)
635 652
636 def get_source_path(self, resolve): 653 def get_source_path(self, resolve):
637 if self.sourcePath is None: 654 if self.sourcePath is None:
638 return None 655 return None
639 path = _make_absolute(self.sourcePath, self.suite.dir) 656 path = _make_absolute(self.sourcePath, self.suite.dir)
640 sha1path = path + '.sha1' 657 sha1path = path + '.sha1'
641 658
642 return _download_file_with_sha1(self.name, path, self.sourceUrls, self.sourceSha1, sha1path, resolve, len(self.sourceUrls) != 0, sources=True) 659 return download_file_with_sha1(self.name, path, self.sourceUrls, self.sourceSha1, sha1path, resolve, len(self.sourceUrls) != 0, sources=True)
643 660
644 def append_to_classpath(self, cp, resolve): 661 def append_to_classpath(self, cp, resolve):
645 path = self.get_path(resolve) 662 path = self.get_path(resolve)
646 if path and (exists(path) or not resolve): 663 if path and (exists(path) or not resolve):
647 cp.append(path) 664 cp.append(path)