-
Notifications
You must be signed in to change notification settings - Fork 6
/
ui.py
297 lines (235 loc) · 7.86 KB
/
ui.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Manage the OCR pipeline, both remotely and locally
.. Authors:
Philippe Dessauw
philippe.dessauw@nist.gov
.. Sponsor:
Alden Dima
alden.dima@nist.gov
Information Systems Group
Software and Systems Division
Information Technology Laboratory
National Institute of Standards and Technology
http://www.nist.gov/itl/ssd/is
"""
from __future__ import division
import logging
from os import listdir
import os
from apputils.config import load_config
from apputils.fileop import create_directories
from denoiser import Denoiser
from os.path import join, isdir, exists, abspath
from fabric.contrib.console import confirm
from fabric.contrib.project import upload_project
from fabric.contrib.files import exists as fab_exists
from fabric.decorators import task, roles, runs_once
from fabric.api import env, run, local, cd
from fabric.operations import sudo
from fabric.tasks import Task, execute
from fabric.state import output
from shutil import copytree, rmtree
# Initialize the app_config variable
load_config("conf/app.yaml", os.environ['ROOT'])
from apputils.config import app_config
logger = logging.getLogger('app')
# Hosts configurations
# env.roledefs = app_config["machines"]
# env.hosts = [ip for ips in env.roledefs.values() for ip in ips]
# Extra default configuration
env.warn_only = True
output.update({
"warnings": False,
"running": False
})
# Global variable to know if the execution is done locally or remotely
local_exec = False
# @task
# def install():
# """Install the pipeline on the specified cluster
# """
# logger.debug("Installing pipeline...")
#
# local_root = os.environ['ROOT']
# remote_root = app_config['root']
#
# if local_exec:
# if abspath(local_root) == abspath(remote_root):
# logger.error("Source and destination folder are the same")
# exit(1)
#
# if exists(remote_root):
# if confirm("Existing data will be deleted. Do you want to proceed anyway?", default=False):
# rmtree(remote_root)
# else:
# logger.error("Pipeline destination folder already exists")
# exit(2)
#
# copytree(local_root, remote_root)
# local(remote_root+'/utils/install.sh')
# else:
# if app_config["use_sudo"]:
# run_fn = sudo
# else:
# run_fn = run
#
# if not fab_exists(remote_root):
# logging.debug("Building remote directory...")
# run_fn("mkdir -p "+remote_root)
# else:
# if not confirm("Existing data will be deleted. Do you want to proceed anyway?", default=False):
# logger.error("Pipeline destination folder already exists")
# exit(2)
#
# logging.debug("Uploading project...")
# upload_project(
# local_dir=local_root,
# remote_dir=remote_root,
# use_sudo=app_config["use_sudo"]
# )
#
# if run_fn(remote_root+"/utils/auth.sh").failed:
# logger.error("An error occured with modifying the right for the pipeline")
# exit(3)
#
# if run(remote_root+"/utils/install.sh").failed:
# logger.error("An error occured with the install script")
# exit(4)
#
# logger.info("Pipeline successfully installed")
@task
def init():
"""Initialize the app (available for localhost only)
"""
logger.debug("Initializing app...")
if not local_exec:
logger.error("Pipeline can only be initialized locally")
exit(1)
# Create project tree
create_directories(app_config["dirs"])
logger.info("App initialized")
@task
def create_models(dataset_dir):
"""Initialize the app (available for localhost only)
Parameters:
dataset_dir (:func:`str`): Path to the training set
"""
logger.debug("Creating models...")
if not local_exec:
logger.error("Models can only be generated locally")
exit(1)
# Modify the configuration for local execution
app_config['root'] = os.environ['ROOT']
# Generate inline models and train classifier
denoiser = Denoiser(app_config)
if not exists(dataset_dir) or not isdir(dataset_dir):
logger.error(dataset_dir+" is not a valid directory")
exit(2)
dataset = [join(dataset_dir, f) for f in listdir(dataset_dir)]
denoiser.generate_models(dataset)
logger.info("Inline models generated")
denoiser.train(dataset)
logger.info("Classifier trained")
@task
def check():
"""Check that the 3rd party software are all installed
"""
base_dir = "utils/check"
scripts = [join(base_dir, sc) for sc in listdir(base_dir) if sc.endswith(".sh")]
for script in scripts:
launch_script(script)
# @task
# @runs_once
# def start_pipeline():
# """Start the conversion process across the platform.
# """
# execute(start_master)
# execute(start_slave)
@task
@roles("master")
def start_master():
"""Start the master server on the local machine
"""
launch_script("utils/run-wrapper.sh", ["--master", ("> %s/master.log" % app_config["dirs"]["logs"])], True)
@task
@roles("slaves")
def start_slave():
"""Start a slave on the local machine
"""
launch_script("utils/run-wrapper.sh", ["--slave", ("> %s/slave.log" % app_config["dirs"]["logs"])], True)
def launch_script(script_name, script_opts=list(), background=False):
"""Launch any script you specify
Parameters:
script_name (:func:`str`): Path of the script to run
script_opts (:func:`str`): Options to pass to the script
background (bool): Whether to launch the script in background
"""
if local_exec:
root_dir = os.environ['ROOT']
else:
root_dir = app_config['root']
command = join(root_dir, script_name)+" "+" ".join(script_opts)
logger.debug("Execute: "+command)
with cd(root_dir):
if local_exec:
if background:
local(command)
return
if local(command).failed:
logger.error("Command failed "+command)
exit(1)
else:
if background:
run(command)
return
if run(command).failed:
logger.error("Command failed "+command)
exit(1)
def print_help(light=False):
"""Print the help message
Parameter
light (bool): If true, only print the usage
"""
print "Usage: python ui.py [-h] command [args [args ...]]"
if light:
return
print "Commands: "
default_func_help = "No description available"
for func in functions.keys():
help_str = "* "+func+" "*(20-len(func))
if func in functions_help.keys():
help_str += functions_help[func]
else:
help_str += default_func_help
print help_str
print ""
print "Options:"
print_opts = ', '.join(help_opts)
print print_opts+" "*(22-len(print_opts))+"Print this help message"
def setup_local_exec():
"""Set the local_exec variable to true
"""
global local_exec
local_exec = True
if __name__ == "__main__":
import sys
args = sys.argv[1:]
setup_local_exec()
help_opts = ["--help", "-h"]
functions = {k: v for k, v in locals().items() if isinstance(v, Task) and v.__module__ == "__main__"
and k != "print_help"}
functions_help = {k: v.__doc__.split("\n")[0] for k, v in functions.items() if v.__doc__ is not None}
if len(args) == 0:
logger.error("No function specified")
print_help(True)
exit(1)
if len(args) == 1 and args[0] in help_opts:
print_help()
exit(0)
if args[0] not in functions.keys():
logger.fatal("Command "+args[0]+" unknown")
print_help(True)
exit(1)
if len(args) > 1:
functions[args[0]](*args[1:])
else:
functions[args[0]]()