1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# drawElements Quality Program utilities 5# -------------------------------------- 6# 7# Copyright 2015-2017 The Android Open Source Project 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import os 24import urllib2 25import hashlib 26 27import registry 28 29BASE_URL = "" 30 31class RegistrySource: 32 def __init__(self, repository, filename, revision, checksum): 33 self.repository = repository 34 self.filename = filename 35 self.revision = revision 36 self.checksum = checksum 37 38 def __hash__(self): 39 return hash((self.repository, self.filename, self.revision, self.checksum)) 40 41 def __eq__(self, other): 42 return (self.repository, self.filename, self.revision, self.checksum) == (other.repository, other.filename, other.revision, other.checksum) 43 44 def getFilename (self): 45 return os.path.basename(self.filename) 46 47 def getCacheFilename (self): 48 return "r%s-%s" % (self.revision, self.getFilename()) 49 50 def getChecksum (self): 51 return self.checksum 52 53 def getRevision (self): 54 return self.revision 55 56 def getSourceUrl (self): 57 return "%s/%s/%s" % (self.repository, self.revision, self.filename) 58 59def computeChecksum (data): 60 return hashlib.sha256(data).hexdigest() 61 62def fetchUrl (url): 63 req = urllib2.urlopen(url) 64 data = req.read() 65 return data 66 67def fetchFile (dstPath, url, checksum): 68 def writeFile (filename, data): 69 f = open(filename, 'wb') 70 f.write(data) 71 f.close() 72 73 if not os.path.exists(os.path.dirname(dstPath)): 74 os.makedirs(os.path.dirname(dstPath)) 75 76 print "Fetching %s" % url 77 data = fetchUrl(url) 78 gotChecksum = computeChecksum(data) 79 80 if checksum != gotChecksum: 81 raise Exception("Checksum mismatch, expected %s, got %s" % (checksum, gotChecksum)) 82 83 writeFile(dstPath, data) 84 85def checkFile (filename, checksum): 86 def readFile (filename): 87 f = open(filename, 'rb') 88 data = f.read() 89 f.close() 90 return data 91 92 if os.path.exists(filename): 93 return computeChecksum(readFile(filename)) == checksum 94 else: 95 return False 96 97g_registryCache = {} 98 99def getRegistry (source): 100 global g_registryCache 101 102 if source in g_registryCache: 103 return g_registryCache[source] 104 105 cacheDir = os.path.join(os.path.dirname(__file__), "cache") 106 cachePath = os.path.join(cacheDir, source.getCacheFilename()) 107 108 if not checkFile(cachePath, source.checksum): 109 fetchFile(cachePath, source.getSourceUrl(), source.getChecksum()) 110 111 parsedReg = registry.parse(cachePath) 112 113 g_registryCache[source] = parsedReg 114 115 return parsedReg 116