This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
/
utils.py
110 lines (97 loc) · 4.22 KB
/
utils.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
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
from ..api.task import OperationTask
from .. import exceptions
def create_node_task(node, interface_name, operation_name):
"""
Returns a new operation task if the operation exists in the node, otherwise returns None.
"""
try:
return OperationTask.for_node(node=node,
interface_name=interface_name,
operation_name=operation_name)
except exceptions.OperationNotFoundException:
# We will skip nodes which do not have the operation
return None
def create_relationships_tasks(
node, interface_name, source_operation_name=None, target_operation_name=None):
"""
Creates a relationship task (source and target) for all of a node_instance relationships.
:param basestring source_operation_name: the relationship operation name.
:param basestring interface_name: the name of the interface.
:param source_operation_name:
:param target_operation_name:
:param NodeInstance node: the source_node
:return:
"""
sub_tasks = []
for relationship in node.outbound_relationships:
try:
relationship_operations = relationship_tasks(
relationship,
interface_name,
source_operation_name=source_operation_name,
target_operation_name=target_operation_name)
sub_tasks.append(relationship_operations)
except exceptions.OperationNotFoundException:
# We will skip relationships which do not have the operation
pass
return sub_tasks
def relationship_tasks(
relationship, interface_name, source_operation_name=None, target_operation_name=None):
"""
Creates a relationship task source and target.
:param Relationship relationship: the relationship instance itself
:param source_operation_name:
:param target_operation_name:
:return:
"""
operations = []
if source_operation_name:
operations.append(
OperationTask.for_relationship(relationship=relationship,
interface_name=interface_name,
operation_name=source_operation_name,
runs_on='source')
)
if target_operation_name:
operations.append(
OperationTask.for_relationship(relationship=relationship,
interface_name=interface_name,
operation_name=target_operation_name,
runs_on='target')
)
return operations
def create_node_task_dependencies(graph, tasks_and_nodes, reverse=False):
"""
Creates dependencies between tasks if there is a relationship (outbound) between their nodes.
"""
def get_task(node_name):
for task, node in tasks_and_nodes:
if node.name == node_name:
return task
return None
for task, node in tasks_and_nodes:
dependencies = []
for relationship in node.outbound_relationships:
dependency = get_task(relationship.target_node.name)
if dependency:
dependencies.append(dependency)
if dependencies:
if reverse:
for dependency in dependencies:
graph.add_dependency(dependency, task)
else:
graph.add_dependency(task, dependencies)