@@ -6,52 +6,36 @@ print_message() {
6
6
echo " ${MESSAGE} "
7
7
}
8
8
9
- # Function to install Python 3 and pip3
10
- install_python3 () {
11
- # Detect the operating system
12
- OS=$( uname -s)
13
-
14
- if [ " $OS " == " Linux" ]; then
15
- # Check if the system is using apt (Debian/Ubuntu)
16
- if command -v apt-get & > /dev/null; then
17
- sudo apt-get update
18
- sudo apt-get install -y python3 python3-pip
19
- # Check if the system is using yum (CentOS/RHEL)
20
- elif command -v yum & > /dev/null; then
21
- sudo yum install -y python3 python3-pip
22
- else
23
- echo " Unsupported Linux package manager. Please install Python 3 manually."
24
- exit 1
25
- fi
26
- elif [ " $OS " == " Darwin" ]; then
27
- # macOS
28
- if command -v brew & > /dev/null; then
29
- brew install python3
30
- else
31
- echo " Homebrew not found. Please install Homebrew and try again."
32
- exit 1
33
- fi
34
- else
35
- echo " Unsupported OS. Please install Python 3 manually."
36
- exit 1
9
+ # Function to create and activate a virtual environment
10
+ create_and_activate_venv () {
11
+ VENV_DIR=" .venv"
12
+
13
+ # Check if the virtual environment already exists
14
+ if [ ! -d " $VENV_DIR " ]; then
15
+ echo " Creating virtual environment in ${VENV_DIR} ..."
16
+ python3 -m venv " $VENV_DIR "
37
17
fi
38
18
39
- # Ensure Python 3 is usable in PATH
40
- if ! command -v python3 & > /dev/null; then
41
- echo " Python 3 installation failed or not found in PATH."
42
- exit 1
43
- else
44
- echo " Python 3 is successfully installed and available in PATH."
45
- fi
19
+ # Activate the virtual environment
20
+ echo " Activating virtual environment..."
21
+ source " ${VENV_DIR} /bin/activate"
46
22
47
- # Install tqdm using pip3
48
- sudo -H pip3 install tqdm
23
+ # Upgrade pip within the virtual environment
24
+ pip install --upgrade pip
25
+ }
49
26
50
- if ! python3 -m pip show tqdm & > /dev/null; then
51
- echo " tqdm installation failed."
52
- exit 1
53
- else
27
+ # Function to install tqdm using pip
28
+ install_tqdm () {
29
+ # Attempt to install tqdm
30
+ pip install tqdm
31
+
32
+ # Check if tqdm was successfully installed
33
+ if python3 -m pip show tqdm & > /dev/null; then
54
34
echo " tqdm is successfully installed."
35
+ return 0
36
+ else
37
+ echo " tqdm installation failed. Falling back to ASCII progress bar."
38
+ return 1
55
39
fi
56
40
}
57
41
@@ -60,7 +44,22 @@ insert_bootnodes() {
60
44
python3 << EOF
61
45
import re
62
46
import time
63
- from tqdm import tqdm
47
+
48
+ try:
49
+ from tqdm import tqdm
50
+
51
+ def progress_bar(iterable, desc):
52
+ return tqdm(iterable, desc=desc, ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}")
53
+
54
+ except ImportError:
55
+ # Fallback ASCII progress bar
56
+ def progress_bar(iterable, desc):
57
+ total = len(iterable)
58
+ print(f"{desc} [{'#' * total}]")
59
+ for i, item in enumerate(iterable):
60
+ yield item
61
+ print(f"\r{desc} [{'#'*(i+1)}{'.'*(total-i-1)}] | {i+1}/{total}", end='')
62
+ print()
64
63
65
64
def insert_bootnodes(original_file, bootnodes_file):
66
65
try:
@@ -71,15 +70,15 @@ def insert_bootnodes(original_file, bootnodes_file):
71
70
bootnodes_content = file.read().strip()
72
71
73
72
# Progress bar for processing the content
74
- for _ in tqdm (range(10), desc="🌟 Processing content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt} "):
73
+ for _ in progress_bar (range(10), desc="🌟 Processing content"):
75
74
time.sleep(0.1) # Simulate work being done
76
75
77
76
# Find the bootNodes section, clear its contents, and insert the new bootnodes content
78
- pattern = re.compile(r'("bootNodes"\\ s*:\\ s*\\ [)[^\\ ]]*? (\\ ])', re.DOTALL)
77
+ pattern = re.compile(r'("bootNodes"\\ s*:\\ s*\\ [)[^\\ ]]*(\\ ])', re.DOTALL)
79
78
new_content = pattern.sub(r'\\ 1\n' + bootnodes_content + r'\\ 2', original_content)
80
79
81
80
# Progress bar for writing the new content
82
- for _ in tqdm (range(10), desc="🌟 Writing new content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt} "):
81
+ for _ in progress_bar (range(10), desc="🌟 Writing new content"):
83
82
time.sleep(0.1) # Simulate work being done
84
83
85
84
# Write the modified content back to the original file
@@ -96,7 +95,7 @@ def main():
96
95
bootnodes_file = 'bootnodes.txt' # Path to the bootnodes file
97
96
98
97
# Progress bar for reading files
99
- for _ in tqdm (range(10), desc="📄 Reading files", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt} "):
98
+ for _ in progress_bar (range(10), desc="📄 Reading files"):
100
99
time.sleep(0.1) # Simulate work being done
101
100
102
101
insert_bootnodes(original_file, bootnodes_file)
@@ -106,8 +105,8 @@ if __name__ == "__main__":
106
105
EOF
107
106
}
108
107
109
-
110
108
# Main script execution
111
- insert_bootnodes
109
+ create_and_activate_venv
110
+ install_tqdm && insert_bootnodes || insert_bootnodes
112
111
113
112
print_message " Boot nodes insertion complete."
0 commit comments