Skip to content

Commit 0f8b7e8

Browse files
authored
Merge pull request #33 from Devolved-AI/psyfercom-patch-8
Update update_bootnodes.sh
2 parents ceb69fc + a6ae8d2 commit 0f8b7e8

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

update_bootnodes.sh

+47-48
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,36 @@ print_message() {
66
echo "${MESSAGE}"
77
}
88

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"
3717
fi
3818

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"
4622

47-
# Install tqdm using pip3
48-
sudo -H pip3 install tqdm
23+
# Upgrade pip within the virtual environment
24+
pip install --upgrade pip
25+
}
4926

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
5434
echo "tqdm is successfully installed."
35+
return 0
36+
else
37+
echo "tqdm installation failed. Falling back to ASCII progress bar."
38+
return 1
5539
fi
5640
}
5741

@@ -60,7 +44,22 @@ insert_bootnodes() {
6044
python3 <<EOF
6145
import re
6246
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()
6463
6564
def insert_bootnodes(original_file, bootnodes_file):
6665
try:
@@ -71,15 +70,15 @@ def insert_bootnodes(original_file, bootnodes_file):
7170
bootnodes_content = file.read().strip()
7271
7372
# 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"):
7574
time.sleep(0.1) # Simulate work being done
7675
7776
# 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)
7978
new_content = pattern.sub(r'\\1\n' + bootnodes_content + r'\\2', original_content)
8079
8180
# 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"):
8382
time.sleep(0.1) # Simulate work being done
8483
8584
# Write the modified content back to the original file
@@ -96,7 +95,7 @@ def main():
9695
bootnodes_file = 'bootnodes.txt' # Path to the bootnodes file
9796
9897
# 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"):
10099
time.sleep(0.1) # Simulate work being done
101100
102101
insert_bootnodes(original_file, bootnodes_file)
@@ -106,8 +105,8 @@ if __name__ == "__main__":
106105
EOF
107106
}
108107

109-
110108
# Main script execution
111-
insert_bootnodes
109+
create_and_activate_venv
110+
install_tqdm && insert_bootnodes || insert_bootnodes
112111

113112
print_message "Boot nodes insertion complete."

0 commit comments

Comments
 (0)