-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
138 lines (111 loc) · 3.87 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
#!/bin/python3
from os import path, rename, environ, chdir, getcwd
import sys
import subprocess
from shutil import which
import json
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
return which(name) is not None
# Check that submodules are initialized
if not path.exists("libwallaby") or not path.exists("scratch-blocks"):
print("Submodules not initialized. Run 'git submodule update --init' to initialize them.")
exit(1)
# Check for CMake
if not is_tool("cmake"):
print("CMake is required to build libwallaby.")
exit(1)
if not is_tool('java'):
print('Java is required to build scratch-blocks.')
exit(1)
# Check for Node.js
if not is_tool("node"):
print("Node.js is required to build libwallaby.")
exit(1)
# Check node version
ret = subprocess.run(['node', '-v'], capture_output=True)
version_str = ret.stdout.decode()
node_major_version = int(version_str.strip()[1:3])
# Get additional CMake arguments
cmake_args = []
# Check LIBWALLABY_CMAKE_ARGS environment variable
if "LIBWALLABY_CMAKE_ARGS" in environ:
cmake_args = environ["LIBWALLABY_CMAKE_ARGS"].split(";")
cmake_args.append("-Dwith_camera=OFF")
cmake_args.append("-Dwith_graphics=OFF")
cmake_args.append("-Dwith_tello=OFF")
cmake_args.append("-Dwith_python_binding=OFF")
cmake_args.append("-Dwith_xml_binding=ON")
cmake_args.append("-DDUMMY=ON")
cmake_args.append("-Dwith_tests=OFF")
cmake_args.append("-Slibwallaby")
cmake_args.append("-Blibwallaby-build")
# Build libwallaby
print('Configuring libwallaby...')
ret = subprocess.run(["cmake"] + cmake_args)
if ret.returncode != 0:
print("Failed to configure libwallaby.")
exit(1)
print('Building libwallaby...')
ret = subprocess.run(["cmake", "--build", "libwallaby-build"])
if ret.returncode != 0:
print("Failed to build libwallaby.")
exit(1)
# Delete unnecessary blocks from scratch-blocks
print("Deleting unnecessary blocks from scratch-blocks...")
to_delete = [
'event.js',
'extensions.js',
'default_toolbox.js',
'looks.js',
'motion.js',
'sensing.js',
'sound.js'
]
python3 = 'python3'
if is_tool('python3.12'):
python3 = 'python3.12'
elif is_tool('python3.11'):
python3 = 'python3.11'
elif is_tool('python3.10'):
python3 = 'python3.10'
elif is_tool('python3.9'):
python3 = 'python3.9'
elif is_tool('python3.8'):
python3 = 'python3.8'
elif is_tool('python3.7'):
python3 = 'python3.7'
else:
print('Warning: Python 3.7+ could not be found. Using `python3`. This might not work.')
blocks_vertical_path = path.join("scratch-blocks", "blocks_vertical")
for file in to_delete:
file_path = path.join(blocks_vertical_path, file)
if not path.exists(file_path): continue
rename(file_path, path.join(blocks_vertical_path, file + ".old"))
# Blockify
print("Blockifying libwallaby...")
ret = subprocess.run([python3, "blockify.py", "libwallaby-build", "scratch-blocks/blocks_vertical"])
# Install and build scratch-blocks dependencies
scratch_blocks_node_modules_bin = path.join(getcwd(), "scratch-blocks", "node_modules", ".bin")
npm_env = {
'PATH': f"{scratch_blocks_node_modules_bin}:{environ['PATH']}",
}
if node_major_version >= 17:
npm_env['NODE_OPTIONS'] = '--openssl-legacy-provider'
# Run without scripts to skip the prepublish script
# We need to run prepublish steps separately so we can specifically use python3
print("Running 'npm install' for scratch-blocks...")
ret = subprocess.run(["npm", "install", "--ignore-scripts"], cwd="scratch-blocks")
if ret.returncode != 0:
print("Failed to run 'npm install' for scratch-blocks.")
exit(1)
print("Building scratch-blocks...")
ret = subprocess.run([python3, "build.py"], cwd="scratch-blocks", env=npm_env)
if ret.returncode != 0:
print("Failed to build scratch-blocks.")
exit(1)
print("Webpacking scratch-blocks...")
ret = subprocess.run(["webpack"], cwd="scratch-blocks", env=npm_env)
if ret.returncode != 0:
print("Failed to webpack scratch-blocks.")
exit(1)