-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripting.py
More file actions
158 lines (131 loc) · 5.15 KB
/
Copy pathscripting.py
File metadata and controls
158 lines (131 loc) · 5.15 KB
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
import os
import subprocess
def list_directory(root):
print("\nThe available directories are: \n")
subprocess.run(["ls","-a", root])
def search_for_env(directory):
virtualenv = []
reqfile = "pyvenv.cfg"
for dirpath, dirnames, filenames in os.walk(directory):
if reqfile in filenames:
virtualenv.append(dirpath)
return virtualenv
def show_virtual(a_virtual):
if len(a_virtual) == 0:
print("No virtual environments present in selected directory.")
while True:
ask_user = input("\nDo you want to search in another directory?? (Y/N) : ")
if ask_user.lower() in {"y","yes"}:
main()
break
elif ask_user.lower() in {"n", "no"}:
print("\nExited Successfully!!!")
break
else:
print("\nInvalid input. Enter again")
else:
while True:
try:
print(
"The virtual environments present in the selected directory are: "
)
for index, environments in enumerate(a_virtual, start=1):
print(f"{index}.{os.path.basename(environments)}")
position = int(
input(
"\nEnter the number of the environment that you want to manage: "
)
)
if position <= len(a_virtual):
index = position - 1
manage_virtual(a_virtual[index], a_virtual, index)
break
else:
print("\nChoice invalid. Please select again!!!\n")
except ValueError:
print("\nEnter a valid integer value\n")
def manage_virtual(dirpath, a_virtual, index):
print(f"\nYou are inside the virtual environment named {os.path.basename(dirpath)}")
while True:
qn = input("\nDo you want to checkout the modules installed? (Y/N): ")
if qn.lower() in {"y","yes"}:
print("\n")
show_dependencies(dirpath)
break
elif qn.lower() not in {"n", "no"}:
print("Invalid input, enter again!!! ")
else:
break
while True:
ask = input("Do you want to delete the Virtual Environment?? (Y/N): ")
if ask.lower() in {"y","yes"}:
del_virtualenv(dirpath)
del a_virtual[index]
break
elif ask.lower() not in {"n", "no"}:
print("\nInvalid input. Enter again!!! ")
else:
break
while True:
ask_user = input("Exit program or select another virtual environment?? (E/s): ")
if ask_user.lower() in {"s"}:
print("\n")
show_virtual(a_virtual)
elif ask_user.lower() in {"e","exit"}:
print("Exited Successfully!!!")
break
else:
print("Invalid input. Enter again!!!\n")
def show_dependencies(item_path):
bash_command = os.path.join(item_path,"bin/python -m pip list")
subprocess.run(bash_command, shell=True)
while True:
qn = input(
"\nDo you want to add or delete dependecies? Press S to skip (Add/Del/S): "
)
if qn.lower() in {"del","d"}:
delete_dependency(item_path)
break
elif qn.lower() in {"add","a"}:
add_dependency(item_path)
break
elif qn.lower() in {"s"}:
break
else:
print("Invalid input. Enter again!!!")
def delete_dependency(item_path):
package_name = input("\nEnter the name of the package to be uninstalled: ")
bash_command = os.path.join(item_path , f"bin/python -m pip uninstall {package_name}")
subprocess.run(bash_command, shell=True)
print("\nThe dependency now present in the virtual environment are: ")
show_dependencies(item_path)
def add_dependency(item_path):
bash_check = os.path.join(item_path, "bin/python -m pip list")
result = subprocess.run(bash_check, capture_output=True, text=True, shell=True)
package_name = input("\n Enter the name of the package to be installed: ")
if package_name in result.stdout:
print("\nPackage already installed. Enter another package!!!")
else:
bash_command = os.path.join(item_path,f"bin/python -m pip install {package_name}")
subprocess.run(bash_command, shell=True)
print("\nThe dependency currently present in the virtual environment are: ")
show_dependencies(item_path)
def del_virtualenv(item_path):
bash_command = "rm -rf " + item_path
subprocess.run(bash_command, shell=True)
print("Environment Deleted Successfully!!! \n")
def main():
while True:
root = os.path.expanduser("~")
list_directory(root)
directo = input("\nEnter directory name to search for virtual environment: ")
print("\n")
directory = os.path.join(root, directo)
if os.path.isdir(directory):
a_virtual = search_for_env(directory)
show_virtual(a_virtual)
break
else:
print("No such directory exists. Enter again")
if __name__ == "__main__":
main()