-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf.py
69 lines (59 loc) · 2.04 KB
/
sf.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
# shapeflow CLI entry point
import sys
import logging
import contextlib
import subprocess
def main():
# import shapeflow essentials
# - may raise ModuleNotFoundError if called from system interpreter
# -> attempt to call from virtual environment (see below)
# - suppress logs for cleaner --version / --help output
with _suppress_logs():
import shapeflow.cli
shapeflow.cli.Sf()
@contextlib.contextmanager
def _suppress_logs():
"""Don't log anything in this context.
Note that `logging.root.manager.disable` is hacky and undocumented
https://gist.github.com/simon-weber/7853144
"""
previous_level = logging.root.manager.disable # noqa
logging.disable(logging.CRITICAL)
try:
yield
finally:
logging.disable(previous_level)
def _bootstrap_venv(method):
# if called directly
if not '--recursive-call' in sys.argv[1:]:
# try calling main()
try:
method()
except ModuleNotFoundError:
# if this fails, call this script ~ util/from_venv.py
# -> specify that it's a recursive call
try:
subprocess.check_call([
sys.executable, 'shapeflow/util/from_venv.py',
__file__, *sys.argv[1:] + ['--recursive-call']
])
except KeyboardInterrupt:
# clean exit on Ctrl+C
pass
except subprocess.CalledProcessError as e:
# exit with error code on subprocess exceptions
exit(e.returncode)
except Exception:
# re-raise any other exceptions
raise
else:
# remove `--recursive-call` argument, as it shouldn't be parsed
sys.argv.remove('--recursive-call')
try:
method()
except Exception as e:
# if still can't run main(), just exit
print(f"ERROR: {e.__class__.__name__}: {e}")
exit(1)
if __name__ == '__main__':
_bootstrap_venv(main)