-
Notifications
You must be signed in to change notification settings - Fork 1
/
dodo.py
193 lines (169 loc) · 5.18 KB
/
dodo.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
This module defines the tasks that can be executed using `surround run [task name]`
"""
import os
import sys
import subprocess
from surround import load_config
from tree_diff.config import Config
CONFIG = load_config(name="config", config_class=Config)
DOIT_CONFIG = {'verbosity':2, 'backend':'sqlite3'}
PACKAGE_PATH = os.path.basename(CONFIG["package_path"])
IMAGE = "%s/%s:%s" % (CONFIG["company"], CONFIG["image"], CONFIG["version"])
IMAGE_JUPYTER = "%s/%s-jupyter:%s" % (CONFIG["company"], CONFIG["image"], CONFIG["version"])
DOCKER_JUPYTER = "Dockerfile.Notebook"
PARAMS = [
{
'name': 'args',
'long': 'args',
'type': str,
'default': ""
}
]
def task_status():
"""Show information about the project such as available runners and assemblers"""
return {
'actions': ["%s -m %s status=1" % (sys.executable, PACKAGE_PATH)]
}
def task_build():
"""Build the Docker image for the current project"""
cmd = ['poetry install &&', # Installing dependencies
'poetry export -f requirements.txt --output requirements.txt --dev --without-hashes &&',
'docker build --tag=%s .' % IMAGE]
return {
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_remove():
"""Remove the Docker image for the current project"""
return {
'actions': ['docker rmi %s %s -f' % (IMAGE, IMAGE_JUPYTER)],
'params': PARAMS
}
def task_dev():
"""Run the main task for the project"""
cmd = [f"poetry run python -m {PACKAGE_PATH}"]
return {
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_interactive():
cmd =[f"poetry "]
def task_interactive_docker():
"""Run the Docker container in interactive mode"""
def run():
cmd = [
'docker',
'run',
'-it',
'--rm',
'-w',
'/app',
'--volume',
'%s/:/app' % CONFIG['volume_path'],
IMAGE,
'bash'
]
process = subprocess.Popen(cmd, encoding='utf-8')
process.wait()
return {
'actions': [run]
}
def task_prod():
"""Run the main task inside a Docker container for use in production """
return {
'actions': ["docker run %s python3 -m %s %s" % (IMAGE, PACKAGE_PATH, "%(args)s")],
'task_dep': ["build"],
'params': PARAMS
}
def task_train():
"""Run training mode inside the container"""
output_path = CONFIG["volume_path"] + "/output"
data_path = CONFIG["volume_path"] + "/input"
cmd = [
"docker run",
"--volume \"%s\":/app/output" % output_path,
"--volume \"%s\":/app/input" % data_path,
IMAGE,
"python3 -m simple_example mode=train %(args)s"
]
return {
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_batch():
"""Run batch mode inside the container"""
output_path = CONFIG["volume_path"] + "/output"
data_path = CONFIG["volume_path"] + "/input"
cmd = [
"docker run",
"--volume \"%s\":/app/output" % output_path,
"--volume \"%s\":/app/input" % data_path,
IMAGE,
"python3 -m simple_example mode=batch %(args)s"
]
return {
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_train_local():
"""Run training mode locally"""
cmd = [
"poetry install &&", # Installing dependencies
"source `poetry env info --path`/bin/activate &&", # Entering virtual env
"poetry run",
sys.executable,
"-m %s" % PACKAGE_PATH,
"mode=train",
"%(args)s",
"&& deactivate" # Exiting virtual env
]
return {
'basename': 'trainLocal',
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_batch_local():
"""Run batch mode locally"""
cmd = [
"poetry install &&", # Installing dependencies
"source `poetry env info --path`/bin/activate &&", # Entering virtual env
"poetry run",
sys.executable,
"-m %s" % PACKAGE_PATH,
"mode=batch",
"%(args)s",
"&& deactivate" # Exiting virtual env
]
return {
'basename': 'batchLocal',
'actions': [" ".join(cmd)],
'params': PARAMS
}
def task_build_jupyter():
"""Build the Docker image for a Jupyter Lab notebook"""
cmd = ['poetry install &&', # Installing dependencies
'poetry export -f requirements.txt --output requirements.txt --dev --without-hashes &&',
'docker build --tag=%s . -f %s' % (IMAGE_JUPYTER, DOCKER_JUPYTER)]
return {
'basename': 'buildJupyter',
'actions': [" ".join(cmd)],
'task_dep': ['build'],
'params': PARAMS
}
def task_jupyter():
"""Run a Jupyter Lab notebook"""
cmd = [
"docker",
"run",
"-itp",
"8888:8888",
'-w',
'/app',
"--volume",
"\"%s/\":/app" % CONFIG["volume_path"],
IMAGE_JUPYTER
]
return {
'actions': [" ".join(cmd)],
}