-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·100 lines (80 loc) · 2.87 KB
/
setup.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
96
97
98
99
100
#!/usr/bin/env python3
import os, sys, shutil, configparser
if os.name == "nt":
import _winapi
home = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
else:
home = os.environ["HOME"]
def confirm(question, default="y"):
default = default.lower()
if default == "y":
options = "Y/n"
else:
options = "y/N"
# TODO: make this a single character read (no endline)
choice = input("%s (%s)? " % (question, options))
choice = choice.lower()
if choice == "":
choice = default
if choice == "a":
always = True
return choice == "y"
def getboolean(config, section, option, default=False):
try:
return config.getboolean(section, option)
except configparser.NoSectionError:
return default
except configparser.NoOptionError:
return default
def recursive_symlink(path):
config = configparser.ConfigParser()
# read config first
for fn in os.listdir(path):
if fn.lower() == "dotfiles.cfg":
rel_path = os.path.normpath(os.path.join(path, fn))
config.read(rel_path)
for fn in os.listdir(path):
if fn == ".git":
continue
if fn.lower() == "dotfiles.cfg": # reserve filename for dotfiles config
continue
rel_path = os.path.normpath(os.path.join(path, fn))
# if getboolean(config, fn, "pull"): # pull repos before we recurse
# recursive_pull(path)
# recurse or link here?
if os.path.isdir(rel_path) and not getboolean(config, fn, "link"):
recursive_symlink(rel_path)
else:
link_path = os.path.join(home, rel_path)
print("link path: ", link_path)
if os.path.lexists(
link_path
): # and confirm("%s already exists. overwrite? " % link_path):
if os.path.isdir(link_path) and not os.path.islink(link_path):
print("rmtree ", link_path)
shutil.rmtree(link_path)
else:
print("os.remove ", link_path)
os.remove(link_path)
try:
os.makedirs(os.path.dirname(link_path))
except FileExistsError:
pass
print("%s <- %s" % (os.path.abspath(rel_path), link_path))
os.symlink(
os.path.abspath(rel_path),
link_path,
target_is_directory=os.path.isdir(link_path),
)
if __name__ == "__main__":
if not confirm(
"Warning: This program will create links, potentially "
+ "overwriting any dotfiles matching between your home directory and "
"this program's subdirectory 'files'. " + "Do you wish to continue",
"n",
):
sys.exit(1)
back = os.getcwd()
os.chdir("files")
recursive_symlink(os.path.join("."))
os.chdir(back)