-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_hostname.py
executable file
·36 lines (29 loc) · 1.24 KB
/
sync_hostname.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
import requests
import json
import os
CONFIG_DIST_SERVER_URI = "http://49.50.175.88:6000/config/"
HOME_PATH = os.environ['HOME']
BASHRC_PATH = f"{HOME_PATH}/.bashrc"
BASHRC_BAK_PATH = f"{BASHRC_PATH}-bak"
TARGET_PHRASE = "ROS_MASTER_URI"
if __name__ == "__main__":
response = requests.get(f"{CONFIG_DIST_SERVER_URI}server")
config = json.loads(response.text)
print(f"Fetched config file for server from {CONFIG_DIST_SERVER_URI}")
server_ip = config["server"]["ip"]
print(f"Target ROS master URI is {server_ip}")
with open(BASHRC_PATH, 'r') as file:
bashrc_data = file.readlines()
print(f"Read {len(bashrc_data)} lines from {BASHRC_PATH}")
with open(BASHRC_BAK_PATH, 'w') as file:
file.writelines(bashrc_data)
print(f"Backed up ~/.bashrc in {BASHRC_BAK_PATH}")
for i, line in enumerate(bashrc_data):
if TARGET_PHRASE in line:
new_line = f"export {TARGET_PHRASE}=http://{server_ip}:11311/"
print(f"Found {line[:-1]} in line number {i}. Replacing to {new_line}")
bashrc_data[i] = new_line + "\n"
with open(BASHRC_PATH, 'w') as file:
file.writelines(bashrc_data)
print(f"Wrote new ~/.bashrc in {BASHRC_PATH}")
print(f"Make sure to: source ~/.bashrc")