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,45 @@ 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
+ if shutil .which ("brew" ):
119
+ return True
120
+ else :
121
+ return False
122
+
123
+ def darwin_helper (self ):
124
+ info (
125
+ "Installer for homebrew is not yet supported on macOS,"
126
+ "the nice news is that the installation process is easy!"
127
+ "See: https://brew.sh for further instructions."
128
+ )
129
+
99
130
100
131
class JDKPrerequisite (Prerequisite ):
101
132
name = "JDK"
102
- mandatory = True
103
- darwin_installer_is_supported = True
133
+ mandatory = dict ( linux = False , darwin = True )
134
+ installer_is_supported = dict ( linux = False , darwin = True )
104
135
min_supported_version = 11
105
136
106
137
def darwin_checker (self ):
@@ -216,13 +247,123 @@ def darwin_installer(self):
216
247
os .environ ["JAVA_HOME" ] = jdk_path
217
248
218
249
219
- def check_and_install_default_prerequisites ():
220
- DEFAULT_PREREQUISITES = dict (darwin = [JDKPrerequisite ()], linux = [], all_platforms = [])
250
+ class OpenSSLPrerequisite (Prerequisite ):
251
+
252
+ mandatory = dict (linux = False , darwin = True )
253
+ installer_is_supported = dict (linux = False , darwin = True )
254
+
255
+ def darwin_checker (self ):
256
+ if self .
_darwin_get_brew_formula_location_prefix (
"[email protected] " ,
installed = True ):
257
+ return True
258
+ else :
259
+ return False
260
+
261
+ def darwin_installer (self ):
262
+ info ("Installing OpenSSL ..." )
263
+ subprocess .
check_output ([
"brew" ,
"install" ,
"[email protected] " ])
264
+
265
+
266
+ class AutoconfPrerequisite (Prerequisite ):
267
+ name = "autoconf"
268
+ mandatory = dict (linux = False , darwin = True )
269
+ installer_is_supported = dict (linux = False , darwin = True )
270
+
271
+ def darwin_checker (self ):
272
+ if self ._darwin_get_brew_formula_location_prefix ("autoconf" , installed = True ):
273
+ return True
274
+ else :
275
+ return False
276
+
277
+ def darwin_installer (self ):
278
+ info ("Installing Autoconf ..." )
279
+ subprocess .check_output (["brew" , "install" , "autoconf" ])
280
+
221
281
222
- required_prerequisites = (
223
- DEFAULT_PREREQUISITES ["all_platforms" ] + DEFAULT_PREREQUISITES [sys .platform ]
282
+ class AutomakePrerequisite (Prerequisite ):
283
+ name = "automake"
284
+ mandatory = dict (linux = False , darwin = True )
285
+ installer_is_supported = dict (linux = False , darwin = True )
286
+
287
+ def darwin_checker (self ):
288
+ if self ._darwin_get_brew_formula_location_prefix ("automake" , installed = True ):
289
+ return True
290
+ else :
291
+ return False
292
+
293
+ def darwin_installer (self ):
294
+ info ("Installing Automake ..." )
295
+ subprocess .check_output (["brew" , "install" , "automake" ])
296
+
297
+
298
+ class LibtoolPrerequisite (Prerequisite ):
299
+ name = "libtool"
300
+ mandatory = dict (linux = False , darwin = True )
301
+ installer_is_supported = dict (linux = False , darwin = True )
302
+
303
+ def darwin_checker (self ):
304
+ if self ._darwin_get_brew_formula_location_prefix ("libtool" , installed = True ):
305
+ return True
306
+ else :
307
+ return False
308
+
309
+ def darwin_installer (self ):
310
+ info ("Installing Libtool ..." )
311
+ subprocess .check_output (["brew" , "install" , "libtool" ])
312
+
313
+
314
+ class PkgConfigPrerequisite (Prerequisite ):
315
+ name = "pkg-config"
316
+ mandatory = dict (linux = False , darwin = True )
317
+ installer_is_supported = dict (linux = False , darwin = True )
318
+
319
+ def darwin_checker (self ):
320
+ if self ._darwin_get_brew_formula_location_prefix ("pkg-config" , installed = True ):
321
+ return True
322
+ else :
323
+ return False
324
+
325
+ def darwin_installer (self ):
326
+ info ("Installing Pkg-Config ..." )
327
+ subprocess .check_output (["brew" , "install" , "pkg-config" ])
328
+
329
+
330
+ class CmakePrerequisite (Prerequisite ):
331
+ name = "cmake"
332
+ mandatory = dict (linux = False , darwin = True )
333
+ installer_is_supported = dict (linux = False , darwin = True )
334
+
335
+ def darwin_checker (self ):
336
+ if self ._darwin_get_brew_formula_location_prefix ("cmake" , installed = True ):
337
+ return True
338
+ else :
339
+ return False
340
+
341
+ def darwin_installer (self ):
342
+ info ("Installing cmake ..." )
343
+ subprocess .check_output (["brew" , "install" , "cmake" ])
344
+
345
+
346
+ def get_required_prerequisites (platform = "linux" ):
347
+ DEFAULT_PREREQUISITES = dict (
348
+ darwin = [
349
+ HomebrewPrerequisite (),
350
+ AutoconfPrerequisite (),
351
+ AutomakePrerequisite (),
352
+ LibtoolPrerequisite (),
353
+ PkgConfigPrerequisite (),
354
+ CmakePrerequisite (),
355
+ OpenSSLPrerequisite (),
356
+ JDKPrerequisite (),
357
+ ],
358
+ linux = [],
359
+ all_platforms = [],
224
360
)
225
361
362
+ return DEFAULT_PREREQUISITES ["all_platforms" ] + DEFAULT_PREREQUISITES [platform ]
363
+
364
+
365
+ def check_and_install_default_prerequisites ():
366
+
226
367
prerequisites_not_met = []
227
368
228
369
warning (
@@ -232,7 +373,7 @@ def check_and_install_default_prerequisites():
232
373
233
374
# Phase 1: Check if all prerequisites are met and add the ones
234
375
# which are not to `prerequisites_not_met`
235
- for prerequisite in required_prerequisites :
376
+ for prerequisite in get_required_prerequisites ( sys . platform ) :
236
377
if not prerequisite .is_valid ():
237
378
prerequisites_not_met .append (prerequisite )
238
379
0 commit comments