-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_InstallTheLibs.py
More file actions
191 lines (161 loc) · 6.88 KB
/
_InstallTheLibs.py
File metadata and controls
191 lines (161 loc) · 6.88 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import subprocess
import sys
from typing import List
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import pkg_resources
import threading
import re
from time import sleep
class ProgressTracker:
def __init__(self, total_packages):
self.total_packages = total_packages
self.completed = 0
self.current_operations = {}
self.lock = threading.Lock()
def update_progress(self, package: str, status: str) -> None:
"""Update the progress status for a specific package"""
with self.lock:
self.current_operations[package] = status
self._display_progress()
def complete_package(self, package: str) -> None:
"""Mark a package as completed and remove it from current operations"""
with self.lock:
self.completed += 1
if package in self.current_operations:
del self.current_operations[package]
self._display_progress()
def _display_progress(self) -> None:
"""Display the current progress of all operations"""
percentage = (self.completed / self.total_packages) * 100
# Clear previous lines
sys.stdout.write("\033[K") # Clear current line
for _ in range(len(self.current_operations)):
sys.stdout.write("\033[F") # Move cursor up
sys.stdout.write("\033[K") # Clear line
# Print progress bar
bar_length = 30
filled = int(bar_length * percentage / 100)
bar = "█" * filled + "░" * (bar_length - filled)
print(f"\rOverall Progress: [{bar}] {percentage:.1f}% ({self.completed}/{self.total_packages})")
# Print current operations
for pkg, status in self.current_operations.items():
print(f" → {pkg}: {status}")
sys.stdout.flush()
def get_package_version(package: str) -> tuple[bool, str]:
"""
Check if a package is installed and get its version.
Args:
package: Name of the package to check
Returns:
Tuple of (is_installed, version_string)
"""
try:
version = pkg_resources.get_distribution(package).version
return True, version
except pkg_resources.DistributionNotFound:
return False, ""
def install_or_update_package(package: str, progress_tracker: ProgressTracker) -> tuple[str, bool, str]:
"""
Install a package if not present, or update it if already installed.
Args:
package: Name of the package to install/update
progress_tracker: ProgressTracker instance for monitoring progress
Returns:
Tuple of (package_name, success_status, message)
"""
installed, current_version = get_package_version(package)
try:
if installed:
progress_tracker.update_progress(package, "Checking for updates...")
# Try to upgrade the package
process = subprocess.Popen(
[sys.executable, "-m", "pip", "install", "--upgrade", package],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Monitor the upgrade process
while True:
output = process.stdout.readline()
if output:
progress_tracker.update_progress(package, output.strip())
if process.poll() is not None:
break
# Check if version changed after upgrade
_, new_version = get_package_version(package)
if process.returncode == 0:
message = (f"Updated from {current_version} to {new_version}"
if new_version != current_version
else f"Already up to date (version {current_version})")
progress_tracker.complete_package(package)
return package, True, message
else:
error = process.stderr.read().strip()
progress_tracker.complete_package(package)
return package, False, f"Update failed: {error}"
else:
progress_tracker.update_progress(package, "Starting installation...")
# Package not installed, perform fresh install
process = subprocess.Popen(
[sys.executable, "-m", "pip", "install", package],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Monitor the installation process
while True:
output = process.stdout.readline()
if output:
progress_tracker.update_progress(package, output.strip())
if process.poll() is not None:
break
if process.returncode == 0:
_, installed_version = get_package_version(package)
progress_tracker.complete_package(package)
return package, True, f"Newly installed (version {installed_version})"
else:
error = process.stderr.read().strip()
progress_tracker.complete_package(package)
return package, False, error.strip()
except Exception as e:
progress_tracker.complete_package(package)
return package, False, str(e)
def batch_install(packages: List[str], max_workers: int = 4) -> None:
"""
Install/update multiple packages in parallel using ThreadPoolExecutor.
Args:
packages: List of package names to install/update
max_workers: Maximum number of concurrent operations
"""
print(f"\nStarting batch installation/update at {datetime.now().strftime('%H:%M:%S')}")
print(f"Packages to process: {', '.join(packages)}\n")
progress_tracker = ProgressTracker(len(packages))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [
executor.submit(install_or_update_package, package, progress_tracker)
for package in packages
]
results = [future.result() for future in futures]
# Print final results
success_count = 0
print("\nFinal Results:")
print("-" * 60)
for package, success, message in results:
status = "✓" if success else "✗"
success_count += int(success)
print(f"{status} {package}: {message}")
print("-" * 60)
print(f"Successfully processed {success_count}/{len(packages)} packages")
if __name__ == "__main__":
# You can either pass packages as command line arguments
# or modify this list directly
packages_to_install = sys.argv[1:] if len(sys.argv) > 1 else [
"requests",
"pandas",
"numpy",
"glob",
"pyperclip",
"PIL"
]
batch_install(packages_to_install)