-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
118 lines (97 loc) · 4.54 KB
/
setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
"""
setup - Setup build environment
Copyright 2018-2019, Daniel Kristensen, Garmin Ltd, or its subsidiaries.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import hashlib
import os
import shutil
import subprocess
import sys
import urllib.request
import zipfile
TOP_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
BUILD_DIR = os.path.join(TOP_DIR, "build")
OUT_DIR = os.path.join(TOP_DIR, "_out")
OUT_SETUP_DIR = os.path.join(OUT_DIR, "setup")
GLOBIFEST_DIR = os.path.join(OUT_DIR, "globifest")
GLOBIFEST_ZIP = os.path.join(OUT_SETUP_DIR, "globifest.zip")
def extract_external_zipfile(zip_fn, replace_src=None, replace_dest=None):
print(" {}".format(zip_fn.replace(replace_src + "/", replace_dest + "/")))
with zipfile.ZipFile(zip_fn,"r") as zip_fh:
for file in zip_fh.infolist():
file.filename = file.filename.replace(replace_src + "/", replace_dest + "/")
zip_fh.extract(file, OUT_DIR)
def hash_file(file_name, algorithm):
"""Hash the given file with the given algorithm"""
BUF_SIZE = 4096
hash_fn = getattr(hashlib, algorithm)
calc = hash_fn()
data = True
with open(file_name, "rb") as file_to_hash:
while data:
data = file_to_hash.read(BUF_SIZE)
if data:
calc.update(data)
return calc.hexdigest()
def run_cmd():
# The only package that should be manually downloaded is Globifest, since it can manage
# dependencies better than this script.
zip_packages = [
dict(
name="globifest",
url="https://github.com/dpkristensen/globifest/archive/v0.0.0-alpha.zip",
# From PS: Get-FileHash -Algorithm SHA256 globifest.zip
sha256="C57350EBAD005053474B4AF32AD3E2F0B23EF4C27700CDA9655569602006042D",
folder="globifest-0.0.0-alpha"
)
]
os.makedirs(OUT_SETUP_DIR, exist_ok=True)
for p in zip_packages:
archive_fn = os.path.join(OUT_SETUP_DIR, "{}.zip".format(p["name"]))
out_dir = os.path.join(OUT_DIR, "{}".format(p["name"]))
# Check hash
if os.path.isfile(archive_fn):
hash_str = hash_file(archive_fn, "sha256")
if hash_str != p["sha256"].lower():
# Rename the file before deleting to avoid OS race conditions
tmp_file = archive_fn + ".old"
os.rename(archive_fn, archive_fn + ".old")
os.unlink(tmp_file)
if not os.path.isfile(archive_fn):
print("Downloading {} ({})".format(p["name"], p["url"]))
with urllib.request.urlopen(p["url"]) as response, open(archive_fn, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
if not os.path.isdir(out_dir):
print("Extracting {}".format(p["name"]))
extract_external_zipfile(archive_fn, p["folder"], p["name"])
os.chdir(GLOBIFEST_DIR)
os.system("build -i {} -o {}".format(
os.path.join(BUILD_DIR, "setup.gproj"),
OUT_DIR
))
os.chdir(TOP_DIR)
return 0
if __name__ == "__main__":
exit( run_cmd() )