-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pip-installer.py
471 lines (392 loc) · 16.3 KB
/
pip-installer.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import json
import os
import platform
import subprocess
import sys
import time
import traceback
PIP_NOT_INSTALLED_ERROR = 1
PACKAGE_INSTALLATION_ERROR = 2
PACKAGE_UPDATE_ERROR = 3
def create_package(author, name, version, requirements, main_file_path):
# Create package directory
package_dir = f"{name.lower().replace(' ', '_')}"
os.makedirs(package_dir)
# Create package files
with open(os.path.join(package_dir, "__init__.py"), "w") as init_file:
pass
if main_file_path:
# Read content from user-specified file path
with open(main_file_path, "r") as main_content_file:
main_content = main_content_file.read()
else:
# Use default content for main.py if no file path is provided
main_content = f"""
def hello():
print("Hello, {name}!")
if __name__ == "__main__":
hello()
"""
with open(os.path.join(package_dir, "main.py"), "w") as main_file:
main_file.write(main_content)
with open("setup.py", "w") as setup_file:
setup_file.write(f"""
from setuptools import setup
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(
name='{name}',
version='{version}',
author='{author}',
description='{name} package',
long_description=long_description,
long_description_content_type="text/markdown",
packages=['{package_dir}'],
install_requires={requirements},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
""")
with open("README.md", "w") as readme_file:
readme_file.write(f"# {name}\n\nThis is the {name} package.\n")
# Print success message
print(f"Package '{name}' created successfully!")
def display_stats(start_time, errors, num_updated_installed):
end_time = time.time()
elapsed_time = end_time - start_time
print("\n---------- Statistics ----------")
print(f"Time elapsed: {elapsed_time:.2f} seconds")
print(f"Errors encountered: {errors}")
print(f"Number of updated/installed packages: {num_updated_installed}")
print("---------------------------------")
def get_installed_packages_count():
try:
output = subprocess.run(["pip", "list"],
check=True,
capture_output=True,
text=True)
lines = output.stdout.strip().split("\n")
# Subtract 2 from the total count to exclude header and footer lines
count = max(len(lines) - 2, 0)
return count
except subprocess.CalledProcessError:
return 0
def get_updatable_packages_count():
try:
output = subprocess.run(["pip", "list", "--outdated"],
check=True,
capture_output=True,
text=True)
lines = output.stdout.strip().split("\n")
# Subtract 2 from the total count to exclude header and footer lines
count = max(len(lines) - 2, 0)
return count
except subprocess.CalledProcessError:
return 0
def check_pip_availability():
try:
subprocess.run(["pip", "--version"], check=True, capture_output=True)
return True
except subprocess.CalledProcessError:
return False
def install_pip():
try:
subprocess.run([sys.executable, "-m", "ensurepip", "--upgrade"],
check=True)
print("Successfully installed pip.")
except subprocess.CalledProcessError:
print("Error installing pip.")
sys.exit(PIP_NOT_INSTALLED_ERROR)
def check_package_availability(package):
try:
subprocess.run(["pip", "show", package],
check=True,
capture_output=True)
return True
except subprocess.CalledProcessError:
return False
def get_available_versions(package):
try:
output = subprocess.run(["pip", "search", package],
check=True,
capture_output=True,
text=True)
lines = output.stdout.strip().split("\n")
versions = [line.split("(")[1].split(")")[0].strip() for line in lines]
return versions
except subprocess.CalledProcessError:
return []
def install_package(package, version=None, combined=False):
start_time = time.time()
errors = 0
num_updated_installed = 0
try:
if version:
subprocess.run(["pip", "install", f"{package}=={version}"],
check=True)
print(f"Successfully installed {package} version {version}")
num_updated_installed += 1
else:
subprocess.run(["pip", "install", package], check=True)
print(f"Successfully installed {package}")
num_updated_installed += 1
except subprocess.CalledProcessError:
print(f"Error installing {package}")
errors += 1
if combined is False:
display_stats(start_time, errors, num_updated_installed)
def install_packages(package_list):
start_time = time.time()
errors = 0
num_updated_installed = 0
packages = [package.strip() for package in package_list.split(",")]
for package in packages:
if check_package_availability(package):
print(f"Package '{package}' already installed")
else:
if package.strip() != "":
result = install_package(package, combined=True)
if result != 0:
errors += 1
else:
num_updated_installed += 1
display_stats(start_time, errors, num_updated_installed)
return errors
def view_extensions():
try:
output = subprocess.run(["pip", "list", "--outdated"],
check=True,
capture_output=True,
text=True)
print("Extensions:")
print("-----------")
print(output.stdout)
except subprocess.CalledProcessError:
print("Error retrieving extension information.")
def update_packages():
try:
start_time = time.time()
num_updated_packages = 0
errors = 0
# Update pip
subprocess.run(["pip", "install", "--upgrade", "pip"], check=True)
num_updated_packages += 1
# Update other packages
outdated_packages = subprocess.run(
"pip list --outdated --format=json",
shell=True,
check=True,
capture_output=True,
text=True,
)
packages = json.loads(outdated_packages.stdout)
for package in packages:
package_name = package["name"]
if package_name.strip() != "":
try:
subprocess.run(["pip", "install", "-U", package_name],
check=True)
num_updated_packages += 1
except subprocess.CalledProcessError as e:
error_message = e.stderr
if error_message is not None:
error_message = error_message.decode().strip()
if not error_message.startswith(
"ERROR: Invalid requirement:"):
print(
f"Error updating package '{package_name}': {error_message}"
)
else:
print(
f"Skipped updating package '{package_name}' due to an error"
)
else:
print(f"Error updating package '{package_name}'")
errors += 1
else:
print(f"Updated package '{package_name}'")
if num_updated_packages == 1:
print("All packages are up to date.")
else:
print(f"{num_updated_packages - 1} packages updated successfully.")
display_stats(start_time, errors, num_updated_packages)
except subprocess.CalledProcessError:
print("Error updating packages.")
sys.exit(PACKAGE_UPDATE_ERROR)
def display_crash_screen(error):
print("\n========== Crash Report ==========")
print("An error occurred while running the program.")
print("Please help us improve by reporting this issue:")
print(
"- Repository: https://github.com/LopeKinz/pip-installer/issues/new?assignees=&labels=Bug&projects=&template=Bug.md"
)
print("-----------------------------------")
print("System Information:")
print(f"Python Version: {sys.version}")
print(f"Operating System: {platform.system()} {platform.release()}")
print("-----------------------------------")
print("Error Details:")
traceback.print_exception(type(error), error, error.__traceback__)
print("-----------------------------------")
print("Thank you for your support!")
print("===================================")
def main():
try:
installed_count = get_installed_packages_count()
updatable_count = get_updatable_packages_count()
while True:
if not check_pip_availability():
print("pip is not installed. Installing pip...")
install_pip()
print("\n===================================")
print(" Pip Installer ")
print("===================================")
print("1. Single Mode")
print("2. Multi Mode")
print("3. View Updatable Extensions")
print("4. Update Packages")
print("5. Uninstall Package")
print("6. Create Own Package")
print("7. Install from Textfile")
print("0. Exit")
print("-----------------------------------")
print("Author: LopeKinz")
print("GitHub: https://github.com/LopeKinz")
print("-----------------------------------")
print("Installed packages:", installed_count)
print("Updatable packages:", updatable_count)
print("-----------------------------------")
choice = input("Enter your choice: ")
if choice == "1":
start_time = time.time()
errors = 0
num_updated_installed = 0
while True:
print(
"Enter the package name to install (or type 'q' to quit):"
)
package_name = input()
if package_name.lower() == "q":
break
if check_package_availability(package_name):
print(f"Package '{package_name}' already installed")
else:
versions = get_available_versions(package_name)
if versions:
print(f"Available versions for {package_name}:")
for i, version in enumerate(versions, start=1):
print(f"{i}. {version}")
version_choice = input(
"Choose the version number (or press Enter for the latest version): "
)
if version_choice.isdigit() and int(
version_choice) in range(
1,
len(versions) + 1):
version = versions[int(version_choice) - 1]
else:
version = None
try:
install_package(package_name, version)
num_updated_installed += 1
except subprocess.CalledProcessError:
errors += 1
else:
try:
install_package(package_name)
num_updated_installed += 1
except subprocess.CalledProcessError:
errors += 1
display_stats(start_time, errors, num_updated_installed)
elif choice == "2":
start_time = time.time()
errors = 0
num_updated_installed = 0
print(
"Enter package names separated by commas (or type 'q' to quit):"
)
package_list = input()
if package_list.lower() == "q":
continue
try:
install_packages(package_list)
num_updated_installed = len(package_list.split(","))
except subprocess.CalledProcessError:
errors += 1
elif choice == "3":
start_time = time.time()
errors = 0
num_updated_installed = 0
view_extensions()
elif choice == "4":
start_time = time.time()
errors = 0
num_updated_installed = 0
try:
update_packages()
num_updated_installed = 1
except subprocess.CalledProcessError:
errors += 1
elif choice == "5":
start_time = time.time()
errors = 0
num_updated_installed = 0
print(
"Enter the package name to uninstall (or type 'q' to quit):"
)
package_name = input()
if package_name.lower() == "q":
continue
try:
subprocess.run(["pip", "uninstall", package_name],
check=True)
num_updated_installed += 1
except subprocess.CalledProcessError:
errors += 1
display_stats(start_time, errors, num_updated_installed)
elif choice == "6":
author = input("Enter the author name: ")
name = input("Enter the package name: ")
version = input("Enter the package version: ")
requirements = input(
"Enter the package requirements (comma-separated): "
).split(",")
main_file_path = input(
"Enter the path to the main.py file (leave blank to use default content): "
)
# Create the package
create_package(author, name, version, requirements,
main_file_path)
elif choice == "7":
start_time = time.time()
errors = 0
num_updated_installed = 0
print("Enter the path to the text file containing package names (or type 'q' to quit):")
file_path = input()
if file_path.lower() == "q":
continue
try:
with open(file_path, "r") as file:
package_list = file.read()
install_packages(package_list)
num_updated_installed = len(package_list.split(","))
except FileNotFoundError:
print("File not found. Please check the path and try again.")
errors += 1
except Exception as e:
print("An error occurred while installing packages from the file.")
print(f"Error message: {str(e)}")
errors += 1
display_stats(start_time, errors, num_updated_installed)
elif choice == "0":
break
else:
print("Invalid choice. Please try again.")
except Exception as e:
display_crash_screen(e)
if __name__ == "__main__":
main()