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 #-------------------------------------------------------------------------
27 from . import registry
29 sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
31 from build.common import *
36 def __init__(self, repository, filename, revision, checksum):
37 self.repository = repository
38 self.filename = filename
39 self.revision = revision
40 self.checksum = checksum
43 return hash((self.repository, self.filename, self.revision, self.checksum))
45 def __eq__(self, other):
46 return (self.repository, self.filename, self.revision, self.checksum) == (other.repository, other.filename, other.revision, other.checksum)
48 def getFilename (self):
49 return os.path.basename(self.filename)
51 def getCacheFilename (self):
52 return "r%s-%s" % (self.revision, self.getFilename())
54 def getChecksum (self):
57 def getRevision (self):
61 return self.repository
63 def getRevision (self):
66 def getFilename (self):
69 def computeChecksum (data):
70 dataFiltered = data.replace('\r','')
71 hash = hashlib.sha256(dataFiltered.encode("utf-8")).hexdigest()
74 def makeSourceUrl (repository, revision, filename):
75 return "%s/%s/%s" % (repository, revision, filename)
77 def checkoutGit (repository, revision, fullDstPath):
78 if not os.path.exists(fullDstPath):
79 execute(["git", "clone", "--no-checkout", repository, fullDstPath])
81 pushWorkingDir(fullDstPath)
83 execute(["git", "fetch", repository, "+refs/heads/*:refs/remotes/origin/*"])
84 execute(["git", "checkout", revision])
88 def checkoutFile (repository, revision, filename, cacheDir):
89 if sys.version_info < (3, 0):
90 from urllib2 import urlopen
92 from urllib.request import urlopen
95 req = urlopen(makeSourceUrl(repository, revision, filename))
97 if sys.version_info >= (3, 0):
98 data = data.decode("utf-8")
100 fullDstPath = os.path.join(cacheDir, "git")
102 checkoutGit(repository, revision, fullDstPath)
103 f = open(os.path.join(fullDstPath, filename), "rt")
107 print("Unexpected error:", sys.exc_info()[0])
111 def fetchFile (dstPath, repository, revision, filename, checksum, cacheDir):
112 def writeFile (filename, data):
113 f = open(filename, 'wt')
117 if not os.path.exists(os.path.dirname(dstPath)):
118 os.makedirs(os.path.dirname(dstPath))
120 print("Fetching %s/%s@%s" % (repository, filename, revision))
121 data = checkoutFile(repository, revision, filename, cacheDir)
122 gotChecksum = computeChecksum(data)
124 if checksum != gotChecksum:
125 raise Exception("Checksum mismatch, expected %s, got %s" % (checksum, gotChecksum))
127 writeFile(dstPath, data)
129 def checkFile (filename, checksum):
130 def readFile (filename):
131 f = open(filename, 'rt')
136 if os.path.exists(filename):
137 return computeChecksum(readFile(filename)) == checksum
143 def getRegistry (source):
144 global g_registryCache
146 if source in g_registryCache:
147 return g_registryCache[source]
149 cacheDir = os.path.join(os.path.dirname(__file__), "cache")
150 cachePath = os.path.join(cacheDir, source.getCacheFilename())
152 if not checkFile(cachePath, source.checksum):
153 fetchFile(cachePath, source.getRepo(), source.getRevision(), source.getFilename(), source.getChecksum(), cacheDir)
155 parsedReg = registry.parse(cachePath)
157 g_registryCache[source] = parsedReg