/
graph.py
459 lines (362 loc) · 16.7 KB
/
graph.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# -*- coding: utf-8 -*-
#
# Copyright 2018-2021 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# 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.
"""Dependency and Provenance graph building."""
import functools
import shutil
import sys
import traceback
from collections import defaultdict
from pathlib import Path
from typing import Dict
import git
from git import NULL_TREE, Commit, GitCommandError
from pkg_resources import resource_filename
from renku.core import errors
from renku.core.commands.dataset import create_dataset_helper
from renku.core.commands.update import execute_workflow
from renku.core.incubation.database import Database
from renku.core.management import LocalClient
from renku.core.management.command_builder.command import Command, inject
from renku.core.management.config import RENKU_HOME
from renku.core.management.datasets import DATASET_METADATA_PATHS, DatasetsApiMixin
from renku.core.management.migrate import migrate
from renku.core.management.repository import RepositoryApiMixin
from renku.core.models.dataset import DatasetsProvenance
from renku.core.models.entities import Entity
from renku.core.models.jsonld import load_yaml
from renku.core.models.provenance.activities import Activity
from renku.core.models.provenance.provenance_graph import ProvenanceGraph
from renku.core.models.workflow.dependency_graph import DependencyGraph
from renku.core.models.workflow.run import Run
from renku.core.utils import communication
from renku.core.utils.contexts import measure
from renku.core.utils.migrate import MigrationType, read_project_version_from_yaml
from renku.core.utils.scm import git_unicode_unescape
from renku.core.utils.shacl import validate_graph
GRAPH_METADATA_PATHS = [
Path(RENKU_HOME) / Path(RepositoryApiMixin.DATABASE_PATH),
Path(RENKU_HOME) / Path(RepositoryApiMixin.DEPENDENCY_GRAPH),
Path(RENKU_HOME) / Path(RepositoryApiMixin.PROVENANCE_GRAPH),
Path(RENKU_HOME) / Path(DatasetsApiMixin.DATASETS_PROVENANCE),
]
def generate_graph():
"""Return a command for generating the graph."""
command = Command().command(_generate_graph).lock_project().require_migration()
return command.with_database(write=True, create=True).with_commit(commit_only=GRAPH_METADATA_PATHS)
@inject.autoparams()
def _generate_graph(client: LocalClient, database: Database, force=False):
"""Generate graph and dataset provenance metadata."""
def process_workflows(commit: Commit):
for file_ in commit.diff(commit.parents or NULL_TREE, paths=f"{client.workflow_path}/*.yaml"):
# Ignore deleted files (they appear as ADDED in this backwards diff)
if file_.change_type == "A":
continue
path: str = git_unicode_unescape(file_.a_path)
if not path.startswith(".renku/workflow") or not path.endswith(".yaml"):
continue
if not (client.path / path).exists():
communication.warn(f"Workflow file does not exists: '{path}'")
continue
workflow = Activity.from_yaml(path=path, client=client)
client.update_graphs(workflow)
def process_datasets(commit):
files_diff = list(commit.diff(commit.parents or NULL_TREE, paths=".renku/datasets/*/*.yml"))
paths = [git_unicode_unescape(f.a_path) for f in files_diff]
paths = [p for p in paths if len(Path(p).parents) == 4] # Exclude files that are not in the right place
deleted_paths = [git_unicode_unescape(f.a_path) for f in files_diff if f.change_type == "A"]
datasets, deleted_datasets = _fetch_datasets(commit.hexsha, paths=paths, deleted_paths=deleted_paths)
revision = commit.hexsha
date = commit.authored_datetime
for dataset in datasets:
datasets_provenance.add_or_update(dataset, revision=revision, date=date)
for dataset in deleted_datasets:
datasets_provenance.remove(dataset, revision=revision, date=date, client=client)
commits = list(client.repo.iter_commits(paths=[f"{client.workflow_path}/*.yaml", ".renku/datasets/*/*.yml"]))
n_commits = len(commits)
commits = reversed(commits)
if force:
client.remove_graph_files()
elif client.has_graph_files():
raise errors.OperationError("Graph metadata exists. Use ``--force`` to regenerate it.")
client.initialize_graph()
datasets_provenance = DatasetsProvenance.from_database(database)
for n, commit in enumerate(commits, start=1):
communication.echo(f"Processing commits {n}/{n_commits}", end="\r")
try:
process_workflows(commit)
process_datasets(commit)
except errors.MigrationError:
communication.echo("")
communication.warn(f"Cannot process commit '{commit.hexsha}' - Migration failed: {traceback.format_exc()}")
except Exception:
communication.echo("")
communication.warn(f"Cannot process commit '{commit.hexsha}' - Exception: {traceback.format_exc()}")
def status():
"""Return a command for getting workflow graph status."""
return Command().command(_status).with_database(write=False)
@inject.autoparams()
def _status(client: LocalClient, database: Database):
"""Get status of workflows."""
with measure("BUILD AND QUERY GRAPH"):
pg = ProvenanceGraph.from_json(client.provenance_graph_path, lazy=True)
plans_usages = pg.get_latest_plans_usages()
if client.has_external_files():
communication.warn(
"Changes in external files are not detected automatically. To update external files run "
"`renku dataset update -e`."
)
try:
communication.echo("On branch {0}".format(client.repo.active_branch))
except TypeError:
communication.warn("Git HEAD is detached!\n Please move back to your working branch to use renku\n")
with measure("CALCULATE MODIFIED"):
modified, deleted = _get_modified_paths(plans_usages=plans_usages)
if not modified and not deleted:
return None, None, None
stales = defaultdict(set)
with measure("CALCULATE UPDATES"):
for plan_id, path, _ in modified:
paths = DependencyGraph.from_database(database).get_dependent_paths(plan_id, path)
for p in paths:
stales[p].add(path)
modified = {v[1] for v in modified}
return stales, modified, deleted
def update():
"""Return a command for generating the graph."""
command = Command().command(_update).lock_project().with_database(write=True)
return command.require_migration().require_clean().require_nodejs().with_commit(commit_if_empty=False)
@inject.autoparams()
def _update(dry_run, client: LocalClient, database: Database):
"""Update outdated outputs."""
with measure("BUILD AND QUERY GRAPH"):
pg = ProvenanceGraph.from_json(client.provenance_graph_path, lazy=True)
plans_usages = pg.get_latest_plans_usages()
with measure("CALCULATE MODIFIED"):
modified, deleted = _get_modified_paths(plans_usages=plans_usages)
if not modified:
communication.echo("Everything is up-to-date.")
return
with measure("CALCULATE UPDATES"):
plans, plans_with_deleted_inputs = DependencyGraph.from_database(database).get_downstream(modified, deleted)
if plans_with_deleted_inputs:
formatted_deleted_plans = "".join((f"\n\t{p}" for p in plans_with_deleted_inputs))
communication.warn(
f"The following steps cannot be executed because one of their inputs is deleted: {formatted_deleted_plans}"
)
if dry_run:
formatted_plans = "".join((f"\n\t{p}" for p in plans))
communication.echo(f"The following steps would be executed:{formatted_plans}")
return
with measure("CONVERTING RUNS"):
entities_cache: Dict[str, Entity] = {}
runs = [p.to_run(entities_cache) for p in plans]
parent_process = Run()
for run in runs:
parent_process.add_subprocess(run)
execute_workflow(workflow=parent_process, output_paths=None, command_name="update", update_commits=True)
def export_graph():
"""Return a command for exporting graph data."""
return Command().command(_export_graph).with_database(write=False)
@inject.autoparams()
def _export_graph(format, workflows_only, strict, client: LocalClient):
"""Output graph in specific format."""
if not client.provenance_graph_path.exists():
raise errors.ParameterError("Graph is not generated.")
format = format.lower()
if strict and format not in ["json-ld", "jsonld"]:
raise errors.SHACLValidationError(f"'--strict' not supported for '{format}'")
pg = ProvenanceGraph.from_json(client.provenance_graph_path, lazy=True)
# TODO: Add dataset provenance to graph
# if not workflows_only:
# pg.rdf_graph.parse(location=str(client.datasets_provenance_path), format="json-ld")
graph = pg.rdf_graph
if strict:
if format == "jsonld":
format = "json-ld"
_validate_graph(graph, format)
return FORMATS[format](graph)
@inject.autoparams()
def _get_modified_paths(plans_usages, client: LocalClient):
"""Get modified and deleted usages/inputs of a plan."""
modified = set()
deleted = set()
for plan_usage in plans_usages:
_, path, checksum = plan_usage
try:
current_checksum = client.repo.git.rev_parse(f"HEAD:{str(path)}")
except GitCommandError:
deleted.add(plan_usage)
else:
if current_checksum != checksum:
modified.add(plan_usage)
return modified, deleted
@inject.autoparams()
def _fetch_datasets(revision, paths, deleted_paths, client: LocalClient):
from renku.core.models.datasets import Dataset
datasets_path = client.path / ".renku" / "tmp" / "datasets"
shutil.rmtree(datasets_path, ignore_errors=True)
datasets_path.mkdir(parents=True, exist_ok=True)
def read_project_version():
"""Read project version at revision."""
try:
project_file_content = client.repo.git.show(f"{revision}:.renku/metadata.yml")
except GitCommandError: # Project metadata file does not exist
return 1
try:
yaml_data = load_yaml(project_file_content)
return int(read_project_version_from_yaml(yaml_data))
except ValueError:
return 1
def copy_and_migrate_datasets():
existing = []
deleted = []
for path in paths:
rev = revision
if path in deleted_paths:
rev = client.find_previous_commit(path, revision=f"{revision}~")
identifier = Path(path).parent.name
new_path = datasets_path / identifier / "metadata.yml"
new_path.parent.mkdir(parents=True, exist_ok=True)
content = client.repo.git.show(f"{rev}:{path}")
new_path.write_text(content)
if path in deleted_paths:
deleted.append(new_path)
else:
existing.append(new_path)
try:
project_version = read_project_version()
client.set_temporary_datasets_path(datasets_path)
communication.disable()
client.migration_type = MigrationType.DATASETS
migrate(project_version=project_version, skip_template_update=True, skip_docker_update=True)
finally:
communication.enable()
client.clear_temporary_datasets_path()
return existing, deleted
paths, deleted_paths = copy_and_migrate_datasets()
datasets = []
for path in paths:
dataset = Dataset.from_yaml(path, client)
# NOTE: Fixing dataset path after migration
original_identifier = Path(dataset.path).name
dataset.path = f".renku/datasets/{original_identifier}"
datasets.append(dataset)
deleted_datasets = []
for path in deleted_paths:
dataset = Dataset.from_yaml(path, client)
# NOTE: Fixing dataset path after migration
original_identifier = Path(dataset.path).name
dataset.path = f".renku/datasets/{original_identifier}"
deleted_datasets.append(dataset)
return datasets, deleted_datasets
def _dot(rdf_graph, simple=True, debug=False, landscape=False):
"""Format graph as a dot file."""
from rdflib.tools.rdf2dot import rdf2dot
from renku.core.commands.format.graph import _rdf2dot_reduced, _rdf2dot_simple
if debug:
rdf2dot(rdf_graph, sys.stdout)
return
sys.stdout.write('digraph { \n node [ fontname="DejaVu Sans" ] ; \n ')
if landscape:
sys.stdout.write('rankdir="LR" \n')
if simple:
_rdf2dot_simple(rdf_graph, sys.stdout, graph=rdf_graph)
return
_rdf2dot_reduced(rdf_graph, sys.stdout)
_dot_full = functools.partial(_dot, simple=False, landscape=False)
_dot_landscape = functools.partial(_dot, simple=True, landscape=True)
_dot_full_landscape = functools.partial(_dot, simple=False, landscape=True)
_dot_debug = functools.partial(_dot, debug=True)
def _json_ld(rdf_graph):
"""Format graph as JSON-LD."""
data = rdf_graph.serialize(format="json-ld").decode("utf-8")
print(data)
FORMATS = {
"dot": _dot,
"dot-full": _dot_full,
"dot-landscape": _dot_landscape,
"dot-full-landscape": _dot_full_landscape,
"dot-debug": _dot_debug,
"json-ld": _json_ld,
"jsonld": _json_ld,
}
def _validate_graph(rdf_graph, format):
shacl_path = resource_filename("renku", "data/new_graph_shacl_shape.json")
r, _, t = validate_graph(rdf_graph, shacl_path=shacl_path, format=format)
if not r:
raise errors.SHACLValidationError(f"{t}\nCouldn't export: Invalid Knowledge Graph data")
def create_dataset():
"""Return a command for creating an empty dataset in the current repo."""
command = Command().command(_create_dataset).lock_dataset()
return command.require_migration().with_database(write=True).with_commit(commit_only=DATASET_METADATA_PATHS)
@inject.autoparams()
def _create_dataset(name, client: LocalClient, title=None, description="", creators=None, keywords=None):
"""Create a dataset in the repository."""
if not client.has_graph_files():
raise errors.OperationError("Dataset provenance is not generated. Run `renku graph generate`.")
return create_dataset_helper(name=name, title=title, description=description, creators=creators, keywords=keywords)
def add_to_dataset():
"""Return a command for adding data to a dataset."""
command = Command().command(_add_to_dataset).lock_dataset()
return (
command.require_migration()
.with_database(write=True)
.with_commit(raise_if_empty=True, commit_only=DATASET_METADATA_PATHS)
)
@inject.autoparams()
def _add_to_dataset(
client: LocalClient,
urls,
name,
datasets_provenance: DatasetsProvenance,
external=False,
force=False,
overwrite=False,
create=False,
sources=(),
destination="",
ref=None,
):
"""Add data to a dataset."""
if not client.has_graph_files():
raise errors.OperationError("Dataset provenance is not generated. Run `renku graph generate`.")
if len(urls) == 0:
raise errors.UsageError("No URL is specified")
if sources and len(urls) > 1:
raise errors.UsageError("Cannot use `--source` with multiple URLs.")
try:
with client.with_dataset_provenance(name=name, create=create) as dataset:
client.add_data_to_dataset(
dataset,
urls=urls,
external=external,
force=force,
overwrite=overwrite,
sources=sources,
destination=destination,
ref=ref,
)
datasets_provenance.add_or_update(dataset)
except errors.DatasetNotFound:
raise errors.DatasetNotFound(
message=f"Dataset `{name}` does not exist.\nUse `renku dataset create {name}` to create the dataset or "
f"retry with `--create` option for automatic dataset creation."
)
except (FileNotFoundError, git.exc.NoSuchPathError) as e:
message = "\n\t".join(urls)
raise errors.ParameterError(f"Could not find paths/URLs: \n\t{message}") from e