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 | class PostFtrackHook(PostLaunchHook):
order = None
launch_types = {LaunchTypes.local}
def execute(self):
project_name = self.data.get("project_name")
project_settings = self.data.get("project_settings")
folder_path = self.data.get("folder_path")
task_name = self.data.get("task_name")
missing_context_keys = [
key
for value, key in (
(project_name, "project_name"),
(project_settings, "project_settings"),
(folder_path, "asset_name"),
(task_name, "task_name"),
)
if not value
]
if missing_context_keys:
missing_keys_str = ", ".join([
f"'{key}'" for key in missing_context_keys
])
self.log.debug(
f"Hook {self.__class__.__name__} skipped."
f" Missing required data: {missing_keys_str}"
)
return
if "ftrack" not in project_settings:
self.log.debug(
"Missing ftrack settings. Skipping post launch logic."
)
return
if not is_ftrack_enabled_in_settings(project_settings["ftrack"]):
self.log.debug(
f"ftrack is disabled for project '{project_name}'. Skipping."
)
return
required_keys = ("FTRACK_SERVER", "FTRACK_API_USER", "FTRACK_API_KEY")
for key in required_keys:
if not os.environ.get(key):
self.log.debug(
f"Missing required environment '{key}'"
" for ftrack post launch procedure."
)
return
try:
session = ftrack_api.Session(auto_connect_event_hub=False)
self.log.debug("ftrack session created")
except Exception:
self.log.warning("Couldn't create ftrack session")
return
try:
entity = self._find_ftrack_task_entity(
session, project_name, folder_path, task_name
)
if entity:
self.ftrack_status_change(session, entity, project_name)
except Exception:
self.log.warning(
"Couldn't finish ftrack post launch logic.",
exc_info=True
)
return
finally:
session.close()
def ftrack_status_change(self, session, entity, project_name):
project_settings = get_project_settings(project_name)
status_update = (
project_settings
["ftrack"]
["post_launch_hook"]
)
if not status_update["enabled"]:
self.log.debug(
f"Status changes are disabled for project '{project_name}'"
)
return
status_mapping = status_update["mapping"]
if not status_mapping:
self.log.warning(
f"Project '{project_name}' does not have set status changes."
)
return
current_status = entity["status"]["name"].lower()
already_tested = set()
ent_path = "/".join(
[ent["name"] for ent in entity["link"]]
)
statuses = session.query("select id, name from Status").all()
statuses_by_low_name = {
status["name"].lower(): status
for status in statuses
}
# TODO refactor
while True:
next_status_name = None
for item in status_mapping:
new_status = item["name"].lower()
if new_status in already_tested:
continue
already_tested.add(new_status)
found_match = False
for from_status in item["value"]:
from_status = from_status.lower()
if from_status in (current_status, "__any__"):
found_match = True
if new_status != "__ignore__":
next_status_name = new_status
break
if found_match:
break
if next_status_name is None:
break
status = statuses_by_low_name.get(next_status_name)
if status is None:
self.log.warning(
f"Status '{next_status_name}' not found in ftrack."
)
continue
try:
entity["status_id"] = status["id"]
session.commit()
self.log.debug(
f"Status changed to \"{next_status_name}\" <{ent_path}>"
)
break
except Exception:
session.rollback()
self.log.warning(
f"Status '{next_status_name}' is not available"
f" for ftrack entity type '{entity.entity_type}'"
)
def _find_ftrack_folder_entity(self, session, folder):
"""
Args:
session (ftrack_api.Session): ftrack session.
folder (dict): AYON folder data.
Returns:
Union[ftrack_api.entity.base.Entity, None]: ftrack folder entity.
"""
# Find ftrack entity by id stored on folder
# - Maybe more options could be used? Find ftrack entity by folder
# path in ftrack custom attributes.
if folder:
ftrack_id = folder["attrib"].get(FTRACK_ID_ATTRIB)
if ftrack_id:
entity = session.query(
f"TypedContext where id is \"{ftrack_id}\""
).first()
if entity:
return entity
return None
def _find_ftrack_task_entity(
self, session, project_name, folder_path, task_name
):
"""
Args:
session (ftrack_api.Session): ftrack session.
project_name (str): Project name.
folder_path (str): Folder path.
task_name (str): Task name.
Returns:
Union[ftrack_api.entity.base.Entity, None]: ftrack task entity.
"""
project_entity = session.query(
f"Project where full_name is \"{project_name}\""
).first()
if not project_entity:
self.log.warning(
f"Couldn't find project '{project_name}' in ftrack."
)
return None
# TODO use 'folder' entity data from launch context when is available.
# At this moment there is only "asset doc".
folder = ayon_api.get_folder_by_path(project_name, folder_path)
parent_entity = self._find_ftrack_folder_entity(session, folder)
if parent_entity is None:
self.log.warning(
f"Couldn't find folder '{folder_path}' in ftrack"
f" project '{project_name}'."
)
return None
parent_id = parent_entity["id"]
task_entity = session.query(
f"Task where parent_id is '{parent_id}' and name is '{task_name}'"
).first()
if task_entity is None:
self.log.warning(
f"Couldn't find task '{folder_path}/{task_name}' in ftrack."
)
return task_entity
|