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 #-------------------------------------------------------------------------
30 sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
32 from build.common import *
37 def __init__(self, repository, filename, revision, checksum):
38 self.repository = repository
39 self.filename = filename
40 self.revision = revision
41 self.checksum = checksum
44 return hash((self.repository, self.filename, self.revision, self.checksum))
46 def __eq__(self, other):
47 return (self.repository, self.filename, self.revision, self.checksum) == (other.repository, other.filename, other.revision, other.checksum)
49 def getFilename (self):
50 return os.path.basename(self.filename)
52 def getCacheFilename (self):
53 return "r%s-%s" % (self.revision, self.getFilename())
55 def getChecksum (self):
58 def getRevision (self):
62 return self.repository
64 def getRevision (self):
67 def getFilename (self):
70 def computeChecksum (data):
71 return hashlib.sha256(data).hexdigest()
73 def makeSourceUrl (repository, revision, filename):
74 return "%s/%s/%s" % (repository, revision, filename)
76 def checkoutGit (repository, revision, fullDstPath):
77 if not os.path.exists(fullDstPath):
78 execute(["git", "clone", "--no-checkout", repository, fullDstPath])
80 pushWorkingDir(fullDstPath)
82 execute(["git", "fetch", repository, "+refs/heads/*:refs/remotes/origin/*"])
83 execute(["git", "checkout", revision])
87 def checkoutFile (repository, revision, filename, cacheDir):
89 req = urllib2.urlopen(makeSourceUrl(repository, revision, filename))
92 fullDstPath = os.path.join(cacheDir, "git")
94 checkoutGit(repository, revision, fullDstPath)
95 f = open(os.path.join(fullDstPath, filename), "r")
99 print "Unexpected error:", sys.exc_info()[0]
103 def fetchFile (dstPath, repository, revision, filename, checksum, cacheDir):
104 def writeFile (filename, data):
105 f = open(filename, 'wb')
109 if not os.path.exists(os.path.dirname(dstPath)):
110 os.makedirs(os.path.dirname(dstPath))
112 print "Fetching %s/%s@%s" % (repository, filename, revision)
113 data = checkoutFile(repository, revision, filename, cacheDir)
114 gotChecksum = computeChecksum(data)
116 if checksum != gotChecksum:
117 raise Exception("Checksum mismatch, expected %s, got %s" % (checksum, gotChecksum))
119 writeFile(dstPath, data)
121 def checkFile (filename, checksum):
122 def readFile (filename):
123 f = open(filename, 'rb')
128 if os.path.exists(filename):
129 return computeChecksum(readFile(filename)) == checksum
135 def getRegistry (source):
136 global g_registryCache
138 if source in g_registryCache:
139 return g_registryCache[source]
141 cacheDir = os.path.join(os.path.dirname(__file__), "cache")
142 cachePath = os.path.join(cacheDir, source.getCacheFilename())
144 if not checkFile(cachePath, source.checksum):
145 fetchFile(cachePath, source.getRepo(), source.getRevision(), source.getFilename(), source.getChecksum(), cacheDir)
147 parsedReg = registry.parse(cachePath)
149 g_registryCache[source] = parsedReg