-
Notifications
You must be signed in to change notification settings - Fork 2
/
shell.py
82 lines (62 loc) 路 2.07 KB
/
shell.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
import argparse
from galileo.shell.shell import *
def _init():
import redis
rds = redis.Redis(
host=os.getenv('galileo_redis_host', 'localhost'),
port=int(os.getenv('galileo_redis_port', 6379)),
decode_responses=True
)
init_module(rds, __name__)
def _init_standalone():
from tempfile import mktemp
from threading import Thread
from shutil import rmtree
from galileo.worker.context import Context
from galileo.worker.daemon import WorkerDaemon
ctx = Context()
# prepare embedded redis instance
filename = mktemp(prefix='galileo_shell_', suffix='.db')
os.environ['galileo_redis_host'] = 'file://' + filename # Context resolves this correctly using redislite
rds = ctx.create_redis()
rds.randomkey()
# init this module with global variables
init_module(rds, __name__)
# start worker daemon
global eventbus
daemon = WorkerDaemon(ctx=ctx, eventbus=eventbus)
t_daemon = Thread(target=daemon.run, daemon=True)
t_daemon.start()
# register cleanup method
def cleanup():
daemon.close()
eventbus.close()
# remove redislite db
rds.shutdown()
os.remove(filename)
os.remove(rds.redis_configuration_filename)
os.remove(rds.settingregistryfile)
rmtree(rds.redis_dir)
atexit.register(cleanup)
def _main():
parser = argparse.ArgumentParser()
parser.add_argument('--standalone', required=False, action="store_true",
help='also start an embedded redis server and a worker instance')
args = parser.parse_args()
if args.standalone:
print('running in standalone mode')
_init_standalone()
else:
_init()
if is_interactive:
sys.ps1 = prompt
print(banner)
print('Welcome to the galileo shell!')
print('')
print('Type `usage` to list available functions')
print('')
else:
sys.ps1 = ''
if __name__ == '__main__':
# python -i shell.py will execute to here and then drop into the interactive shell
_main()