-
Notifications
You must be signed in to change notification settings - Fork 160
/
build.py
executable file
·225 lines (183 loc) · 8.8 KB
/
build.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import platform, subprocess
import os, sys
from shutil import copyfile, copytree, make_archive, rmtree
# To use this script:
#
# python3 build.py [args]
#
# Script Keyword Arguments:
#
# do_clean If present, the existing files will be cleaned before compiling.
# exclude_runner Excludes the WhiteboxTools Runner app from the build.
# zip Creates a zip file output in addition to the WBT folder
#
# Notes:
# You will need Rust installed before running the script. The output will be contained within a
# folder named 'WBT'. The WhiteboxTools Runner app often results in an error when compiling on
# Linux. This seems to be related to openssl libraries, which need to be set up correctly. If
# you are unable to figure out the set-up correctly and you do not need the Runner app, you
# would be advised to use the exclude_runner argument on linux.
#
# Example:
# python3 build.py do_clean exclude_runner zip
def build(do_clean=False, exclude_runner=False, create_zip_artifact=False):
# Set the directory variables
app_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(app_dir, 'WBT')
output_plugin_dir = os.path.join(app_dir, 'WBT/plugins')
plugins_dir = os.path.join(app_dir, 'whitebox-plugins/src')
target_dir = os.path.join(app_dir, 'target/release')
if platform.system() == "Linux":
target_dir = os.path.join(app_dir, 'target/x86_64-unknown-linux-musl/release')
if do_clean:
print("Cleaning old files...")
result = subprocess.run(['cargo', 'clean'], stdout=subprocess.PIPE)
if len(result.stdout) > 0:
print(result.stdout)
if os.path.exists(output_dir):
rmtree(output_dir)
# Create the Cargo.toml file
workspace_str = '[workspace]\nmembers = ["whitebox-common", "whitebox-lidar", "whitebox-plugins", "whitebox-raster", "whitebox-runner", "whitebox-tools-app", "whitebox-vector"]\n\n'
if exclude_runner:
# Exclude the runner if the second command line arg is set to True or if the platform is linux
workspace_str = '[workspace]\nmembers = ["whitebox-common", "whitebox-lidar", "whitebox-plugins", "whitebox-raster", "whitebox-tools-app", "whitebox-vector"]\n\n'
with open('Cargo.toml', "w") as cargo_file:
cargo_file.write(workspace_str)
cargo_file.write("""[profile.release]
incremental = true
strip = true""")
print("Compiling...")
# result = subprocess.run(['cargo', 'build', "--release"], stdout=subprocess.PIPE)
# if len(result.stdout) > 0:
# print(result.stdout)
if platform.system() != 'Linux':
result = subprocess.run(['cargo', 'build', "--release"], stdout=subprocess.PIPE)
if len(result.stdout) > 0:
print(result.stdout)
else:
print("Compiling for musl target...")
result = subprocess.run(['rustup', 'target', "add", "x86_64-unknown-linux-musl"], stdout=subprocess.PIPE)
if len(result.stdout) > 0:
print(result.stdout)
result = subprocess.run(['cargo', 'build', "--release", "--target=x86_64-unknown-linux-musl"], stdout=subprocess.PIPE)
if len(result.stdout) > 0:
print(result.stdout)
if not os.path.exists(output_plugin_dir):
os.makedirs(output_plugin_dir)
ext = ''
if platform.system() == 'Windows':
ext = '.exe'
# Copy the whitebox executable over
exe_file = os.path.join(target_dir, 'whitebox_tools') + ext
dst = os.path.join(output_dir, 'whitebox_tools') + ext
copyfile(exe_file, dst)
if platform.system() != 'Windows':
result = subprocess.run(['strip', dst], stdout=subprocess.PIPE)
os.system("chmod 755 " + dst) # grant executable permission
# Copy the ancillary files
src = os.path.join(app_dir, 'LICENSE.txt')
dst = os.path.join(output_dir, 'LICENSE.txt')
copyfile(src, dst)
src = os.path.join(app_dir, 'readme.txt')
dst = os.path.join(output_dir, 'readme.txt')
copyfile(src, dst)
src = os.path.join(app_dir, 'settings.json')
dst = os.path.join(output_dir, 'settings.json')
copyfile(src, dst)
src = os.path.join(app_dir, 'UserManual.txt')
dst = os.path.join(output_dir, 'UserManual.txt')
copyfile(src, dst)
# If the Runner app exists, copy it
exe_file = os.path.join(target_dir, 'whitebox_runner') + ext
if os.path.exists(exe_file):
dst = os.path.join(output_dir, 'whitebox_runner') + ext
copyfile(exe_file, dst)
if platform.system() != 'Windows':
result = subprocess.run(['strip', dst], stdout=subprocess.PIPE)
os.system("chmod 755 " + dst) # grant executable
src = os.path.join(app_dir, 'whitebox_tools.py')
dst = os.path.join(output_dir, 'whitebox_tools.py')
copyfile(src, dst)
os.system("chmod 755 " + dst) # grant executable permission
src = os.path.join(app_dir, 'img')
dst = os.path.join(output_dir, 'img')
copytree(src, dst)
plugins = os.listdir(plugins_dir)
for plugin in plugins:
if ".DS" not in plugin and "._" not in plugin:
print(f'Copying plugin: {plugin}')
# Copy the json file into the plugins directory
json_file = os.path.join(plugins_dir, plugin, plugin) + '.json'
dst = os.path.join(output_plugin_dir, plugin) + '.json'
copyfile(json_file, dst)
# Copy the executable file into the plugins directory
exe_file = os.path.join(target_dir, plugin) + ext
dst = os.path.join(output_plugin_dir, plugin) + ext
copyfile(exe_file, dst)
if platform.system() != 'Windows':
print("Stripping", plugin)
result = subprocess.run(['strip', dst], stdout=subprocess.PIPE)
# print(result)
os.system("chmod 755 " + dst) # grant executable permission
# Copy the register_license binary into the plugins folder if it is available
os.chdir(app_dir)
if os.path.exists('../GeneralToolsetExtension'):
# Copy the executable file into the plugins directory
exe_file = f"../GeneralToolsetExtension/register_license{ext}"
if os.path.exists(exe_file):
dst = os.path.join(output_plugin_dir, 'register_license') + ext
copyfile(exe_file, dst)
os.system("chmod 755 " + dst) # grant executable permission
else:
print("No register_license file found...")
else:
print("No directory containing the register_license file found...")
if create_zip_artifact:
# Make a zip of the WBT folder
print("Creating zip artifact...")
proc = "amd64"
if "arm" in platform.processor().lower() and "darwin" in platform.system().lower():
proc = "m_series"
pltfm = platform.system().lower()
if "windows" in pltfm:
pltfm = "win"
zip_name = f"WhiteboxTools_{pltfm}_{proc}"
# output_zip = os.path.join(app_dir, zip_name, "WBT")
copytree(output_dir, os.path.join(app_dir, zip_name, "WBT"), dirs_exist_ok=True)
# output_zip = os.path.join(app_dir, zip_name)
with open(os.path.join(os.path.join(app_dir, zip_name), 'readme.txt'), "w") as readme_file:
readme_file.write("""Instructions:
Copy the WBT folder and its entire contents to any location on your system. Configure your Whitebox
frontend, whether that is the QGIS or ArcGIS plugin, or the Python package, to point to this WBT
folder location. To access the functionality of WhiteboxTools without the need for a 3rd party
frontend, launch the WhiteboxTools Runner app (whitebox_runner), if it is contained within the WBT
folder.""")
# output_zip = os.path.join(app_dir, 'zip_file', zip_name)
make_archive(os.path.join(app_dir, 'zip_file', zip_name), 'zip', os.path.join(app_dir, zip_name))
# Delete the folder
if os.path.exists(os.path.join(app_dir, zip_name)):
rmtree(os.path.join(app_dir, zip_name))
print("Done!")
def main():
# Read in the script arguments
do_clean = False
if any("do_clean" in s for s in sys.argv):
matching = [s for s in sys.argv if "do_clean" in s]
if len(matching) > 0:
if "false" not in matching[0].lower():
do_clean = True
exclude_runner = False
if any("exclude_runner" in s for s in sys.argv):
matching = [s for s in sys.argv if "exclude_runner" in s]
if len(matching) > 0:
if "false" not in matching[0].lower():
exclude_runner = True
create_zip_artifact = False
if any("zip" in s for s in sys.argv):
matching = [s for s in sys.argv if "zip" in s]
if len(matching) > 0:
if "false" not in matching[0].lower():
create_zip_artifact = True
build(do_clean, exclude_runner, create_zip_artifact)
if __name__ == "__main__":
main()