-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotbin-link.py
181 lines (153 loc) · 6.16 KB
/
dotbin-link.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Manager for dotbin/extra
You will put any portable software package in `dotbin/extra/portable`
and any single-binary portable software in `dotbin/extra/bin`
`dotbin/extra/bin` and `dotbin/extra/symlink` should be added to PATH
`dotbin-link [--force]` will create symlinks from `portable` to `symlink`.
The links are configured by `dotbin/extra/portable/link` text file.
The file contains one configuration entry per line. Each entry is of the form:
(alias=ALIASES)?(shim:)?PATH
or
alias=ALIASES(shim:)?which:PATH
PATH should be the relative path or a glob pattern from `dotbin/extra/portable`.
A symbolic link will be created in `dotbin/extra/symlink` pointing to the original
file, unless `shim:` is specified, in which case a `.cmd` shim (no extension on non-Windows)
will be created to invoke the original file. Use `shim` when the executable has local
dependencies.
`alias=ALIASES` will create additional symbolic links to the link/shim created.
Alternatively, you can put the aliases in the shell profile, which will only make
them available for that shell. Use `,` to separate multiple aliases.
`alias` can only be used for path or glob pattern that matches exactly 1 file
`which` can be used to create aliases to other programs. In this case, `shim` determines
if a symbolic or shim is created for that alias.
--force will delete existing symlinks/shims
"""
import sys
import subprocess
import os
import shutil
import glob
WINDOWS = os.name == "nt"
LN = shutil.which("ln")
if not LN:
raise ValueError("ln not found")
if WINDOWS:
CHMOD = None
else:
CHMOD = shutil.which("chmod")
def chmod():
if not CHMOD:
raise ValueError("chmod not found")
return CHMOD
def find_extra_dir():
return os.path.join(os.path.dirname(__file__), "extra")
def create_batch_shim(symlink_dir, path, shim_name):
name = shim_name + ".cmd"
shim_path = os.path.join(symlink_dir, name)
if os.path.exists(shim_path):
return
print(f"shim: {shim_path}")
with open(shim_path, "w", encoding="utf-8") as f:
f.write("@echo off\r\n")
executable = os.path.abspath(path)
f.write(f"\"{executable}\" %*")
def create_bash_shim(symlink_dir, path, shim_name):
name = shim_name
shim_path = os.path.join(symlink_dir, name)
if os.path.exists(shim_path):
return
print(f"shim: {shim_path}")
with open(shim_path, "w", encoding="utf-8") as f:
f.write("#!/usr/bin/bash\n")
executable = os.path.abspath(path)
f.write(f"exec \"{executable}\" \"$@\"")
subprocess.run([chmod(), "+x", shim_path], check=True)
def create_shim(symlink_dir, path, shim_name):
if WINDOWS:
create_batch_shim(symlink_dir, path, shim_name)
else:
create_bash_shim(symlink_dir, path, shim_name)
def create_link(symlink_dir, path):
name = os.path.basename(path)
symlink_path = os.path.join(symlink_dir, name)
if os.path.exists(symlink_path):
return
print(f"ln -s {path} {symlink_path}")
subprocess.run([LN, "-s", path, symlink_path], check=True)
def create_alias(symlink_dir, target, aliases, shim):
if not os.path.exists(target):
print(f"warning: alias target {target} doesn't exist, skipping")
return
for alias in aliases:
if shim:
create_shim(symlink_dir, target, alias)
else:
_, ext = os.path.splitext(target)
alias_path = os.path.join(symlink_dir, alias+ext)
if os.path.exists(alias_path):
continue
print(f"ln -s {target} {alias_path}")
subprocess.run([LN, "-s", target, alias_path], check=True)
def add_link(symlink_dir, portable_dir, config: str):
original_config = config
if config.startswith("alias="):
config = config[6:]
colon = config.find(":")
if colon < 0:
print(f"error: invalid alias config {original_config}")
sys.exit(1)
aliases = config[:colon]
config = config[colon+1:]
aliases = [x.strip() for x in aliases.split(",") if x.strip()]
else:
aliases = []
if config.startswith("shim:"):
shim = True
config = config[5:]
else:
shim = False
if config.startswith("which:"):
if not aliases:
print(f"warning: `which` used without `alises`, skipping")
return
config = config[6:]
result = shutil.which(config)
if not result:
print(f"warning: no match for \"which:{config}\"")
return
target = result.strip()
else:
files = glob.glob(config, root_dir=portable_dir, recursive=True)
if not files:
print(f"warning: no match for \"{config}\"")
return
if len(files) > 1 and aliases:
raise ValueError("`alias` can only be used for glob patterns that match 1 file")
files = [os.path.join(portable_dir, x) for x in files]
for path in files:
if shim:
name = os.path.basename(path)
name, _ = os.path.splitext(name)
create_shim(symlink_dir, path, name)
else:
create_link(symlink_dir, path)
target = files[0]
create_alias(symlink_dir, target, aliases, shim)
def link(force):
extra_dir = find_extra_dir()
symlink_dir = os.path.join(extra_dir, "symlink")
if force:
if os.path.exists(symlink_dir):
shutil.rmtree(symlink_dir)
os.makedirs(symlink_dir)
portable_dir = os.path.join(extra_dir, "portable")
link_file = os.path.join(portable_dir, "link")
if not os.path.exists(link_file):
print(f"warning: link file {link_file} doesn't exist, skipping")
return
with open(link_file, "r", encoding="utf-8") as f:
for line in f:
add_link(symlink_dir, portable_dir, line.strip())
if __name__ == "__main__":
force = len(sys.argv) > 1 and sys.argv[1] == "--force"
link(force)