-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
95 lines (77 loc) · 2.63 KB
/
config.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
93
94
95
import os
import json
import shutil
import argparse
import platform
# const
config_file_name = "config.json"
dotfile_dir = os.path.dirname(os.path.abspath(__file__))
home_dir = ""
if platform.system() == "Windows":
home_dir = os.path.expanduser('~')
elif platform.system() == "Darwin":
home_dir = os.getenv("HOME")
def get_os_json(path):
with open(path, "r") as config:
json_data = json.load(config)
if platform.system() == "Windows":
return json_data["win"]
elif platform.system() == "Darwin":
return json_data["mac"]
elif platform.system() == "Linux":
return json_data["linux"]
return None
def get_pathinfo_from_json(data):
source = data["source"]
linkto = data["linkto"]
assert source != "" and linkto != ""
return f"{dotfile_dir}/{source}", f"{home_dir}/{linkto}"
def remove_link(path):
if os.path.exists(path) and (os.path.islink(path) or os.path.isfile(path)):
os.remove(path)
elif os.path.exists(path):
shutil.rmtree(path)
def touch_symlink(source, linkto):
remove_link(linkto)
directory = os.path.dirname(linkto)
if not os.path.exists(directory):
os.makedirs(directory)
if os.path.isdir(source):
os.symlink(source, linkto, target_is_directory=True)
elif os.path.isfile(source):
os.symlink(source, linkto, target_is_directory=False)
def install(path):
json = get_os_json(path)
for app_name, config_path_vec in json.items():
for paths in config_path_vec:
source, linkto = get_pathinfo_from_json(paths)
touch_symlink(source, linkto)
print(f"{source} --> {linkto} \n")
print(f"Finish config: {app_name}...\n")
def uninstall(path):
json = get_os_json(path)
for app_name, config_path_vec in json.items():
for path_info in config_path_vec:
_, linkto = get_pathinfo_from_json(path_info)
if os.path.islink(linkto):
os.remove(linkto)
else:
print(f"Error: {linkto} is not a link file")
print(f"Finish unconfig: {app_name}...\n")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--install", action="store_true", help="config the applications."
)
parser.add_argument(
"--uninstall", action="store_true", help="unconfig the applications."
)
args = parser.parse_args()
if args.install:
install(config_file_name)
elif args.uninstall:
uninstall(config_file_name)
else:
print("Usage: python config.py -- [install | uninstall]")
if __name__ == "__main__":
main()