-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.py
executable file
·337 lines (278 loc) · 9.37 KB
/
install.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
"""
Installs "nvim" Micromamba Environ, Containing NVim and all tools like compilers.
- The tools currently tested only on Linux, i.e. intended for server setups
- We install all into a micromamba environment "nvim"
- We use zig as C and C++ compiler
REQUIREMENT: micromamba command available
On linux and OSX:
"${SHELL}" <(curl -L micro.mamba.pm/install.sh)
ARGUMENTS:
- s|status: Status
- i|install: Install
- clean: Remove all existing nvim, except this config (asks for confirmation)
"""
import platform, os, shutil, sys, json
PKG = [
'bat',
'exa',
'fzf',
'git',
'lazygit',
'make',
'unzip',
'zig',
['fd', 'fd-find'],
['npm', 'nodejs'],
['python3.9', 'python==3.9'],
['rg', 'ripgrep'],
]
req_in_env = ['npm', 'python3.9']
editor_pips = ['pynvim', 'blue', 'isort', 'requests']
exists = os.path.exists
dirname = os.path.dirname
abspath = os.path.abspath
H = os.environ.get('HOME', '')
MM = 'micromamba'
RP = os.environ.get('MAMBA_ROOT_PREFIX')
def read_file(fn):
try:
with open(fn) as fd:
return fd.read()
except:
return ''
def write_file(fn, s, chmod=None):
with open(fn, 'w') as fd:
fd.write(s)
if chmod:
os.chmod(fn, chmod)
def system(cmd, no_fail=False, silent=False):
cmd += ' 1>&2'
if silent:
cmd += ' 2>/dev/null'
else:
info(cmd)
err = os.system(cmd)
if err and not no_fail:
if not silent:
info('failed')
sys.exit(1)
return err
def info(msg, **kw):
kw = ', '.join([f'{k}:{v}' for k, v in kw.items()])
print(msg, kw, file=sys.stderr)
def download_file(url, fn):
fn = abspath(fn)
if exists(fn):
info('exists already', fn=fn)
d = dirname(fn)
os.makedirs(d, exist_ok=True)
info('Downloading', url=url, to=fn)
err = os.system(f'wget "{url}" -O "{fn}"')
if err:
system(f'curl -L "{url}" > "{fn}"')
_ = lambda i: i if isinstance(i, list) else [i, i]
mamba_pkgs = sorted([_(i) for i in PKG])
class nvim:
url_nvim = 'https://github.com/neovim/neovim/releases/download/stable/nvim.appimage'
vi = lambda: f'{mamba.bindir()}/vi'
@classmethod
def status(t):
return {
'installed': exists(t.vi()),
'exe': t.vi(),
}
@classmethod
def install(t):
d_bin = mamba.bindir()
os.makedirs(d_bin, exist_ok=True)
os.chdir(d_bin)
if not exists('vi'):
system('rm -rf squashfs-root vi nvim.appimage')
download_file(t.url_nvim, 'nvim.appimage')
os.system(
'chmod u+x nvim.appimage && ./nvim.appimage --appimage-extract'
)
# https://github.com/nvim-treesitter/nvim-treesitter/issues/5098#issuecomment-1696643687
# zig is the better compiler. E.g.
s = f"""#!/usr/bin/env sh
export CC="zigcc"
export CXX="zigc++"
fn="$(readlink -vf "$0")" # micromamba/env/nvim/bin
export PATH="$PATH:$(dirname "$fn")" # last so that conda python's are found first
{d_bin}/squashfs-root/usr/bin/nvim "$@"
"""
vi = d_bin + '/vi'
write_file(vi, s, chmod=0o755)
s = """#!/usr/bin/env sh
echo "zig XX : $*" >> /tmp/nvim_zig.log
zig XX "$@"
"""
write_file(d_bin + '/zigcc', s.replace(' XX ', ' cc '), chmod=0o755)
write_file(d_bin + '/zigc++', s.replace(' XX ', ' c++ '), chmod=0o755)
info(f'{vi} present')
binlink('vi')
return {'nvim': t.status()}
def have(cmd):
cmd = f'type {cmd}'
if exists(mamba.bindir()):
cmd = f'{MM} run -n nvim {cmd}'
return os.popen(f'{cmd} 2>/dev/null').read().strip()
def binlink(cmd):
src = f'{mamba.bindir()}/{cmd}'
if not exists(src):
return
dest = H + f'/.local/bin/{cmd}'
os.unlink(dest) if exists(dest) else 0
info('Linking', src=src, dest=dest)
os.symlink(src, dest)
class mamba:
def bindir():
return f'{mamba.envdir()}/bin'
def envdir():
return f'{RP}/envs/nvim'
def status():
return {k[0]: have(k[0]) for k in mamba_pkgs}
def create_env():
if not exists(mamba.envdir()):
system(f'{MM} create -y -n nvim')
def install():
ret, inst_cmd = [], []
for cmd, pkg in mamba_pkgs:
if not have(cmd):
ret.append(pkg)
inst_cmd.append(cmd)
if cmd in req_in_env and not exists(mamba.bindir() + f'/{cmd}'):
ret.append(pkg)
if ret:
system(f'{MM} install -c conda-forge -n nvim -y {" ".join(ret)}')
_ = ' '.join(editor_pips)
system(f'{mamba.bindir()}/pip install {_}')
[binlink(cmd) for cmd in inst_cmd if not cmd in req_in_env]
[binlink(cmd) for cmd in editor_pips]
_ = 'installed'
return {'tools': {_: ret, 'have': [i[0] for i in mamba_pkgs]}}
def get_installed():
db = H + '/.local/bin'
rm = [
f'{H}/.local/share/nvim',
f'{H}/.cache/nvim',
f'{mamba.envdir()}',
]
rm = [r for r in rm if exists(r)]
sl = []
os.makedirs(db, exist_ok=True)
for k in os.listdir(db):
f = db + f'/{k}'
if os.path.islink(f) and mamba.bindir() in os.readlink(f):
sl.append(f)
return {'installed': rm, 'symlinks': sl}
class Action:
def status():
system('type micromamba')
r = {'nvim': nvim.status(), 'tools installed': mamba.status()}
r.update(get_installed())
return r
def install():
if not Action.status()['nvim']['installed']:
[mamba.create_env(), nvim.install()]
mamba.install()
info('running nvim. libfzf.so error is ok, will be compiled')
system(f'{mamba.bindir()}/vi --headless -c quitall')
post.make_remark_work_globally()
[post.source_shell_helpers(f) for f in (H + '/.bashrc', H + '/.zshrc')]
return Action.status()
def clean():
m = get_installed()
rm = m['installed'] + m['symlinks']
if not rm:
print('all clean')
return
if sys.stdin.isatty():
k = '\n- ' + '\n- '.join(rm) + '\n'
if not 'y' in input(f'Remove {k} [y|N]? ').lower():
print('unconfirmed')
sys.exit(1)
for d in rm:
system(f'rm -rf "{d}"')
class post:
"""Post install functions"""
def make_remark_work_globally():
# https://github.com/orgs/remarkjs/discussions/960#discussioncomment-6848513
# otherwise we have * as list marks and frontmatter screwups:
fn = H + '/.remarkrc.yml'
if not exists(fn):
info('writing', fn=fn)
s = 'settings:\n bullet: "-"\n rule: "-"\n'
write_file(fn, s)
system(f'cd $HOME; PATH="{mamba.bindir()}:$PATH"; npm install remark')
write_file(
H + '/node_modules/README.md',
'Required for remark to work globally',
)
def source_shell_helpers(fn: str):
s = read_file(fn)
if not s or 'shell_helpers' in s:
return
h = H + '/.config/nvim/shell_helpers'
write_file(fn, s + f'\nsource "{h}"\n')
class binenv:
archi = platform.machine().replace('x86_', 'amd').lower()
url_binenv = f'https://github.com/devops-works/binenv/releases/download/v0.19.8/binenv_{platform.uname()[0]}_{archi}'
have_wget = os.system('type wget 2>/dev/null 1>/dev/null') == 0
@staticmethod
def die(msg):
print(msg)
sys.exit(1)
@staticmethod
def run(cmd, diemsg=None):
print(cmd, file=sys.stderr)
err = os.system(cmd)
if not diemsg or not err:
return err
binenv.die(diemsg)
@staticmethod
def download(url, fn, chmod=None):
if os.path.exists(fn) and os.stat(fn).st_size == 0:
os.unlink(fn)
if not os.path.exists(fn):
os.makedirs(os.path.dirname(fn), exist_ok=True)
if binenv.have_wget:
cmd = f'wget -q "{url}" -O "{fn}"'
else:
cmd = f'curl -s "{url}" -o "{fn}"'
binenv.run(cmd, f'could not download {url}')
if chmod:
os.chmod(fn, chmod)
@staticmethod
def bootstrap(bindir=os.environ['HOME'] + '/.local/bin'):
binenv.die(f'Not found: {bindir}') if not os.path.exists(bindir) else 0
os.environ['BINENV_BINDIR'] = bindir
os.environ['BINENV_LINKDIR'] = bindir
tmp = f'/tmp/{os.environ["USER"]}/binenv'
os.makedirs(os.path.dirname(tmp), exist_ok=True)
binenv.download(binenv.url_binenv, tmp, chmod=0o755)
binenv.run(f'{tmp} update')
binenv.run(f'{tmp} install binenv')
def help():
print(__doc__)
pkgs = ' '.join([c[0] for c in mamba_pkgs])
_ = '\nPackages we will put into this nvim environ, if not found in current(!) $PATH:\n'
print(_ + pkgs + '\n')
def main():
os.chdir(dirname(__file__))
a = sys.argv[1:]
if not a or '-h' in a or '--help' in a:
sys.exit(help())
if not RP or not exists(RP):
print('No $MAMBA_ROOT_PREFIX')
sys.exit(help())
if 's' in a or 'status' in a:
return print(json.dumps(Action.status(), indent=4))
if 'i' in a or 'install' in a:
return Action.install()
if 'clean' in a:
return Action.clean()
print(sys.exit(help()))
if __name__ == '__main__':
main()