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 #-------------------------------------------------------------------------
31 EXTERNAL_DIR = os.path.realpath(os.path.normpath(os.path.dirname(__file__)))
34 def __init__(self, url, filename, checksum, dstDir, postExtract=None):
36 self.filename = filename
37 self.checksum = checksum
39 self.postExtract = postExtract
41 def computeChecksum (data):
42 return hashlib.sha256(data).hexdigest()
45 srcPath = os.path.join(EXTERNAL_DIR, pkg.dstDir)
47 for entry in os.listdir(srcPath):
48 if entry == "CMakeLists.txt":
51 fullPath = os.path.join(srcPath, entry)
53 if os.path.isfile(fullPath):
55 elif os.path.isdir(fullPath):
56 shutil.rmtree(fullPath, ignore_errors=False)
59 print "Fetching %s" % pkg.url
61 req = urllib2.urlopen(pkg.url)
63 checksum = computeChecksum(data)
64 dstPath = os.path.join(EXTERNAL_DIR, pkg.filename)
66 if checksum != pkg.checksum:
67 raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (pkg.filename, pkg.checksum, checksum))
69 out = open(dstPath, 'wb')
74 print "Extracting %s to %s" % (pkg.filename, pkg.dstDir)
76 srcPath = os.path.join(EXTERNAL_DIR, pkg.filename)
77 tmpPath = os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % pkg.dstDir)
78 dstPath = os.path.join(EXTERNAL_DIR, pkg.dstDir)
79 archive = tarfile.open(srcPath)
81 if os.path.exists(tmpPath):
82 shutil.rmtree(tmpPath, ignore_errors=False)
86 archive.extractall(tmpPath)
89 extractedEntries = os.listdir(tmpPath)
90 if len(extractedEntries) != 1 or not os.path.isdir(os.path.join(tmpPath, extractedEntries[0])):
91 raise Exception("%s doesn't contain single top-level directory" % pkg.filename)
93 topLevelPath = os.path.join(tmpPath, extractedEntries[0])
95 for entry in os.listdir(topLevelPath):
96 if os.path.exists(os.path.join(dstPath, entry)):
97 print " skipping %s" % entry
100 shutil.move(os.path.join(topLevelPath, entry), dstPath)
102 shutil.rmtree(tmpPath, ignore_errors=True)
104 if pkg.postExtract != None:
105 pkg.postExtract(dstPath)
107 def postExtractLibpng (path):
108 shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
109 os.path.join(path, "pnglibconf.h"))
112 SourcePackage("http://zlib.net/zlib-1.2.8.tar.gz",
114 "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
116 SourcePackage("http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz",
117 "libpng-1.6.17.tar.gz",
118 "a18233c99e1dc59a256180e6871d9305a42e91b3f98799b3ceb98e87e9ec5e31",
120 postExtract = postExtractLibpng),
124 parser = argparse.ArgumentParser(description = "Fetch external sources")
125 parser.add_argument('--clean-only', dest='cleanOnly', action='store_true', default=False,
126 help='Clean only, do not fetch/extract')
127 parser.add_argument('--keep-archive', dest='keepArchive', action='store_true', default=False,
128 help='Keep archive after extracting')
129 return parser.parse_args()
131 if __name__ == "__main__":
143 if not args.keepArchive:
144 os.unlink(os.path.join(EXTERNAL_DIR, pkg.filename))