-
Notifications
You must be signed in to change notification settings - Fork 0
/
CM.py
executable file
·78 lines (63 loc) · 2.79 KB
/
CM.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
#!/usr/bin/env python3
import os
import argparse
import logging
def setup_logging():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def list_configurations(config_dir):
"""List all .ini configuration files in the specified directory."""
try:
return [f for f in os.listdir(config_dir) if f.endswith('.ini')]
except FileNotFoundError:
logging.error(f"Directory '{config_dir}' does not exist.")
return []
def get_current_symlink(symlink_path):
"""Get the target of the symlink."""
if os.path.islink(symlink_path):
return os.readlink(symlink_path)
return None
def update_symlink(symlink_path, new_target):
"""Update the symlink to point to the new target."""
try:
if os.path.exists(symlink_path):
os.remove(symlink_path)
os.symlink(new_target, symlink_path)
logging.info(f"Symlink updated to point to {new_target}")
except Exception as e:
logging.error(f"Failed to update symlink: {e}")
def prompt_user_selection(configurations):
"""Prompt the user to select a configuration file from the list."""
while True:
try:
choice = int(input("\nSelect the number of the configuration file you want to use: "))
if 1 <= choice <= len(configurations):
return configurations[choice - 1]
else:
raise ValueError
except ValueError:
print("Invalid selection. Please enter a number corresponding to the configuration file.")
def main(config_dir, symlink_path):
setup_logging()
print("AWSSOCKS Configuration Manager")
print("==============================")
configurations = list_configurations(config_dir)
if not configurations:
print("No configuration files found in the configs directory.")
return
current_target = get_current_symlink(symlink_path)
print("Available configuration files:")
for i, config in enumerate(configurations, 1):
full_path = os.path.join(config_dir, config)
if current_target and os.path.samefile(full_path, current_target):
print(f"{i}. {config} (current)")
else:
print(f"{i}. {config}")
selected_file = prompt_user_selection(configurations)
new_target = os.path.join(config_dir, selected_file)
update_symlink(symlink_path, new_target)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Manage configuration files via symlink.")
parser.add_argument('--config-dir', type=str, default='configs', help="Directory containing configuration files.")
parser.add_argument('--symlink-path', type=str, default='current-config.ini', help="Path to the symlink.")
args = parser.parse_args()
main(args.config_dir, args.symlink_path)