@@ -721,13 +721,30 @@ def _add_colors(self, txt):
721
721
x = t .replace ('mpv' , '[green]mpv[/green]' ).replace ('mplayer' , '[green]mplayer[/green]' ).replace ('vlc' , '[green]vlc[/green]' )
722
722
return '[bold]' + x .replace ('||' , r']' ).replace ('|' , r'\[' ) + '[/bold]'
723
723
724
+
724
725
class PythonExecutable (object ):
726
+ ''' A class to verify that python is installed
727
+ and in the PATH
728
+ '''
725
729
is_debian = False
726
730
_python = [None , None ]
727
731
requested_python_version = 3
728
732
729
- def __init__ (self , requested_python_version ):
733
+ def __init__ (
734
+ self ,
735
+ requested_python_version ,
736
+ terminate_if_not_found = False ):
737
+ ''' Parameters
738
+ ==========
739
+ requested_python_version
740
+ The version of python we are looking for
741
+ (either 2 or 3)
742
+ terminate_if_not_found
743
+ If True, the program will terminate if python
744
+ is not found (default is False)
745
+ '''
730
746
self .requested_python_version = requested_python_version
747
+ self ._terminate_if_not_found = terminate_if_not_found
731
748
if not platform .system ().lower ().startswith ('win' ):
732
749
self ._check_if_is_debian_based ()
733
750
self ._get_pythons ()
@@ -790,16 +807,25 @@ def _get_python(self, version):
790
807
stdout = subprocess .PIPE ,
791
808
stderr = subprocess .PIPE
792
809
)
793
- # print(p.communicate())
794
- # print(p.returncode)
795
- # if p.communicate()[0]:
796
810
p .communicate ()
797
811
if p .returncode == 0 :
798
812
self ._python [version - 2 ] = 'python' + str (version )
799
813
800
- def can_install (self ):
801
- return True if self ._python [self .requested_python_version - 2 ] else False
814
+ if self ._terminate_if_not_found and \
815
+ not self .can_use ():
816
+ print ('''
802
817
818
+ Python {} was not found in your system.
819
+ If you have already installed it, you probably have not added it in your PATH.
820
+ To verify that Python is in your PATH open a terminal/console and type "python".
821
+ If you get en error, you have to add it to your PATH.
822
+ ''' .format ('2' if self .requested_python_version == 2 else '3' ))
823
+ sys .exit (1 )
824
+
825
+ def can_use (self ):
826
+ ''' Can I use the python I requested?
827
+ '''
828
+ return True if self ._python [self .requested_python_version - 2 ] else False
803
829
804
830
class PyRadioUpdate (object ):
805
831
@@ -847,17 +873,10 @@ def __init__(self,
847
873
self ._package = package
848
874
self .user = user
849
875
self ._github_long_description = github_long_description
850
- self ._python_exec = PythonExecutable (python_version_to_use )
851
- if self ._python_exec .python is None :
852
- print ('''
853
-
854
- Python was not found in your system.
855
- If you have already installed it, chances are you have not added it in your PATH.
856
- To verify that Python is in your PATH open a terminal/console and type "python".
857
- If you get en error, you have to add it to your PATH.
858
- ''' )
859
- sys .exit (1 )
860
-
876
+ self ._python_exec = PythonExecutable (
877
+ python_version_to_use ,
878
+ terminate_if_not_found = True
879
+ )
861
880
self .python2 = True if python_version_to_use == 2 else False
862
881
self ._pix_isolated = pix_isolated
863
882
@@ -1259,17 +1278,10 @@ def __init__(self,
1259
1278
self ._package = package
1260
1279
self ._fromTUI = fromTUI
1261
1280
self ._github_long_description = github_long_description
1262
- self ._python_exec = PythonExecutable (python_version_to_use )
1263
- if self ._python_exec .python is None :
1264
- print ('''
1265
-
1266
- Python was not found in your system.
1267
- If you have already installed it, chances are you have not added it in your PATH.
1268
- To verify that Python is in your PATH open a terminal/console and type "python".
1269
- If you get en error, you have to add it to your PATH.
1270
- ''' )
1271
- sys .exit (1 )
1272
-
1281
+ self ._python_exec = PythonExecutable (
1282
+ python_version_to_use ,
1283
+ terminate_if_not_found = True
1284
+ )
1273
1285
self .python2 = True if python_version_to_use == 2 else False
1274
1286
self ._pix_isolated = pix_isolated
1275
1287
self ._get_cache = False
@@ -1465,19 +1477,12 @@ def _do_it(self, mode='update'):
1465
1477
print_python2 ()
1466
1478
1467
1479
python_version_to_use = 2 if args .python2 else 3
1468
- python_exec = PythonExecutable (python_version_to_use )
1469
-
1470
- if python_exec .python is None :
1471
- print ('''
1472
-
1473
- Python was not found in your system.
1474
- If you have already installed it, chances are you have not added it in your PATH.
1475
- To verify that Python is in your PATH open a terminal/console and type "python".
1476
- If you get en error, you have to add it to your PATH.
1477
- ''' )
1478
- sys .exit (1 )
1480
+ python_exec = PythonExecutable (
1481
+ python_version_to_use ,
1482
+ terminate_if_not_found = True
1483
+ )
1479
1484
1480
- if not python_exec .can_install :
1485
+ if not python_exec .can_use :
1481
1486
print ('Error: Python {} not found on your system...\n ' .format ('2' if python_exec .requested_python_version == 2 else '3' ))
1482
1487
sys .exit (1 )
1483
1488
0 commit comments