/
CWLKernel.py
318 lines (277 loc) · 12.5 KB
/
CWLKernel.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
308
309
310
311
312
313
314
315
316
317
318
import json
import logging
import os
import re
import shutil
import traceback
from io import StringIO
from pathlib import Path
from typing import List, Dict, Optional, Tuple, Union, Callable, NoReturn
from ipykernel.kernelbase import Kernel
from ruamel import yaml
from ruamel.yaml import YAML
from .AutoCompleteEngine import AutoCompleteEngine
from .CWLBuilder import CWLSnippetBuilder
from .CWLExecuteConfigurator import CWLExecuteConfigurator
from .CWLLogger import CWLLogger
from .CoreExecutor import CoreExecutor
from .IOManager import IOFileManager, ResultsManager
from .cwlrepository.CWLComponent import WorkflowComponentFactory, CWLWorkflow
from .cwlrepository.cwlrepository import WorkflowRepository
from .git.CWLGitResolver import CWLGitResolver
version = "0.0.2"
BOOT_DIRECTORY = Path(os.getcwd()).absolute()
CONF = CWLExecuteConfigurator()
class CWLKernel(Kernel):
"""Jupyter Notebook kernel for CWL."""
implementation = 'CWLKernel'
implementation_version = version
language_version = '1.1'
language_info = {
'name': 'yaml',
'mimetype': 'text/x-cwl',
'file_extension': '.cwl',
}
banner = "Common Workflow Language"
_magic_commands: Dict = {}
_auto_complete_engine = AutoCompleteEngine(map(lambda m: m.__name__, _magic_commands.values()))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._session_dir = os.path.join(CONF.CWLKERNEL_BOOT_DIRECTORY, self.ident)
self._boot_directory: Path = BOOT_DIRECTORY
self._yaml_input_data: Optional[str] = None
self._results_manager = ResultsManager(os.path.join(self._session_dir, 'results'))
runtime_file_manager = IOFileManager(os.path.join(self._session_dir, 'runtime_data'))
self._cwl_executor = CoreExecutor(runtime_file_manager, self._boot_directory)
self._pid = (os.getpid(), os.getppid())
self._cwl_logger = CWLLogger(os.path.join(CONF.CWLKERNEL_BOOT_DIRECTORY, self.ident, 'logs'))
self._set_process_ids()
self._cwl_logger.save()
self._workflow_repository = WorkflowRepository(
Path(os.sep.join([CONF.CWLKERNEL_BOOT_DIRECTORY, self.ident, 'repo'])))
self._snippet_builder = CWLSnippetBuilder()
self._workflow_composer: Optional[CWLWorkflow] = None
self._github_resolver: CWLGitResolver = CWLGitResolver(
Path(os.sep.join([CONF.CWLKERNEL_BOOT_DIRECTORY, self.ident, 'git'])))
if self.log is None: # pylint: disable=access-member-before-definition
self.log = logging.getLogger()
@property
def runtime_directory(self) -> Path:
return Path(self._cwl_executor.file_manager.ROOT_DIRECTORY).absolute()
@property
def workflow_repository(self) -> WorkflowRepository:
return self._workflow_repository
@property
def results_manager(self) -> ResultsManager:
return self._results_manager
@property
def workflow_composer(self) -> CWLWorkflow:
return self._workflow_composer
@workflow_composer.setter
def workflow_composer(self, composer=Optional[CWLWorkflow]):
self._workflow_composer = composer
class register_magic:
"""Registers magic commands. That method should be used as a decorator to register custom magic commands."""
def __init__(self, magics_name: Optional[str] = None):
self._magics_name = magics_name
def __call__(self, magic: Callable):
magics_name = self._magics_name if self._magics_name is not None else magic.__name__
CWLKernel._magic_commands[magics_name] = magic
CWLKernel._auto_complete_engine.add_magic_command(magic.__name__)
return magic
class register_magics_suggester:
"""Decorator for registering functions for suggesting commands line arguments"""
def __init__(self, magic_command_name: str):
self._magic_command_name = magic_command_name
def __call__(self, suggester):
CWLKernel._auto_complete_engine.add_magic_commands_suggester(self._magic_command_name, suggester)
return suggester
def _set_process_ids(self):
self._cwl_logger.process_id = {
"process_id": os.getpid(),
"parent_process_id": os.getppid()
}
def _code_is_valid_yaml(self, code: str) -> Optional[Dict]:
yaml = YAML(typ='safe')
try:
return yaml.load(code)
except Exception:
return None
def _is_magic_command(self, code: str) -> bool:
split_code = code.split()
if len(split_code) < 2:
return False
if code.startswith("% ") and code.split()[1] in self._magic_commands:
return True
return False
def do_execute(self, code: str, silent=False, store_history: bool = True,
user_expressions=None, allow_stdin: bool = False) -> Dict:
status = 'ok'
try:
if self._is_magic_command(code):
self._do_execute_magic_command(code)
else:
dict_code = self._code_is_valid_yaml(code)
if dict_code is None:
raise RuntimeError('Input cannot be parsed')
else:
self._do_execute_yaml(dict_code, code)
except Exception as e:
status = 'error'
traceback.print_exc()
self.send_error_response(f'{type(e).__name__}: {e}')
finally:
return {
'status': status,
# The base class increments the execution count
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {},
}
def _do_execute_yaml(self, dict_code, code):
if not self._is_cwl(dict_code):
raise NotImplementedError()
else:
cwl_component = WorkflowComponentFactory().get_workflow_component(code)
self._workflow_repository.register_tool(cwl_component)
self.send_response(
self.iopub_socket, 'stream',
{'name': 'stdout', 'text': f"tool '{cwl_component.id}' registered"}
)
def _do_execute_magic_command(self, commands: str):
for command in re.compile(r'^%[ ]+', re.MULTILINE).split(commands):
command = command.strip()
if command == '':
continue
command = command.split(" ")
command_name = command[0].strip()
args = " ".join(command[1:])
self._magic_commands[command_name](self, args)
def send_error_response(self, text) -> None:
"""
Sends a response to the jupyter notebook's stderr.
@param text: The message to display
@return: None
"""
self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': text})
def send_json_response(self, json_data: Union[Dict, List]) -> None:
"""
Display a Dict or a List object as a JSON. The object must be json dumpable to use that function.
@param json_data: Data to print in Jupyter Notebook
@return: None
"""
self.send_response(
self.iopub_socket,
'display_data',
{
'data': {
'text/plain': json.dumps(json_data),
'application/json': json_data
},
'metadata': {
'application/json': {
'expanded': False,
'root': 'root'
}
}
}
)
def _set_data(self, code: str) -> NoReturn:
if len(code.split()) > 0:
cwd = Path(self._cwl_executor.file_manager.get_files_uri().path)
data = self._preprocess_data(yaml.load(code, Loader=yaml.Loader))
self._cwl_executor.validate_input_files(data, cwd)
code_stream = StringIO()
yaml.safe_dump(data, code_stream)
self._yaml_input_data = code_stream.getvalue()
self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': 'Add data in memory'})
def _preprocess_data(self, data: Dict) -> Dict:
"""
On the execution the user can reference the data id of a file instead of the actual path. That function
apply that logic
@param data: the actual data
@return the data after the transformation
"""
has_change = False
for key_id in data:
if isinstance(data[key_id], dict) and \
'class' in data[key_id] and \
data[key_id]['class'] == 'File' and \
'$data' in data[key_id]:
has_change = True
data[key_id]['location'] = self._results_manager.get_last_result_by_id(data[key_id]["$data"])
data[key_id].pop('$data')
if has_change is True:
self.send_text_to_stdout('set data to:\n')
self.send_json_response(data)
return data
def _clear_data(self):
self._yaml_input_data = None
def _execute_workflow(self, code_path: Path, provenance: bool = False) -> Optional[Exception]:
input_data = [self._yaml_input_data] if self._yaml_input_data is not None else []
self._cwl_executor.set_data(input_data)
self._cwl_executor.set_workflow_path(str(code_path))
self.log.debug('starting executing workflow ...')
run_id, results, exception, research_object = self._cwl_executor.execute(provenance)
self.log.debug(f'\texecution results: {run_id}, {results}, {exception}')
output_directory_for_that_run = str(run_id)
for output in results:
if isinstance(results[output], list):
for i, _ in enumerate(results[output]):
results[output][i]['id'] = f'{output}_{i + 1}'
results[output][i]['result_counter'] = self._results_manager.files_counter
self._results_manager.append_files(
[results[output][i]['location']],
output_directory_for_that_run,
metadata=results[output][i]
)
else:
results[output]['id'] = output
results[output]['result_counter'] = self._results_manager.files_counter
self._results_manager.append_files(
[results[output]['location']],
output_directory_for_that_run,
metadata=results[output]
)
self.send_json_response(results)
if research_object is not None:
self.send_text_to_stdout(f'\nProvenance stored in directory {research_object.folder}')
for path, _, files in os.walk(research_object.folder):
for name in files:
file = os.path.relpath(os.path.join(path, name), self._boot_directory.as_posix())
self.send_response(
self.iopub_socket,
'display_data',
{
'data': {
"text/html": f'<a href="/files/{file}">{file}</a>',
"text/plain": f"{file}"
},
'metadata': {},
},
)
if exception is not None:
self.log.debug(f'execution error: {exception}')
self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': str(exception)})
os.chdir(self._boot_directory.as_posix())
return exception
def get_past_results(self) -> List[str]:
return self._results_manager.get_files()
def _is_cwl(self, code: Dict):
return 'cwlVersion' in code.keys()
def get_pid(self) -> Tuple[int, int]:
""":return: The process id and his parents id."""
return self._pid
def do_complete(self, code: str, cursor_pos: int):
self.log.debug(f"code: {code}\ncursor_pos: {cursor_pos}\ncode[{cursor_pos}]=XXX")
suggestions = self._auto_complete_engine.suggest(code, cursor_pos)
self.log.debug(f'suggestions: {suggestions["matches"]}')
return {**suggestions, 'status': 'ok'}
def send_text_to_stdout(self, text: str):
self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': text})
def __del__(self):
shutil.rmtree(self._session_dir, ignore_errors=True)
os.chdir(self._boot_directory.as_posix())
if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=CWLKernel)