forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.py
66 lines (49 loc) · 1.65 KB
/
system.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
# -----------------------------------------------------------------------------
# Copyright (c) Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# -----------------------------------------------------------------------------
"""
"""
from __future__ import annotations
# Standard library imports
import os
import sys
from subprocess import PIPE, STDOUT, run as stdlib_run
from typing import Any
# Bokeh imports
from .logger import LOG
from .ui import shell
__all__ = ("System",)
class System:
""""""
def __init__(self, dry_run: bool = False) -> None:
self.dry_run: bool = dry_run
self._pushd_state: list[str] = []
def run(self, cmd: str, **kw: Any) -> str:
""""""
envstr = " ".join(f"{k}={v}" for k, v in kw.items()) + min(len(kw), 1) * " "
LOG.record(shell(f"{envstr}{cmd}"))
env = dict(os.environ)
env.update(kw)
if self.dry_run:
return ""
result = stdlib_run(cmd, shell=True, stdout=PIPE, stderr=STDOUT, text=True, env=env)
if result.returncode != 0:
raise RuntimeError(*result.stdout.strip().split("\n"))
return result.stdout
def abort(self) -> None:
""""""
sys.exit(1)
def cd(self, new_dir: str) -> None:
""""""
os.chdir(new_dir)
LOG.record(shell(f"cd {new_dir} # [now: {os.getcwd()}]"))
def pushd(self, new_dir: str) -> None:
""""""
self._pushd_state.append(os.getcwd())
self.cd(new_dir)
def popd(self) -> None:
""""""
self.cd(self._pushd_state.pop())