-
Notifications
You must be signed in to change notification settings - Fork 0
/
evilclone.py
executable file
·462 lines (360 loc) · 11.8 KB
/
evilclone.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2021-08-11
# @Filename: evilclone.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)
from __future__ import annotations
import os
import re
import subprocess
import sys
from glob import glob
from shutil import rmtree
from typing import Tuple, cast
try:
import click
except ImportError:
print("Click needs to be installed.")
sys.exit(1)
PRODUCT_DIR = "/home/sdss5/software/"
@click.command()
@click.argument("PRODUCT", type=str)
@click.option("--clone", is_flag=True, help="Clone the a repository.")
@click.option(
"-d",
"--dir",
type=click.Path(exists=True, file_okay=False),
help="Root of the products directory.",
)
@click.option(
"-m",
"--modulepath",
type=click.Path(exists=True, file_okay=False),
help="Root of the modulefiles path.",
)
@click.option("-b", "--branch", type=str, default="main", help="Branch to checkout.")
@click.option("-t", "--tag", type=str, help="Tag to checkout.")
@click.option("-e", "--environment", type=str, help="Name of the virtual environment.")
@click.option("-y", "--yes", is_flag=True, help="Accept default values.")
def evilclone(
product: str,
clone=False,
environment: str | None = None,
branch: str = "main",
tag: str | None = None,
dir: str | None = None,
modulepath: str | None = None,
yes=False,
):
"""Install operations software à la SDSS."""
if dir:
product_dir = str(dir)
else:
product_dir = PRODUCT_DIR
lmod_envvars = {}
repo_path = None
name = get_name(product, is_repo=clone)
if clone:
product = get_repo_path(product)
environment = create_environment(
environment,
product,
is_repo=True,
branch=branch,
tag=tag,
name=name,
yes=yes,
)
repo_path = clone_repo(
product,
environment,
product_dir=product_dir,
branch=branch,
tag=tag,
yes=yes,
)
install_repo(repo_path, environment, yes=yes)
else:
environment = create_environment(
environment,
product,
is_repo=False,
name=name,
yes=yes,
)
click.echo(click.style("pip-installing product.", fg="blue"))
run_with_pyenv(f"pip install {product}", environment)
create_modulefile(
product,
environment,
is_repo=clone,
branch=branch,
tag=tag,
name=name,
repo_path=repo_path,
envvars=lmod_envvars,
modulepath=modulepath,
yes=yes,
)
def fail(msg: str | None = None):
if msg:
click.echo(click.style(msg, fg="red"))
raise click.Abort()
def run(command: str, shell=True, cwd=None) -> str:
"""Runs a command in a shell and return the stdout."""
cmd = subprocess.run(command, shell=shell, capture_output=True, cwd=cwd)
if cmd.returncode != 0:
fail(f"Command {command} failed with error: {cmd.stderr.decode()}")
return cmd.stdout.decode()
def run_with_pyenv(command: str, environment: str, **kwargs):
"""Runs a command in a valid pyenv environment."""
# This is a hack but it seems to be necessary when running in a subprocess.
env_path = get_env_path(environment)
full_command = (
'eval "$(pyenv init --path -)" && '
'export PATH="$PYENV_ROOT/bin:$PATH" && '
'export PATH="$PYENV_ROOT/shims:$PATH" && '
'eval "$(pyenv init -)" && '
f"pyenv shell {environment} && "
f"export VIRTUAL_ENV='{env_path}' && "
f"{command}"
)
run(full_command, **kwargs)
def yn(msg: str, default="y", yes=False) -> bool:
"""Yes/no prompt."""
if yes:
return True
res = click.prompt(
msg,
type=click.Choice(["Y", "n"], case_sensitive=False),
default=default.upper(),
show_choices=False,
value_proc=lambda x: x.lower(),
)
return True if res == "y" else False
def get_name(product: str, is_repo=False) -> str:
"""Prompts for the product name."""
if not is_repo:
name, _ = get_product_parts(product)
if name.startswith("sdss-"):
name = name[5:]
else:
name = product.split("/")[-1]
name = click.prompt("Product name", default=name)
return name
def get_repo_path(product: str):
"""Returns the path to the repository."""
if product.startswith("http"):
fail("Repositories must use git@github.com paths.")
if not product.startswith("git@"):
product = "git@github.com:" + product
return product
def get_product_parts(product: str) -> Tuple[str, str]:
"""Split dependency specification into name and version."""
match = re.match(r"([a-zA-Z0-9_-]+)[><=~!]*([0-9.ab]*)", product)
assert match and (match.group(2) is None or match.group(2) != "")
return cast(Tuple[str, str], match.groups())
def create_environment(
environment: str | None,
product: str,
is_repo: bool = False,
branch: str = "main",
tag: str | None = None,
name: str | None = None,
yes: bool = False,
) -> str:
"""Creates the virtual environment."""
if tag is not None:
branch = tag
if environment is None:
if is_repo:
branch_safe = branch.replace("/", "_")
environment = product.split("/")[-1] + "-" + branch_safe
else:
auto_name, version = get_product_parts(product)
name = name or auto_name
if version:
environment = name + "-" + version
else:
environment = name
environment = click.prompt("Environment name", default=environment)
if not environment:
fail()
pyenv_versions = run("pyenv versions --bare")
versions = list(map(lambda x: x.strip(), pyenv_versions.splitlines()))
pyenv_global = run("pyenv global").strip()
if environment in versions:
if yn("Environment already exists. Use it?", yes=yes):
return environment
else:
fail()
else:
base_version = click.prompt("Base version", default=pyenv_global)
click.echo(
click.style(
f"Creating virtual environment {environment}.",
fg="blue",
)
)
run(f"pyenv virtualenv {base_version} {environment}")
run_with_pyenv("pip install -U pip setuptools wheel", environment)
return environment
def clone_repo(
repo_url: str,
environment: str,
product_dir: str = PRODUCT_DIR,
branch: str = "main",
tag: str | None = None,
yes: bool = False,
):
"""Clones a repo."""
is_tag = False
if tag is not None:
branch = tag
is_tag = True
name = repo_url.split("/")[-1]
default_path = os.path.join(product_dir, name, branch.replace("/", "_"))
if yes:
path = default_path
else:
path = click.prompt("Path for cloned repository?", default=default_path)
if os.path.exists(path):
if yn("Path already exists. Use it?"):
return path
else:
fail()
click.echo(click.style("Cloning repository.", fg="blue"))
run(f"git clone {repo_url} {path}")
os.chdir(path)
branch_output = run("git status --branch --porcelain")
match = re.match(r"## (.+)\.\.\..+", branch_output)
if not match:
fail("Cannot parse current branch")
current_branch = match.groups()[0]
if current_branch != branch:
if is_tag:
run(f"git checkout -b {branch} {tag}")
else:
run(f"git checkout -b {branch} origin/{branch}")
with open(".python-version", "w") as f:
f.write(environment)
if is_tag:
rmtree(os.path.join(path, ".git"))
return path
def get_env_path(environment: str):
"""Get the environment path."""
python_versions = run("pyenv versions --bare --skip-aliases").splitlines()
env_path = None
for version in python_versions:
if environment in version:
env_path = os.path.join(os.environ["PYENV_ROOT"], "versions", version)
if env_path is None:
fail("Cannot find environment.")
return env_path
def install_repo(repo_path: str, environment: str, yes=False) -> bool:
"""Install the repository."""
os.chdir(repo_path)
files = glob("*")
if "pyproject.toml" in files:
prompt = "Install with poetry (1), pip (2), or no install (3)?"
setup_prompt = click.prompt(prompt, default="1")
if setup_prompt == "1":
command = "poetry install"
elif setup_prompt == "2":
command = "pip install -e ."
elif setup_prompt == "3":
return False
else:
fail()
elif "setup.py" in files:
command = "pip install -e ."
if not yn("Pip install repository?", yes=yes):
return False
else:
if not yn("Cannot find setup.py or pyproject.py. Continue?", yes=yes):
fail()
else:
return False
click.echo(click.style("Running installation.", fg="blue"))
run_with_pyenv(command, environment)
return True
def create_modulefile(
product: str,
environment: str,
is_repo: bool = False,
branch: str = "main",
tag: str | None = None,
name: str | None = None,
repo_path: str | None = None,
envvars={},
modulepath: str | None = None,
yes: bool = False,
):
"""Creates the modulefile."""
if not yn("Create modulefile?", yes=yes):
fail()
if modulepath is None:
modulepath = "/home/sdss5/software/modulefiles"
if is_repo:
name = name or product.split("/")[-1]
version = tag or branch
else:
if name:
_, version = get_product_parts(product)
else:
name, version = get_product_parts(product)
assert name
modulepath = os.path.join(modulepath, name, version + ".lua")
if yes is False:
modulepath = click.prompt("Module path", default=modulepath)
if not modulepath:
fail()
if os.path.exists(modulepath):
fail("Module path already exists.")
dep_prompt = click.prompt(
"Space-separated modules to load with this product",
default="",
)
if not dep_prompt:
deps = None
else:
deps = dep_prompt.split()
if repo_path:
if yn("Add PYTHONPATH?", default="n"):
pythonpath = click.prompt("PYTHONATH to use", default="repo_path")
envvars["PYTHONPATH"] = pythonpath
if yn("Add to PATH?", default="n") and repo_path:
default_path = os.path.join(repo_path, "bin")
path = click.prompt("PYTHONATH to use", default=default_path)
envvars["PATH"] = path
lines = [f"conflict('{name}')", ""]
if deps:
for dep in deps:
lines += [f"load('{dep}')", f"prereq('{dep}')", ""]
if is_repo and repo_path:
lines += [f"setenv('{name.upper()}_DIR', '{repo_path}')"]
for env in envvars:
if env.endswith("PATH") and not os.path.exists(envvars[env]):
continue
lines += [
f"prepend_path{{'{env.upper()}', '{envvars[env]}', "
"delim=':', priority='0'}"
]
if lines[-1] != "":
lines += [""]
lines += [f"setenv('PYENV_VERSION', '{environment}')", ""]
dirpath = os.path.realpath(os.path.dirname(modulepath))
os.makedirs(dirpath, exist_ok=True)
with open(modulepath, "w") as modulefile:
data = "\n".join(lines)
modulefile.write(data)
click.echo(click.style(f"Created modulefile {modulepath}.", fg="blue"))
if yn("Make default?"):
dest = os.path.join(dirpath, "default")
if os.path.exists(dest):
os.unlink(dest)
os.symlink(modulepath, dest)
if __name__ == "__main__":
evilclone()