-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbiohub-cli.py
executable file
·307 lines (237 loc) · 8.61 KB
/
biohub-cli.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
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
"""
Biohub Command-Line Tool.
"""
import sys
import os
import os.path
import argparse
import django
from django.utils.functional import cached_property
from django.core.management import find_commands, load_command_class, call_command
from django.core.management.base import BaseCommand
# Resolve the base directory of biohub
current_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
biohub_base_dir = current_dir
# Ensure that biohub can be globally imported
try:
import biohub # noqa
except ModuleNotFoundError:
sys.path.insert(0, biohub_base_dir)
# Ensure that Mysql-python is patched and ready
from biohub import compat # noqa
from biohub.utils.path import modpath # noqa
def resolve(*paths):
return os.path.join(biohub_base_dir, *paths)
class ManagementUtility(object):
commands_mapping = {
'plugin': {
'new': 'newplugin',
'mkmigrations': 'mkpluginmigrations',
'migrate': 'migrateplugin',
'install': 'installplugin',
'remove': 'removeplugin'
},
'init': {
},
'installjob': {
},
'runserver': {
}
}
BIOHUB_PLUGINS_MODULE = 'biohub.core.plugins'
def setup(self):
"""
Set up django environment.
"""
os.environ['DJANGO_SETTINGS_MODULE'] = 'biohub.main.settings.dev'
os.environ.setdefault(
'BIOHUB_CONFIG_PATH',
resolve('config.json')
)
django.setup()
@cached_property
def available_commands(self):
"""
Returns a dict mappinng command names to their applications.
"""
# Built-in commands from `biohub.core.plugins`
commands = {
name: self.BIOHUB_PLUGINS_MODULE for name in
find_commands(
modpath('.'.join([self.BIOHUB_PLUGINS_MODULE, 'management'])),
)
}
return commands
def fetch_command(self, subcommand, command_sets=None):
"""
Tries to fetch the given subcommand. If it can't be found, prints a
message with the appropriate command called from the command line
(e.g. "biohub_cli").
"""
# Check if the subcommand is available
try:
app_name = self.available_commands[subcommand]
except KeyError:
sys.stderr.write(
"Unknown command: %r\nType '%s --help' for usage.\n"
% (subcommand, self.prog_name))
sys.exit(1)
if isinstance(app_name, BaseCommand):
klass = app_name
else:
klass = load_command_class(app_name, subcommand)
return klass
def execute(self):
"""
The main function of the tool.
"""
self.setup()
self.argv = sys.argv[:]
self.prog_name = os.path.basename(sys.argv[0])
parser = argparse.ArgumentParser(
prog=self.prog_name,
usage='%(prog)s <command> [<args>]',
description='Biohub CLI Tool')
parser.add_argument(
'command',
choices=self.commands_mapping,
help='Available commands: %s.' % ', '.join(self.commands_mapping)
)
# if no command provided
if len(self.argv) < 2:
parser.print_help()
sys.exit()
command = parser.parse_args(self.argv[1:2]).command
getattr(self, command)()
def plugin(self):
"""
The main function of plugin command.
"""
plugin_commands = self.commands_mapping['plugin']
parser = argparse.ArgumentParser(
prog=self.prog_name,
usage='%(prog)s plugin <subcommand> [<args>]')
parser.add_argument(
'subcommand',
choices=plugin_commands,
help='Available subcommands: %s.' % ', '.join(plugin_commands))
# if no subcommand provided
if not self.argv[2:]:
parser.print_help()
sys.exit()
subcommand = parser.parse_args(self.argv[2:3]).subcommand
self.fetch_command(
plugin_commands[subcommand]).run_from_argv(self.argv[1:])
def installjob(self):
from biohub.biobrick.management.commands.installjob import Command
Command().run_from_argv(self.argv[:])
def runserver(self):
from django.core.management.commands.runserver import Command
Command().run_from_argv(self.argv[:])
def _run_cmd(self, commands, output, input=sys.stdin):
from subprocess import Popen, PIPE
p = Popen(
commands,
stdout=sys.stdout if output else PIPE,
stderr=sys.stderr if output else PIPE,
stdin=input
)
p.communicate()
return p.returncode
def _detect(self, name, commands):
print('Detecting {}...'.format(name))
if self._run_cmd(commands, True) != 0:
print('{} not detected.'.format(name))
sys.exit(1)
else:
print('{} detected.\n'.format(name))
def _run_mysql(self, *commands, input=None, output=True):
return self._run_cmd([
'mysql',
'-u%s' % self.db_user,
*(['-p%s' % self.db_password] if self.db_password else []),
'-h%s' % self.db_host,
'-P%s' % self.db_port,
*commands
], output, input=input)
def _run_mysql_cmd(self, command):
return self._run_mysql(
'-e',
command
)
def _prepare_db(self):
from biohub.core.conf import settings
db_config = settings.DEFAULT_DATABASE
self.db_name = db_config['NAME']
self.db_user = db_config['USER']
self.db_password = db_config['PASSWORD']
self.db_host = db_config['HOST']
self.db_port = db_config['PORT']
if self._run_mysql_cmd('select 1 as test;'):
print('mysql was incorrectly configured.')
sys.exit(1)
self._prepare_igem()
if self._run_mysql_cmd('USE %s;' % self.db_name):
print('Creating main database...')
self._run_mysql_cmd('CREATE DATABASE %s CHARACTER SET utf8;' % self.db_name)
call_command('migrate')
print('\nMain database prepared.')
def _prepare_igem(self):
import errno
try:
os.makedirs(resolve('_download'))
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
dest = resolve('_download', 'biobricks.sql')
if not os.path.isfile(dest):
gz_dest = resolve('_download', 'biobricks.sql.gz')
if os.path.isfile(gz_dest):
print('Download cache detected.')
else:
from biohub.utils.download import download
download('http://parts.igem.org/partsdb/download.cgi?type=parts_sql', dest=gz_dest)[0].close()
print('Extracting...')
self._run_cmd(['gunzip', gz_dest, '-k', '-f'], True)
print('Importing initial data...')
self._run_mysql_cmd('CREATE DATABASE IF NOT EXISTS igem CHARACTER SET utf8;')
self._run_mysql('igem', input=open(dest, 'r'))
print('Initial data imported.\n')
print('Preprocessing initial data...')
self._run_cmd([
sys.executable,
resolve('biohub', 'biobrick', 'bin', 'updateparts.py'),
'--host', self.db_host,
'--port', self.db_port,
'--user', self.db_user,
*(['--password', self.db_password] if self.db_password else []),
'--chunk', '500'
], True)
def _prepare_weights(self):
print('\nCalculating weights for the first time...')
call_command('refreshweight', update_index=True, age='15s')
def _prepare_graph(self):
print('\nInstalling relationships of bricks...')
call_command('installgraph')
print('Relationships installed.\n')
def init(self):
"""
The main function of init command.
"""
self._detect('mysql', ['mysql', '--version'])
self._detect('redis', ['redis-cli', '--version'])
self._prepare_db()
self._prepare_graph()
self._prepare_weights()
print('\nCompiling biocircuit...')
self._run_cmd(['/usr/bin/python3', resolve('biohub', 'biocircuit', 'compile-espresso.py')], True)
print('Compiled.\n')
print('Finished initialization.')
if __name__ == '__main__':
if sys.version_info[0] < 3 or sys.version_info[1] < 5:
print('Your python version is too low.')
sys.exit(1)
ManagementUtility().execute()