/
install
executable file
·97 lines (78 loc) · 2.67 KB
/
install
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
#! /usr/bin/env python3
from __future__ import print_function
from glob import iglob as glob
import argparse
import errno
import os
import sys
def main(arguments):
make_symlinks(arguments)
sys.exit(0)
def make_symlinks(arguments):
"""
Symlink all the dotfiles into the destination directory.
"""
ignored = {".config", ".local", ".git", ".gitignore", ".gitmodules"}
for dotfile in glob(os.path.join(arguments.dotfiles, ".*")):
if os.path.basename(dotfile) in ignored:
continue
dest = os.path.join(arguments.dest_dir, os.path.basename(dotfile))
symlink_unless_present(dotfile, dest)
for each in [".config", ".local", ".local/share"]:
dotfile_dir = os.path.join(arguments.dotfiles, each)
dest_dir = os.path.join(arguments.dest_dir, each)
try:
os.symlink(dotfile_dir, dest_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Fall back on linking individual files if we failed.
if os.path.realpath(dest_dir) != dotfile_dir:
for dotfile in os.listdir(dotfile_dir):
dotfile = os.path.join(dotfile_dir, dotfile)
dest = os.path.join(dest_dir, os.path.basename(dotfile))
symlink_unless_present(dotfile, dest)
bin_dir = os.path.abspath(os.path.normpath(arguments.bin_dir))
try:
os.makedirs(bin_dir)
except OSError as error:
if error.errno != errno.EEXIST:
raise
for dotfile in glob(os.path.join(arguments.dotfiles, "bin", ".*")):
dest = os.path.join(bin_dir, os.path.basename(dotfile))
symlink_unless_present(dotfile, dest)
def symlink_unless_present(file, dest):
"""
Symlink a file into the destination unless it exists already.
"""
file = os.path.abspath(file)
try:
os.symlink(file, dest)
except OSError as e:
if e.errno != errno.EEXIST:
raise
print("{0} already exists! Skipping...".format(dest))
else:
print("Linked {0} into {1}".format(file, dest))
parser = argparse.ArgumentParser(description="Dotfiles installer")
parser.add_argument(
"-b", "--bin-dir",
default=os.path.expanduser("~/.local/bin"),
help="the directory to use for local binaries",
)
parser.add_argument(
"-d", "--dest-dir",
default=os.path.expanduser("~"),
help="the directory to link into",
)
parser.add_argument(
"--dotfiles",
default=os.path.abspath(os.path.dirname(__file__)),
help="the dotfiles to link",
)
parser.add_argument(
"--nvim",
default="nvim",
help="the nvim binary to set up",
)
main(parser.parse_args())