Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
Latest commit
94 lines (78 loc) · 2.53 KB
/
Copy pathcore.py
File metadata and controls
94 lines (78 loc) · 2.53 KB
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
importargparse
importos
importre
importsignal
importsubprocess
importsys
frompathlibimportPath
fromutils.watchimportwatch
defexecute(command):
p=subprocess.Popen(command, shell=True)
try:
p.wait()
exceptKeyboardInterrupt:
try:
os.kill(p.pid, signal.SIGINT)
exceptOSError:
pass
p.wait()
defprepare_parser():
parser=argparse.ArgumentParser(
description='The core script of experiment management.')
subparsers=parser.add_subparsers(dest='command')
add_env_parser(subparsers)
add_watch_parser(subparsers)
returnparser
defadd_env_parser(subparsers):
parser=subparsers.add_parser(
'env', description='Environment Management.')
parser.add_argument(
'action', nargs='?', choices=['prepare', 'enter', 'stop'],
default='enter')
parser.add_argument('-b', '--build', action='store_true', default=False)
parser.add_argument('--root', action='store_true', default=False)
defenv(args):
execute('echo "UID=$(id -u)\nGID=$(id -g)\nUSER_NAME=$(whoami)" > ./.env')
ifargs.action=='prepare':
command='docker-compose up -d'
ifargs.build:
command+=' --build'
elifargs.action=='enter':
ifargs.root:
command='docker-compose exec -u root playground zsh'
else:
command='docker-compose exec playground zsh'
elifargs.action=='stop':
command='docker-compose stop'
else:
raiseNotImplementedError
execute(command)
defadd_watch_parser(subparsers):
"""
This script will run <command> when and only when the condition is true.
Usage:
# python core.py watch <type>:<target> "<command>" [--gap <seconds>] [--reverse]
For example:
1. Waiting until the target file exists:
# python watch.py f:test "ls -l" --gap 1
2. Waiting until the process pid:1 quits:
# python watch.py p:1 "ls -l" --gap 1
"""
parser=subparsers.add_parser('watch', description='Wather.')
parser.add_argument("target")
parser.add_argument("command")
parser.add_argument("--gap", default=300, type=int)
parser.add_argument("--reverse", action="store_true")
if__name__=="__main__":
parser=prepare_parser()
args=parser.parse_args()
ifargs.command=='train':
train(args)
elifargs.command=='test':
test(args)
elifargs.command=='env':
env(args)
elifargs.command=='watch':
watch(args)
else:
pass