3
3
from os .path import isfile , join , splitext
4
4
import click
5
5
import random
6
+
6
7
python_extension = ".py"
7
8
mypath = "./"
8
9
9
10
10
-
11
11
def install_libraries (lib ):
12
12
p = Popen (["pip3" , "install" , lib ], stdout = PIPE , stderr = PIPE )
13
13
while p .poll () is None :
14
14
l = p .stdout .readline ()
15
- click .echo (l .decode (' utf-8' ))
15
+ click .echo (l .decode (" utf-8" ))
16
16
click .echo (p .stdout .read ().decode ("utf-8" ))
17
17
output , error = p .communicate ()
18
18
if p .returncode != 0 :
19
19
click .echo ("\n \n ERROR occured while installing librairies:" )
20
- return error .decode (' utf-8' )
20
+ return error .decode (" utf-8" )
21
21
else :
22
22
return False
23
23
24
+
24
25
def get_missing_libraries (traceback ):
25
- tracebacks = traceback .split (' \n ' )
26
+ tracebacks = traceback .split (" \n " )
26
27
lib = [elem for elem in tracebacks if "ModuleNotFoundError" in elem ]
27
28
if not lib :
28
29
return tracebacks [- 5 :]
29
30
lib = str (lib )
30
31
lib = lib .split ("'" )
31
- click .echo (' \n ' .join (tracebacks [- 5 :]))
32
+ click .echo (" \n " .join (tracebacks [- 5 :]))
32
33
return lib [- 2 ]
33
34
35
+
34
36
def ispythonfile (pythonfile ):
35
37
return isfile (pythonfile ) and splitext (pythonfile )[1 ] == python_extension
36
38
39
+
37
40
def lunch_script (file_name ):
38
41
p = Popen (["python3" , file_name ], stdout = PIPE , stderr = PIPE )
39
42
output , error = p .communicate ()
@@ -42,25 +45,26 @@ def lunch_script(file_name):
42
45
if "ModuleNotFoundError" in error :
43
46
pass
44
47
except TypeError :
45
- error = error .decode (' utf-8' )
48
+ error = error .decode (" utf-8" )
46
49
missing_lib = get_missing_libraries (error )
47
50
if isinstance (missing_lib , list ):
48
51
click .echo ("\n \n ERROR occured while installing librairies:" )
49
52
click .echo ("\n " .join (missing_lib ))
50
- return
51
- click .echo ("Installing " + missing_lib + "" )
53
+ return
54
+ click .echo ("Installing " + missing_lib + "" )
52
55
error_lib = install_libraries (missing_lib )
53
56
if error_lib :
54
57
click .echo (error_lib )
55
58
else :
56
- click .echo ("Installation of " + missing_lib + " done" )
59
+ click .echo ("Installation of " + missing_lib + " done" )
57
60
click .echo ("-------------------------------------\n \n " )
58
61
lunch_script (file_name )
59
62
return
60
63
64
+
61
65
@click .command ()
62
- @click .option (' --all' , is_flag = True , help = "Run all the file in current directory" )
63
- @click .argument (' file' )
66
+ @click .option (" --all" , is_flag = True , help = "Run all the file in current directory" )
67
+ @click .argument (" file" )
64
68
def cli (all , file ):
65
69
click .echo ("Thanks for using lib_install\n \n " )
66
70
if all :
@@ -71,10 +75,7 @@ def cli(all, file):
71
75
pass
72
76
for file_name in python_files :
73
77
click .echo ("++++++++++++++++++++++++++++++++" )
74
- click .echo ("Starting script for " + file_name + "" )
78
+ click .echo ("Starting script for " + file_name + "" )
75
79
lunch_script (file_name )
76
80
else :
77
81
lunch_script (file )
78
-
79
-
80
-
0 commit comments