-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-version.py
executable file
·80 lines (56 loc) · 2.05 KB
/
set-version.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego ([email protected])
# @Date: 2022-12-27
# @Filename: set-version.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)
from __future__ import annotations
import os
import os.path
import subprocess
import sys
try:
import click
except ImportError:
print("Click needs to be installed.")
sys.exit(1)
@click.command(name="set-version")
@click.argument("PRODUCT", type=str)
@click.argument("VERSION", type=str, required=False)
def set_version(product: str, version: str | None = None):
"""Sets a modulefile version as default."""
path = get_modulefile_path(product, version)
if version is None:
return
module_dir = os.path.dirname(path)
default = os.path.join(module_dir, "default")
if os.path.exists(default):
os.unlink(default)
os.symlink(path, default)
click.echo(click.style(f"Created default symlink {default}", fg="white"))
def run(command: str, shell=True, cwd=None) -> str | None:
"""Runs a command in a shell and return the stdout."""
# This seems necessary at LCO
if os.environ.get('OBSERVATORY', None) == "LCO":
command = 'source /home/sdss5/config/bash/00_lmod.sh && ' + command
cmd = subprocess.run(command, shell=shell, capture_output=True, cwd=cwd)
if cmd.returncode != 0:
return None
return cmd.stdout.decode(), cmd.stderr.decode()
def get_modulefile_path(product: str, version: str | None = None):
"""Gets the path to a modulefile."""
module = f"{product}/{version}" if version else product
result = run(f"module show {module}")
if result is None:
click.echo(click.style(f"Module {module} not found.", fg="red"))
raise click.Abort()
lines = result[1].splitlines()
path = lines[1].strip()[:-1]
if version:
click.echo(click.style(f"Module found at {path}", fg="white"))
else:
click.echo(click.style(f"Default module is {path}", fg="white"))
return path
if __name__ == "__main__":
set_version()