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 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "scripts"))
34 from build.common import *
36 EXTERNAL_DIR = os.path.realpath(os.path.normpath(os.path.dirname(__file__)))
38 def computeChecksum (data):
39 return hashlib.sha256(data).hexdigest()
42 def __init__(self, baseDir, extractDir):
43 self.baseDir = baseDir
44 self.extractDir = extractDir
47 fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
48 if os.path.exists(fullDstPath):
49 shutil.rmtree(fullDstPath, ignore_errors=False)
51 class SourcePackage (Source):
52 def __init__(self, url, filename, checksum, baseDir, extractDir = "src", postExtract=None):
53 Source.__init__(self, baseDir, extractDir)
55 self.filename = filename
56 self.checksum = checksum
57 self.archiveDir = "packages"
58 self.postExtract = postExtract
65 if not self.isArchiveUpToDate():
66 self.fetchAndVerifyArchive()
68 if self.getExtractedChecksum() != self.checksum:
71 self.storeExtractedChecksum(self.checksum)
73 def removeArchives (self):
74 archiveDir = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir)
75 if os.path.exists(archiveDir):
76 shutil.rmtree(archiveDir, ignore_errors=False)
78 def isArchiveUpToDate (self):
79 archiveFile = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, pkg.filename)
80 if os.path.exists(archiveFile):
81 return computeChecksum(readFile(archiveFile)) == self.checksum
85 def getExtractedChecksumFilePath (self):
86 return os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, "extracted")
88 def getExtractedChecksum (self):
89 extractedChecksumFile = self.getExtractedChecksumFilePath()
91 if os.path.exists(extractedChecksumFile):
92 return readFile(extractedChecksumFile)
96 def storeExtractedChecksum (self, checksum):
97 writeFile(self.getExtractedChecksumFilePath(), checksum)
99 def fetchAndVerifyArchive (self):
100 print "Fetching %s" % self.url
102 req = urllib2.urlopen(self.url)
104 checksum = computeChecksum(data)
105 dstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
107 if checksum != self.checksum:
108 raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (self.filename, self.checksum, checksum))
110 if not os.path.exists(os.path.dirname(dstPath)):
111 os.mkdir(os.path.dirname(dstPath))
113 writeFile(dstPath, data)
116 print "Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir)
118 srcPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
119 tmpPath = os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % self.baseDir)
120 dstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
121 archive = tarfile.open(srcPath)
123 if os.path.exists(tmpPath):
124 shutil.rmtree(tmpPath, ignore_errors=False)
128 archive.extractall(tmpPath)
131 extractedEntries = os.listdir(tmpPath)
132 if len(extractedEntries) != 1 or not os.path.isdir(os.path.join(tmpPath, extractedEntries[0])):
133 raise Exception("%s doesn't contain single top-level directory" % self.filename)
135 topLevelPath = os.path.join(tmpPath, extractedEntries[0])
137 if not os.path.exists(dstPath):
140 for entry in os.listdir(topLevelPath):
141 if os.path.exists(os.path.join(dstPath, entry)):
142 raise Exception("%s exists already" % entry)
144 shutil.move(os.path.join(topLevelPath, entry), dstPath)
146 shutil.rmtree(tmpPath, ignore_errors=True)
148 if self.postExtract != None:
149 self.postExtract(dstPath)
151 class GitRepo (Source):
152 def __init__(self, url, revision, baseDir, extractDir = "src"):
153 Source.__init__(self, baseDir, extractDir)
155 self.revision = revision
158 fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
160 if not os.path.exists(fullDstPath):
161 execute(["git", "clone", "--no-checkout", self.url, fullDstPath])
163 pushWorkingDir(fullDstPath)
165 execute(["git", "fetch", self.url, "+refs/heads/*:refs/remotes/origin/*"])
166 execute(["git", "checkout", self.revision])
170 def postExtractLibpng (path):
171 shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
172 os.path.join(path, "pnglibconf.h"))
176 "http://zlib.net/zlib-1.2.11.tar.gz",
177 "zlib-1.2.11.tar.gz",
178 "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
181 "http://prdownloads.sourceforge.net/libpng/libpng-1.6.27.tar.gz",
182 "libpng-1.6.27.tar.gz",
183 "c9d164ec247f426a525a7b89936694aefbc91fb7a50182b198898b8fc91174b4",
185 postExtract = postExtractLibpng),
187 "https://github.com/KhronosGroup/SPIRV-Tools.git",
188 "ab03b879cab5d09147c257035145f0ad36c45064",
191 "https://github.com/KhronosGroup/glslang.git",
192 "e3aa654c4b0c761b28d7864192ca8ceea6faf70a",
195 "https://github.com/KhronosGroup/SPIRV-Headers.git",
196 "bd47a9abaefac00be692eae677daed1b977e625c",
201 parser = argparse.ArgumentParser(description = "Fetch external sources")
202 parser.add_argument('--clean', dest='clean', action='store_true', default=False,
203 help='Remove sources instead of fetching')
204 return parser.parse_args()
206 if __name__ == "__main__":