-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
executable file
·92 lines (78 loc) · 3.36 KB
/
setup.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
import subprocess
import datetime
import logging
import os
import time
import sys
import platform
# color vars for the user to understand what went wrong
GREEN = "\033[92m"
RED = "\033[91m"
RESET = "\033[0m"
# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create log directory if it doesn't exist
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
os.makedirs(log_dir, exist_ok=True)
# Define log file path
log_file_path = os.path.join(log_dir, f"{os.path.splitext(os.path.basename(__file__))[0]}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
handler = logging.FileHandler(log_file_path)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
# Platform specific commands
linux_command = ["rm", "-rf", "MacOs", "WINDOWS", "setup.py"]
macos_command = ["rm", "-rf", "Linux", "WINDOWS", "setup.py"]
Windows_command = ["cmd", "/c", "del", "MacOS", "Linux", "setup.py"]
def print_loading_bar(iterations, delay=0.1, width=40):
"""
Prints a loading bar with green dots to visualize progress.
Args:
iterations (int): Total number of iterations.
delay (float, optional): Delay between updates in seconds. Default is 0.1 seconds.
width (int, optional): Width of the loading bar. Default is 40 characters.
"""
for loadingBar in range(iterations + 1):
progress = loadingBar / iterations # Calculate the progress ratio
bar_length = int(progress * width) # Calculate the number of dots for the current progress
bar = GREEN + '•' * bar_length + RESET + ' ' * (width - bar_length) # Construct the loading bar string
percentage = int(progress * 100) # Calculate the percentage of completion
# Print the loading bar and percentage, replacing the line each iteration
print(f'\rRunning Setup.py [{bar}] {percentage} % ', end='', flush=False)
time.sleep(delay) # Pause to control the update rate
if __name__ == "__main__":
try:
# Detect platform
platform_name = platform.system().lower()
if platform_name == "linux":
platform = "linux"
elif platform_name == "darwin":
platform = "macos"
elif platform_name == "windows":
platform = "windows"
else:
platform = None
logger.error("Unsupported platform")
if platform:
# Run command
print_loading_bar(50)
print(f"\n[ {GREEN}DONE{RESET} ] running command")
if platform == "linux":
command = linux_command
elif platform == "macos":
command = macos_command
elif platform == "windows":
command = Windows_command
logger.info(f"Running command: {' '.join(command)}")
try:
subprocess.run(command, check=True, capture_output=True, text=True)
logger.info(f"Command executed successfully: {command}")
except subprocess.CalledProcessError as e:
logger.error(f"Error running command: {e}")
else:
print(f"{RED}Unsupported platform{RESET}")
logger.error("Unsupported platform")
except KeyboardInterrupt:
print("\nSetup.py interrupted")
logger.error("Setup.py interrupted")
sys.exit(1)