1 # -*- coding: utf-8 -*- 2 3 #------------------------------------------------------------------------- 4 # drawElements Quality Program utilities 5 # -------------------------------------- 6 # 7 # Copyright 2015 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 23 import os 24 import urllib2 25 import hashlib 26 27 import registry 28 29 BASE_URL = "" 30 31 class RegistrySource: 32 def __init__(self, filename, revision, checksum): 33 self.filename = filename 34 self.revision = revision 35 self.checksum = checksum 36 37 def __hash__(self): 38 return hash((self.filename, self.revision, self.checksum)) 39 40 def __eq__(self, other): 41 return (self.filename, self.revision, self.checksum) == (other.filename, other.revision, other.checksum) 42 43 def getFilename (self): 44 return self.filename 45 46 def getCacheFilename (self): 47 return "r%d-%s" % (self.revision, self.filename) 48 49 def getChecksum (self): 50 return self.checksum 51 52 def getRevision (self): 53 return self.revision 54 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) 57 58 def computeChecksum (data): 59 return hashlib.sha256(data).hexdigest() 60 61 def fetchUrl (url): 62 req = urllib2.urlopen(url) 63 data = req.read() 64 return data 65 66 def fetchFile (dstPath, url, checksum): 67 def writeFile (filename, data): 68 f = open(filename, 'wb') 69 f.write(data) 70 f.close() 71 72 if not os.path.exists(os.path.dirname(dstPath)): 73 os.makedirs(os.path.dirname(dstPath)) 74 75 print "Fetching %s" % url 76 data = fetchUrl(url) 77 gotChecksum = computeChecksum(data) 78 79 if checksum != gotChecksum: 80 raise Exception("Checksum mismatch, exepected %s, got %s" % (checksum, gotChecksum)) 81 82 writeFile(dstPath, data) 83 84 def checkFile (filename, checksum): 85 def readFile (filename): 86 f = open(filename, 'rb') 87 data = f.read() 88 f.close() 89 return data 90 91 if os.path.exists(filename): 92 return computeChecksum(readFile(filename)) == checksum 93 else: 94 return False 95 96 g_registryCache = {} 97 98 def getRegistry (source): 99 global g_registryCache 100 101 if source in g_registryCache: 102 return g_registryCache[source] 103 104 cacheDir = os.path.join(os.path.dirname(__file__), "cache") 105 cachePath = os.path.join(cacheDir, source.getCacheFilename()) 106 107 if not checkFile(cachePath, source.checksum): 108 fetchFile(cachePath, source.getSourceUrl(), source.getChecksum()) 109 110 parsedReg = registry.parse(cachePath) 111 112 g_registryCache[source] = parsedReg 113 114 return parsedReg 115