-
Notifications
You must be signed in to change notification settings - Fork 0
/
cached-translated-groovy3-parser.py
428 lines (355 loc) · 13.1 KB
/
cached-translated-groovy3-parser.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
# groovy-parser, a proof of concept Groovy parser based on Pygments and Lark
# Copyright (C) 2023 Barcelona Supercomputing Center, José M. Fernández
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import os
import re
import sys
from typing import (
cast,
NamedTuple,
TYPE_CHECKING,
)
if TYPE_CHECKING:
from typing import (
Iterator,
MutableSequence,
Optional,
Sequence,
Tuple,
Union,
)
from groovy_parser.parser import (
EmptyNode,
LeafNode,
RuleNode,
)
from pygments.token import Token
from groovy_parser.parser import (
parse_and_digest_groovy_content,
)
from lark import (
Lark,
Transformer,
v_args,
)
from lark.visitors import Discard
# FilteredOutTokens = (
# Token.Comment,
# Token.Comments,
# Token.Generic,
# Token.Other,
# )
ROOT_RULE = ["compilation_unit", "script_statements"]
INCLUDE_PROCESS_RULE = [
# "script_statement",
"statement",
"statement_expression",
"command_expression",
]
IDENTIFIER_RULE = ["primary", "identifier"]
PRE_IDENTIFIER_NAME = [
"expression",
"postfix_expression",
"path_expression",
]
PROCESS_CHILD = {"leaf": "IDENTIFIER", "value": "process"}
INCLUDE_CHILD = {"leaf": "IDENTIFIER", "value": "include"}
WORKFLOW_CHILD = {"leaf": "IDENTIFIER", "value": "workflow"}
CONTAINER_CHILD = {"leaf": "IDENTIFIER", "value": "container"}
CONDA_CHILD = {"leaf": "IDENTIFIER", "value": "conda"}
TEMPLATE_CHILD = {"leaf": "IDENTIFIER", "value": "template"}
P_RULE = [
"argument_list",
"first_argument_list_element",
"expression_list_element",
"expression",
"postfix_expression",
"path_expression",
]
W_RULE = [
"argument_list",
"first_argument_list_element",
"expression_list_element",
"expression",
"postfix_expression",
"path_expression",
]
NAMELESS_W_RULE = [
"argument_list",
"first_argument_list_element",
"expression_list_element",
"expression",
"postfix_expression",
"path_expression",
"primary",
"closure_or_lambda_expression",
"closure",
]
def extract_strings(node: "Union[EmptyNode, LeafNode, RuleNode]") -> "Iterator[str]":
leaf_type = node.get("leaf")
if leaf_type is not None:
lnode = cast("LeafNode", node)
if leaf_type in ("STRING_LITERAL", "STRING_LITERAL_PART"):
yield lnode["value"]
else:
children = node.get("children")
if isinstance(children, list):
for child in children:
yield from extract_strings(child)
class NfProcess(NamedTuple):
name: "str"
containers: "Sequence[str]"
condas: "Sequence[str]"
templates: "Sequence[str]"
def extract_nextflow_containers(
node: "Union[EmptyNode, LeafNode, RuleNode]",
) -> "Iterator[str]":
# return [ node ]
yield from filter(
lambda s: s not in ("singularity", "docker"), extract_strings(node)
)
def extract_nextflow_condas(
node: "Union[EmptyNode, LeafNode, RuleNode]",
) -> "Iterator[str]":
# return [ node ]
spsplt = re.compile("[\t ]+")
for conda_str in extract_strings(node):
yield from spsplt.split(conda_str)
def extract_nextflow_templates(
node: "Union[EmptyNode, LeafNode, RuleNode]",
) -> "Iterator[str]":
# return [ node ]
yield from extract_strings(node)
def extract_process_features(
t_tree: "RuleNode",
) -> "Tuple[Sequence[str], Sequence[str], Sequence[str]]":
templates: "MutableSequence[str]" = []
containers: "MutableSequence[str]" = []
condas: "MutableSequence[str]" = []
# First, sanity check
# root_rule = t_tree.get("rule")
# if root_rule[-len(ROOT_RULE):] == ROOT_RULE:
# Now, capture what it is interesting
for child in t_tree["children"]:
if "rule" in child:
r_child = cast("RuleNode", child)
child_rule = r_child["rule"]
unprocessed = True
if child_rule[-len(INCLUDE_PROCESS_RULE) :] == INCLUDE_PROCESS_RULE:
# Save the process
c_children = r_child["children"]
c_children_0 = cast("RuleNode", c_children[0])
c_children_0_rule = c_children_0.get("rule")
if (
c_children_0_rule is not None
and c_children_0_rule[-len(PRE_IDENTIFIER_NAME) :]
== PRE_IDENTIFIER_NAME
):
c_children_0 = cast("RuleNode", c_children_0["children"][0])
c_children_0_rule = c_children_0.get("rule")
# This is needed to re-evaluate
if (
c_children_0_rule is not None
and c_children_0_rule[-len(IDENTIFIER_RULE) :] == IDENTIFIER_RULE
):
c_children_0_children = c_children_0["children"]
if c_children_0_children[0] == CONTAINER_CHILD:
containers.extend(extract_nextflow_containers(c_children[1]))
unprocessed = False
elif c_children_0_children[0] == CONDA_CHILD:
# both named and nameless workflows
condas.extend(extract_nextflow_condas(c_children[1]))
unprocessed = False
elif c_children_0_children[0] == TEMPLATE_CHILD:
templates.extend(extract_nextflow_templates(c_children[-1]))
unprocessed = False
if unprocessed:
c_containers, c_condas, c_templates = extract_process_features(r_child)
containers.extend(c_containers)
condas.extend(c_condas)
templates.extend(c_templates)
return containers, condas, templates
def extract_nextflow_process(node: "RuleNode") -> "NfProcess":
p_rule = node.get("rule")
process_name = "<error>"
templates: "Sequence[str]" = []
containers: "Sequence[str]" = []
condas: "Sequence[str]" = []
if p_rule == P_RULE:
p_c_children = node["children"]
assert len(p_c_children) > 0
assert "children" in p_c_children[0]
pro_node = cast("RuleNode", p_c_children[0])
assert len(pro_node["children"]) > 0
assert "value" in pro_node["children"][0]
process_name = cast("LeafNode", pro_node["children"][0])["value"]
process_body = cast("RuleNode", p_c_children[1])
containers, condas, templates = extract_process_features(process_body)
return NfProcess(
name=process_name,
templates=templates,
containers=containers,
condas=condas,
)
class NfInclude(NamedTuple):
path: "str"
def extract_nextflow_includes(node: "RuleNode") -> "Sequence[NfInclude]":
# return [ node ]
return [
NfInclude(
path=path,
)
for path in extract_strings(node)
]
class NfWorkflow(NamedTuple):
name: "Optional[str]"
def extract_nextflow_workflow(node: "RuleNode") -> "NfWorkflow":
nodes = None
name = None
if node["rule"] == W_RULE:
assert len(node["children"]) > 1
name = cast("LeafNode", cast("RuleNode", node["children"][0])["children"][0])[
"value"
]
nodes = cast("RuleNode", node["children"][1])["children"]
elif node["rule"] == NAMELESS_W_RULE:
nodes = node["children"]
return NfWorkflow(
name=name,
)
def extract_nextflow_features(
t_tree: "RuleNode",
) -> "Tuple[Sequence[NfProcess], Sequence[NfInclude], Sequence[NfWorkflow]]":
processes: "MutableSequence[NfProcess]" = []
includes: "MutableSequence[NfInclude]" = []
workflows: "MutableSequence[NfWorkflow]" = []
# First, sanity check
# root_rule = t_tree.get("rule")
# if root_rule[-len(ROOT_RULE):] == ROOT_RULE:
# Now, capture what it is interesting
for a_child in t_tree["children"]:
if "rule" in a_child:
child = cast("RuleNode", a_child)
child_rule = child["rule"]
unprocessed = True
if child_rule[-len(INCLUDE_PROCESS_RULE) :] == INCLUDE_PROCESS_RULE:
# Save the process
c_children = child["children"]
c_children_0 = cast("RuleNode", c_children[0])
c_children_0_rule = c_children_0.get("rule")
if (
c_children_0_rule is not None
and c_children_0_rule[-len(PRE_IDENTIFIER_NAME) :]
== PRE_IDENTIFIER_NAME
):
c_children_0 = cast("RuleNode", c_children_0["children"][0])
c_children_0_rule = c_children_0.get("rule")
# This is needed to re-evaluate
if (
c_children_0_rule is not None
and c_children_0_rule[-len(IDENTIFIER_RULE) :] == IDENTIFIER_RULE
):
c_children_0_children = c_children_0["children"]
if c_children_0_children[0] == PROCESS_CHILD:
processes.append(
extract_nextflow_process(cast("RuleNode", c_children[1]))
)
unprocessed = False
elif c_children_0_children[0] == WORKFLOW_CHILD:
# both named and nameless workflows
workflows.append(
extract_nextflow_workflow(cast("RuleNode", c_children[1]))
)
unprocessed = False
elif c_children_0_children[0] == INCLUDE_CHILD:
includes.extend(
extract_nextflow_includes(cast("RuleNode", c_children[-1]))
)
unprocessed = False
if unprocessed:
c_processes, c_includes, c_workflows = extract_nextflow_features(child)
processes.extend(c_processes)
includes.extend(c_includes)
workflows.extend(c_workflows)
return processes, includes, workflows
def analyze_nf_source(
filename: "str",
jsonfile: "str",
resultfile: "str",
cache_directory: "Optional[str]" = None,
) -> "Union[RuleNode, LeafNode, EmptyNode]":
with open(filename, mode="r", encoding="utf-8") as wfH:
content = wfH.read()
t_tree = parse_and_digest_groovy_content(content, cache_directory=cache_directory)
# These are for debugging purposes
# logging.debug(tree.pretty())
# with open(jsonfile, mode="w", encoding="utf-8") as jH:
# json.dump(tree, jH, indent=4, cls=LarkFilteringTreeEncoder)
with open(jsonfile, mode="w", encoding="utf-8") as jH:
json.dump(t_tree, jH, indent=4)
# res = None
# res = ParseNextflowTreeToDict().transform(tree)
# import json
# json.dump(res, sys.stdout, indent=4, sort_keys=True)
#
# logging.debug('-->')
# logging.debug(res) # prints {'alice': [1, 27, 3], 'bob': [4], 'carrie': [], 'dan': [8, 6]}
if "rule" in t_tree:
processes, includes, workflows = extract_nextflow_features(
cast("RuleNode", t_tree)
)
else:
processes = []
includes = []
workflows = []
with open(resultfile, mode="w", encoding="utf-8") as rW:
print(f"P {processes}", file=rW)
print(f"I {includes}", file=rW)
print(f"W {workflows}", file=rW)
return t_tree
if __name__ == "__main__":
logging.basicConfig(
level=logging.DEBUG,
)
log = logging.getLogger() # root logger
cache_directory = os.environ.get("GROOVY_CACHEDIR")
if cache_directory is not None:
print(f"* Using as caching directory {cache_directory}")
os.makedirs(cache_directory, exist_ok=True)
else:
print(
"[WARNING] No caching is done. If you want to cache parsed content declare variable GROOVY_CACHEDIR"
)
for filename in sys.argv[1:]:
print(f"* Parsing {filename}")
logfile = filename + ".lark"
jsonfile = logfile + ".json"
resultfile = logfile + ".result"
fH = logging.FileHandler(logfile, mode="w", encoding="utf-8")
for hdlr in log.handlers[:]: # remove all old handlers
log.removeHandler(hdlr)
log.addHandler(fH) # set the new handler
try:
analyze_nf_source(
filename, jsonfile, resultfile, cache_directory=cache_directory
)
except Exception as e:
print(f"\tParse failed, see {logfile}")
logging.exception("Parse failed")
fH.close()