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 # \note No way to verify that extracted contents match archive, re-extract
72 def removeArchives (self):
73 archiveDir = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir)
74 if os.path.exists(archiveDir):
75 shutil.rmtree(archiveDir, ignore_errors=False)
77 def isArchiveUpToDate (self):
78 archiveFile = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, pkg.filename)
79 if os.path.exists(archiveFile):
80 return computeChecksum(readFile(archiveFile)) == self.checksum
84 def fetchAndVerifyArchive (self):
85 print "Fetching %s" % self.url
87 req = urllib2.urlopen(self.url)
89 checksum = computeChecksum(data)
90 dstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
92 if checksum != self.checksum:
93 raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (self.filename, self.checksum, checksum))
95 if not os.path.exists(os.path.dirname(dstPath)):
96 os.mkdir(os.path.dirname(dstPath))
98 writeFile(dstPath, data)
101 print "Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir)
103 srcPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
104 tmpPath = os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % self.baseDir)
105 dstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
106 archive = tarfile.open(srcPath)
108 if os.path.exists(tmpPath):
109 shutil.rmtree(tmpPath, ignore_errors=False)
113 archive.extractall(tmpPath)
116 extractedEntries = os.listdir(tmpPath)
117 if len(extractedEntries) != 1 or not os.path.isdir(os.path.join(tmpPath, extractedEntries[0])):
118 raise Exception("%s doesn't contain single top-level directory" % self.filename)
120 topLevelPath = os.path.join(tmpPath, extractedEntries[0])
122 if not os.path.exists(dstPath):
125 for entry in os.listdir(topLevelPath):
126 if os.path.exists(os.path.join(dstPath, entry)):
127 raise Exception("%s exists already" % entry)
129 shutil.move(os.path.join(topLevelPath, entry), dstPath)
131 shutil.rmtree(tmpPath, ignore_errors=True)
133 if self.postExtract != None:
134 self.postExtract(dstPath)
136 class GitRepo (Source):
137 def __init__(self, url, revision, baseDir, extractDir = "src"):
138 Source.__init__(self, baseDir, extractDir)
140 self.revision = revision
143 fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
145 if not os.path.exists(fullDstPath):
146 execute(["git", "clone", "--no-checkout", self.url, fullDstPath])
148 pushWorkingDir(fullDstPath)
150 execute(["git", "fetch", self.url, "+refs/heads/*:refs/remotes/origin/*"])
151 execute(["git", "checkout", self.revision])
155 def postExtractLibpng (path):
156 shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
157 os.path.join(path, "pnglibconf.h"))
161 "http://zlib.net/zlib-1.2.8.tar.gz",
163 "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
166 "http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz",
167 "libpng-1.6.17.tar.gz",
168 "a18233c99e1dc59a256180e6871d9305a42e91b3f98799b3ceb98e87e9ec5e31",
170 postExtract = postExtractLibpng),
172 "https://github.com/KhronosGroup/SPIRV-Tools.git",
173 "7ef6da7b7f9175da509b4d71a881c0a04e0b701b",
176 "https://github.com/KhronosGroup/glslang.git",
177 "f2d8a5c53fda69a7e19defbda7964f380da54ad1",
182 parser = argparse.ArgumentParser(description = "Fetch external sources")
183 parser.add_argument('--clean', dest='clean', action='store_true', default=False,
184 help='Remove sources instead of fetching')
185 return parser.parse_args()
187 if __name__ == "__main__":