1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015-2017 The Android Open Source Project
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 #-------------------------------------------------------------------------
32 def __init__(self, repository, filename, revision, checksum):
33 self.repository = repository
34 self.filename = filename
35 self.revision = revision
36 self.checksum = checksum
39 return hash((self.repository, self.filename, self.revision, self.checksum))
41 def __eq__(self, other):
42 return (self.repository, self.filename, self.revision, self.checksum) == (other.repository, other.filename, other.revision, other.checksum)
44 def getFilename (self):
45 return os.path.basename(self.filename)
47 def getCacheFilename (self):
48 return "r%s-%s" % (self.revision, self.getFilename())
50 def getChecksum (self):
53 def getRevision (self):
56 def getSourceUrl (self):
57 return "%s/%s/%s" % (self.repository, self.revision, self.filename)
59 def computeChecksum (data):
60 return hashlib.sha256(data).hexdigest()
63 req = urllib2.urlopen(url)
67 def fetchFile (dstPath, url, checksum):
68 def writeFile (filename, data):
69 f = open(filename, 'wb')
73 if not os.path.exists(os.path.dirname(dstPath)):
74 os.makedirs(os.path.dirname(dstPath))
76 print "Fetching %s" % url
78 gotChecksum = computeChecksum(data)
80 if checksum != gotChecksum:
81 raise Exception("Checksum mismatch, exepected %s, got %s" % (checksum, gotChecksum))
83 writeFile(dstPath, data)
85 def checkFile (filename, checksum):
86 def readFile (filename):
87 f = open(filename, 'rb')
92 if os.path.exists(filename):
93 return computeChecksum(readFile(filename)) == checksum
99 def getRegistry (source):
100 global g_registryCache
102 if source in g_registryCache:
103 return g_registryCache[source]
105 cacheDir = os.path.join(os.path.dirname(__file__), "cache")
106 cachePath = os.path.join(cacheDir, source.getCacheFilename())
108 if not checkFile(cachePath, source.checksum):
109 fetchFile(cachePath, source.getSourceUrl(), source.getChecksum())
111 parsedReg = registry.parse(cachePath)
113 g_registryCache[source] = parsedReg