4
4
import platform
5
5
import os
6
6
import subprocess
7
+ import shutil
7
8
from pythonforandroid .logger import info , warning , error
8
9
9
10
10
11
class Prerequisite (object ):
11
12
name = "Default"
12
- mandatory = True
13
- darwin_installer_is_supported = False
14
- linux_installer_is_supported = False
13
+ mandatory = dict (linux = False , darwin = False )
14
+ installer_is_supported = dict (linux = False , darwin = False )
15
15
16
16
def is_valid (self ):
17
17
if self .checker ():
18
18
info (f"Prerequisite { self .name } is met" )
19
19
return (True , "" )
20
- elif not self .mandatory :
20
+ elif not self .mandatory [ sys . platform ] :
21
21
warning (
22
22
f"Prerequisite { self .name } is not met, but is marked as non-mandatory"
23
23
)
@@ -73,10 +73,7 @@ def show_helper(self):
73
73
raise Exception ("Unsupported platform" )
74
74
75
75
def install_is_supported (self ):
76
- if sys .platform == "darwin" :
77
- return self .darwin_installer_is_supported
78
- elif sys .platform == "linux" :
79
- return self .linux_installer_is_supported
76
+ return self .installer_is_supported [sys .platform ]
80
77
81
78
def linux_checker (self ):
82
79
raise Exception (f"Unsupported prerequisite check on linux for { self .name } " )
@@ -96,11 +93,42 @@ def darwin_helper(self):
96
93
def linux_helper (self ):
97
94
info (f"No helper available for prerequisite: { self .name } on linux" )
98
95
96
+ def _darwin_get_brew_formula_location_prefix (self , formula , installed = False ):
97
+ opts = ["--installed" ] if installed else []
98
+ p = subprocess .Popen (
99
+ ["brew" , "--prefix" , formula , * opts ],
100
+ stdout = subprocess .PIPE ,
101
+ stderr = subprocess .PIPE ,
102
+ )
103
+ _stdout_res , _stderr_res = p .communicate ()
104
+
105
+ if p .returncode != 0 :
106
+ error (_stderr_res .decode ("utf-8" ).strip ())
107
+ return None
108
+ else :
109
+ return _stdout_res .decode ("utf-8" ).strip ()
110
+
111
+
112
+ class HomebrewPrerequisite (Prerequisite ):
113
+ name = "homebrew"
114
+ mandatory = dict (linux = False , darwin = True )
115
+ installer_is_supported = dict (linux = False , darwin = False )
116
+
117
+ def darwin_checker (self ):
118
+ return shutil .which ("brew" ) is not None
119
+
120
+ def darwin_helper (self ):
121
+ info (
122
+ "Installer for homebrew is not yet supported on macOS,"
123
+ "the nice news is that the installation process is easy!"
124
+ "See: https://brew.sh for further instructions."
125
+ )
126
+
99
127
100
128
class JDKPrerequisite (Prerequisite ):
101
129
name = "JDK"
102
- mandatory = True
103
- darwin_installer_is_supported = True
130
+ mandatory = dict ( linux = False , darwin = True )
131
+ installer_is_supported = dict ( linux = False , darwin = True )
104
132
min_supported_version = 11
105
133
106
134
def darwin_checker (self ):
@@ -216,13 +244,123 @@ def darwin_installer(self):
216
244
os .environ ["JAVA_HOME" ] = jdk_path
217
245
218
246
219
- def check_and_install_default_prerequisites ():
220
- DEFAULT_PREREQUISITES = dict (darwin = [JDKPrerequisite ()], linux = [], all_platforms = [])
247
+ class OpenSSLPrerequisite (Prerequisite ):
248
+ name = "openssl@1.1"
249
+ mandatory = dict (linux = False , darwin = True )
250
+ installer_is_supported = dict (linux = False , darwin = True )
251
+
252
+ def darwin_checker (self ):
253
+ return (
254
+ self ._darwin_get_brew_formula_location_prefix ("openssl@1.1" , installed = True )
255
+ is not None
256
+ )
257
+
258
+ def darwin_installer (self ):
259
+ info ("Installing OpenSSL ..." )
260
+ subprocess .check_output (["brew" , "install" , "openssl@1.1" ])
261
+
262
+
263
+ class AutoconfPrerequisite (Prerequisite ):
264
+ name = "autoconf"
265
+ mandatory = dict (linux = False , darwin = True )
266
+ installer_is_supported = dict (linux = False , darwin = True )
267
+
268
+ def darwin_checker (self ):
269
+ return (
270
+ self ._darwin_get_brew_formula_location_prefix ("autoconf" , installed = True )
271
+ is not None
272
+ )
273
+
274
+ def darwin_installer (self ):
275
+ info ("Installing Autoconf ..." )
276
+ subprocess .check_output (["brew" , "install" , "autoconf" ])
277
+
278
+
279
+ class AutomakePrerequisite (Prerequisite ):
280
+ name = "automake"
281
+ mandatory = dict (linux = False , darwin = True )
282
+ installer_is_supported = dict (linux = False , darwin = True )
221
283
222
- required_prerequisites = (
223
- DEFAULT_PREREQUISITES ["all_platforms" ] + DEFAULT_PREREQUISITES [sys .platform ]
284
+ def darwin_checker (self ):
285
+ return (
286
+ self ._darwin_get_brew_formula_location_prefix ("automake" , installed = True )
287
+ is not None
288
+ )
289
+
290
+ def darwin_installer (self ):
291
+ info ("Installing Automake ..." )
292
+ subprocess .check_output (["brew" , "install" , "automake" ])
293
+
294
+
295
+ class LibtoolPrerequisite (Prerequisite ):
296
+ name = "libtool"
297
+ mandatory = dict (linux = False , darwin = True )
298
+ installer_is_supported = dict (linux = False , darwin = True )
299
+
300
+ def darwin_checker (self ):
301
+ return (
302
+ self ._darwin_get_brew_formula_location_prefix ("libtool" , installed = True )
303
+ is not None
304
+ )
305
+
306
+ def darwin_installer (self ):
307
+ info ("Installing Libtool ..." )
308
+ subprocess .check_output (["brew" , "install" , "libtool" ])
309
+
310
+
311
+ class PkgConfigPrerequisite (Prerequisite ):
312
+ name = "pkg-config"
313
+ mandatory = dict (linux = False , darwin = True )
314
+ installer_is_supported = dict (linux = False , darwin = True )
315
+
316
+ def darwin_checker (self ):
317
+ return (
318
+ self ._darwin_get_brew_formula_location_prefix ("pkg-config" , installed = True )
319
+ is not None
320
+ )
321
+
322
+ def darwin_installer (self ):
323
+ info ("Installing Pkg-Config ..." )
324
+ subprocess .check_output (["brew" , "install" , "pkg-config" ])
325
+
326
+
327
+ class CmakePrerequisite (Prerequisite ):
328
+ name = "cmake"
329
+ mandatory = dict (linux = False , darwin = True )
330
+ installer_is_supported = dict (linux = False , darwin = True )
331
+
332
+ def darwin_checker (self ):
333
+ return (
334
+ self ._darwin_get_brew_formula_location_prefix ("cmake" , installed = True )
335
+ is not None
336
+ )
337
+
338
+ def darwin_installer (self ):
339
+ info ("Installing cmake ..." )
340
+ subprocess .check_output (["brew" , "install" , "cmake" ])
341
+
342
+
343
+ def get_required_prerequisites (platform = "linux" ):
344
+ DEFAULT_PREREQUISITES = dict (
345
+ darwin = [
346
+ HomebrewPrerequisite (),
347
+ AutoconfPrerequisite (),
348
+ AutomakePrerequisite (),
349
+ LibtoolPrerequisite (),
350
+ PkgConfigPrerequisite (),
351
+ CmakePrerequisite (),
352
+ OpenSSLPrerequisite (),
353
+ JDKPrerequisite (),
354
+ ],
355
+ linux = [],
356
+ all_platforms = [],
224
357
)
225
358
359
+ return DEFAULT_PREREQUISITES ["all_platforms" ] + DEFAULT_PREREQUISITES [platform ]
360
+
361
+
362
+ def check_and_install_default_prerequisites ():
363
+
226
364
prerequisites_not_met = []
227
365
228
366
warning (
@@ -232,7 +370,7 @@ def check_and_install_default_prerequisites():
232
370
233
371
# Phase 1: Check if all prerequisites are met and add the ones
234
372
# which are not to `prerequisites_not_met`
235
- for prerequisite in required_prerequisites :
373
+ for prerequisite in get_required_prerequisites ( sys . platform ) :
236
374
if not prerequisite .is_valid ():
237
375
prerequisites_not_met .append (prerequisite )
238
376
0 commit comments