forked from openstreamcaster/ffmpeg-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_utils.py
298 lines (250 loc) · 6.58 KB
/
build_utils.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Contains utiltt for building ffmpeg
"""
# pylint: disable=line-too-long
import logging
import platform
import re
import sys
import plumbum
logger = logging.getLogger(__name__)
def add_path(dir_path: str, take_priority: bool = True) -> None:
"""
Add directory to PATH env
Parameters
----------
dir_path: str
Directory path
take_priority: bool (default True)
If true, the directory will be prioritized
Returns
-------
None
"""
old_path = plumbum.local.env["PATH"]
plumbum.local.env["PATH"] = (
f"{dir_path}:{old_path}" if take_priority else f"{old_path}:{dir_path}"
)
def command_exists(cmd: str) -> bool:
"""
Check if commands exist
Parameters
----------
cmd: str
Command name
Returns
-------
bool
"""
try:
plumbum.local.which(cmd) # pylint: disable=pointless-statement
return True
except plumbum.commands.CommandNotFound:
return False
def run_fg(command: str, *args, silent: bool = False) -> bool:
"""
Run command at foreground
Parameters
----------
command: str
Command
*args
Command additional argument
silent: bool
Print foreground result or not
Returns
-------
bool
"""
try:
cmd = plumbum.local[command][args]
result = cmd.run() if silent else cmd & plumbum.TEE
return result
except plumbum.commands.CommandNotFound:
logger.exception("Command %s not found!", command)
return False
except plumbum.commands.ProcessExecutionError as err:
logger.exception(err)
return False
def require_commands(*commands) -> None:
"""
Check if required commands exist
Parameters
----------
*commands
List of commands
Returns
-------
None
"""
absent_commands_list = [
command for command in commands if not command_exists(command)
]
if absent_commands_list:
absent = ", ".join(absent_commands_list)
print(f"Required commands not found: {absent}")
sys.exit(1)
def path_fixer(src: str) -> str:
"""
Converting windows path to unix style path
Parameters
----------
src: str
Windows path
Returns
-------
str
Unix style path
Raises
------
NotImplementedError
Raised if path doesn't match any pattern
Information
-----------
The following code is the poor's man implementation of this:
https://stackoverflow.com/questions/41492504/how-to-get-native-windows-path-inside-msys-python
It's working, but maybe we should consider to switch to the full version
"""
if platform.system() != "Windows":
return src
# Handle Windows (native path)
path_search = re.search("([a-zA-Z]):/(.*)", src, re.IGNORECASE)
if path_search:
drive = path_search.group(1).lower()
path = path_search.group(2)
result = f"/{drive}/{path}"
return result
# Handle Windows (native path with backward slashes)
# Actually we can skip this, but it's useful for validation
path_search = re.search(r"([a-zA-Z]):\\(.*)", src, re.IGNORECASE)
if path_search:
drive = path_search.group(1).lower()
path = path_search.group(2).replace("\\", "/")
result = f"/{drive}/{path}"
return result
# Handle Windows (MSYS2, Git Bash, Cygwin, etc)
simulated_path_search = re.search("/([a-zA-Z])/(.*)", src, re.IGNORECASE)
if simulated_path_search:
drive = simulated_path_search.group(1).lower()
path = simulated_path_search.group(2)
result = f"/{drive}/{path}"
return result
# No sense in continuing without properly parsed path
raise NotImplementedError
def configure(prefix: str, *args, **kwargs) -> bool:
"""
Run configure
Parameters
----------
prefix : str
Build prefix
Returns
-------
bool
Return True if success
"""
configure_flags = ("./configure", f"--prefix={prefix}") + args
logger.debug("Configuring with configure. Command: %s", "".join(configure_flags))
if platform.system() == "Windows":
configure_flags = ("bash",) + configure_flags
run_fg("chmod", "+x", "./configure")
if not run_fg(*configure_flags, **kwargs):
sys.exit(1)
logger.debug("Configure with configure done!")
return True
def cmake(prefix: str, *args, **kwargs) -> bool:
"""
Run cmake
Parameters
----------
prefix : str
Build prefix
Returns
-------
bool
Return True if success
"""
cmake_flags = (
"cmake",
"-DCMAKE_BUILD_TYPE=Release",
f"-DCMAKE_INSTALL_PREFIX={prefix}",
f"-DCMAKE_PREFIX_PATH={prefix}",
) + args
logger.debug("Configuring with cmake. Command: %s", "".join(cmake_flags))
if platform.system() == "Windows":
cmake_flags += ("-G", "MSYS Makefiles")
if not run_fg(*cmake_flags, **kwargs):
sys.exit(1)
logger.debug("Configure with cmake done!")
return True
def make(threads: int, *args, **kwargs) -> None:
"""
Make command
Parameters
----------
threads: int
How many threads
*args
Will be passed to fg function
**kwargs
Will be passed to fg function
Returns
-------
None
"""
if not run_fg("make", f"-j{threads}", *args, **kwargs):
sys.exit(1)
def make_install(*args, **kwargs) -> None:
"""
Make install
Parameters
----------
*args
Will be passed to fg function
**kwargs
Will be passed to fg function
Returns
-------
None
"""
if not run_fg("make", "install", *args, **kwargs):
sys.exit(1)
def meson(prefix: str, *args, **kwargs) -> bool:
"""
Run meson
Parameters
----------
prefix : str
Build prefix
Returns
-------
bool
Return True if success
"""
meson_flags = (
"meson",
"setup",
f"--prefix={prefix}",
) + args
logger.debug("Configuring with meson. Command: %s", "".join(meson_flags))
if not run_fg(*meson_flags, **kwargs):
sys.exit(1)
logger.debug("Configure with meson done!")
return True
def ninja(threads: int, **kwargs) -> bool:
"""
Run ninja
Parameters
----------
threads : int
How many threads to run ninja
Returns
-------
bool
Return True if success
"""
if not run_fg("ninja", "-j", threads, **kwargs):
sys.exit(1)
if not run_fg("ninja", "install", **kwargs):
sys.exit(1)
return True