am b1584184: am 77e72636: am ea702448: am d38c14ba: am 75e63782: am 523bdab6: am...
[platform/upstream/VK-GL-CTS.git] / external / fetch_sources.py
1 # -*- coding: utf-8 -*-
2
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
6 #
7 # Copyright 2015 The Android Open Source Project
8 #
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
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 #-------------------------------------------------------------------------
22
23 import os
24 import sys
25 import shutil
26 import tarfile
27 import urllib2
28 import hashlib
29 import argparse
30
31 EXTERNAL_DIR    = os.path.realpath(os.path.normpath(os.path.dirname(__file__)))
32
33 class SourcePackage:
34         def __init__(self, url, filename, checksum, dstDir, postExtract=None):
35                 self.url                        = url
36                 self.filename           = filename
37                 self.checksum           = checksum
38                 self.dstDir                     = dstDir
39                 self.postExtract        = postExtract
40
41 def computeChecksum (data):
42         return hashlib.sha256(data).hexdigest()
43
44 def clean (pkg):
45         srcPath = os.path.join(EXTERNAL_DIR, pkg.dstDir)
46
47         for entry in os.listdir(srcPath):
48                 if entry == "CMakeLists.txt":
49                         continue
50
51                 fullPath = os.path.join(srcPath, entry)
52
53                 if os.path.isfile(fullPath):
54                         os.unlink(fullPath)
55                 elif os.path.isdir(fullPath):
56                         shutil.rmtree(fullPath, ignore_errors=False)
57
58 def fetch (pkg):
59         print "Fetching %s" % pkg.url
60
61         req                     = urllib2.urlopen(pkg.url)
62         data            = req.read()
63         checksum        = computeChecksum(data)
64         dstPath         = os.path.join(EXTERNAL_DIR, pkg.filename)
65
66         if checksum != pkg.checksum:
67                 raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (pkg.filename, pkg.checksum, checksum))
68
69         out = open(dstPath, 'wb')
70         out.write(data)
71         out.close()
72
73 def extract (pkg):
74         print "Extracting %s to %s" % (pkg.filename, pkg.dstDir)
75
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)
80
81         if os.path.exists(tmpPath):
82                 shutil.rmtree(tmpPath, ignore_errors=False)
83
84         os.mkdir(tmpPath)
85
86         archive.extractall(tmpPath)
87         archive.close()
88
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)
92
93         topLevelPath = os.path.join(tmpPath, extractedEntries[0])
94
95         for entry in os.listdir(topLevelPath):
96                 if os.path.exists(os.path.join(dstPath, entry)):
97                         print "  skipping %s" % entry
98                         continue
99
100                 shutil.move(os.path.join(topLevelPath, entry), dstPath)
101
102         shutil.rmtree(tmpPath, ignore_errors=True)
103
104         if pkg.postExtract != None:
105                 pkg.postExtract(dstPath)
106
107 def postExtractLibpng (path):
108         shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
109                                 os.path.join(path, "pnglibconf.h"))
110
111 PACKAGES = [
112         SourcePackage("http://zlib.net/zlib-1.2.8.tar.gz",
113                                   "zlib-1.2.8.tar.gz",
114                                   "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
115                                   "zlib"),
116         SourcePackage("http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz",
117                                   "libpng-1.6.17.tar.gz",
118                                   "a18233c99e1dc59a256180e6871d9305a42e91b3f98799b3ceb98e87e9ec5e31",
119                                   "libpng",
120                                   postExtract = postExtractLibpng),
121 ]
122
123 def parseArgs ():
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()
130
131 if __name__ == "__main__":
132         args = parseArgs()
133
134         for pkg in PACKAGES:
135                 clean(pkg)
136
137                 if args.cleanOnly:
138                         continue
139
140                 fetch(pkg)
141                 extract(pkg)
142
143                 if not args.keepArchive:
144                         os.unlink(os.path.join(EXTERNAL_DIR, pkg.filename))