1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 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, filename, revision, checksum):
33 self.filename = filename
34 self.revision = revision
35 self.checksum = checksum
38 return hash((self.filename, self.revision, self.checksum))
40 def __eq__(self, other):
41 return (self.filename, self.revision, self.checksum) == (other.filename, other.revision, other.checksum)
43 def getFilename (self):
46 def getCacheFilename (self):
47 return "r%d-%s" % (self.revision, self.filename)
49 def getChecksum (self):
52 def getRevision (self):
55 def getSourceUrl (self):
56 return "https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/%s?r=%d" % (self.filename, self.revision)
58 def computeChecksum (data):
59 return hashlib.sha256(data).hexdigest()
62 req = urllib2.urlopen(url)
66 def fetchFile (dstPath, url, checksum):
67 def writeFile (filename, data):
68 f = open(filename, 'wb')
72 if not os.path.exists(os.path.dirname(dstPath)):
73 os.makedirs(os.path.dirname(dstPath))
75 print "Fetching %s" % url
77 gotChecksum = computeChecksum(data)
79 if checksum != gotChecksum:
80 raise Exception("Checksum mismatch, exepected %s, got %s" % (checksum, gotChecksum))
82 writeFile(dstPath, data)
84 def checkFile (filename, checksum):
85 def readFile (filename):
86 f = open(filename, 'rb')
91 if os.path.exists(filename):
92 return computeChecksum(readFile(filename)) == checksum
98 def getRegistry (source):
99 global g_registryCache
101 if source in g_registryCache:
102 return g_registryCache[source]
104 cacheDir = os.path.join(os.path.dirname(__file__), "cache")
105 cachePath = os.path.join(cacheDir, source.getCacheFilename())
107 if not checkFile(cachePath, source.checksum):
108 fetchFile(cachePath, source.getSourceUrl(), source.getChecksum())
110 parsedReg = registry.parse(cachePath)
112 g_registryCache[source] = parsedReg