forked from shotgunsoftware/tk-framework-perforce
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync_workers.py
More file actions
295 lines (235 loc) · 10.8 KB
/
Copy pathsync_workers.py
File metadata and controls
295 lines (235 loc) · 10.8 KB
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
import sgtk
from sgtk.platform.qt import QtCore, QtGui
import os
import traceback
import pprint
import random
import time
from ..sync.resolver import TemplateResolver
class SyncSignaller(QtCore.QObject):
"""
Create signaller class for Sync Worker, required for using signals due to QObject inheritance
"""
started = QtCore.Signal(dict)
finished = QtCore.Signal()
progress = QtCore.Signal(dict) # (path to sync, p4 sync response)
class AssetInfoGatherSignaller(QtCore.QObject):
"""
Create signaller class for AssetInfoGather Worker, required for using signals due to QObject inheritance
"""
progress = QtCore.Signal(str)
root_path_resolved = QtCore.Signal(str)
info_gathered = QtCore.Signal(dict)
item_found_to_sync = QtCore.Signal(dict)
status_update = QtCore.Signal(str)
includes = QtCore.Signal(tuple)
class SyncWorker(QtCore.QRunnable):
# structurally anticipate basic p4 calls, which will route to the main form.
p4 = None
fw = None
path_to_sync = None
asset_name = None
def __init__(self):
"""
Handles syncing specific file from perforce depot to local workspace on disk
"""
super(SyncWorker, self).__init__()
self.signaller = SyncSignaller()
# use signals from Signaller, since we cant in a non-QObject derrived
# object like this QRunner.
self.started = self.signaller.started
self.finished = self.signaller.finished
self.progress = self.signaller.progress
@QtCore.Slot()
def run(self):
"""
Ryn syncs from perforce, signals information back to main thread.
"""
self.p4 = self.fw.connection.connect()
# self.fw.log_debug("starting thread in pool to sync {}".format(self.path_to_sync))
self.started.emit({
"asset_name" : self.asset_name,
"sync_path" : self.path_to_sync
}
)
# run the syncs
p4_response = self.p4.run("sync", ["-f"], "{}#head".format(self.path_to_sync))
self.fw.log_debug(p4_response)
# emit item key and p4 response to main thread
self.progress.emit({
"asset_name" : self.asset_name,
"sync_path" : self.path_to_sync,
"response" : p4_response
}
)
class AssetInfoGatherWorker(QtCore.QRunnable):
def __init__(self, app=None, entity=None, framework=None):
"""
Handles gathering information about specific asset from SG and gets related Perforce information
"""
super(AssetInfoGatherWorker, self).__init__()
self.app = app
self.entity = entity
self.force_sync = False
self._items_to_sync = []
self._status = None
self._icon = None
self._detail = None
self.child = False
self.fw = framework
self.asset_item = None
self.signaller = AssetInfoGatherSignaller()
self.info_gathered = self.signaller.info_gathered
self.progress = self.signaller.progress
self.root_path_resolved = self.signaller.root_path_resolved
self.item_found_to_sync = self.signaller.item_found_to_sync
self.status_update = self.signaller.status_update
self.includes = self.signaller.includes
self.publish_file = False
@property
def asset_name(self):
name = None
if self.asset_item.get('context'):
name = self.asset_item.get('context').entity.get('name')
if not name:
if self.entity.get('code'):
name = self.entity.get('code')
else:
name = self.app.shotgun.find_one(self.entity.get('type'), [["id", "is", self.entity.get('id')]], ['code']).get('code')
if self.entity.get('type') in ["PublishFiles"]:
sg_ret = self.app.shotgun.find_one("Asset", [["id", "is", self.entity.get('entity').get('id')]], ['code'])
name = sg_ret.get('code')
return name
@property
def root_path(self):
p4_path_operator = "*"
if self.child is True:
p4_path_operator = "..."
self.fw.logger.warning("processed root path as a child: %s" %self.asset_item.get('root_path'))
rp = os.path.join(self.asset_item.get('root_path'), p4_path_operator )
if self.entity.get('type') in ["PublishedFile"]:
rp = "B:/" + self.entity.get('path_cache')
return rp
@property
def status(self):
if self.asset_item.get('error'):
self._icon = "warning"
self._status = "Error"
self._detail = self.asset_item.get('error')
return self._status
def collect_and_map_info(self):
"""
Call perforce for response and form data we will signal back
"""
if self.status != 'error':
self.get_perforce_sync_dry_reponse()
# payload that we'll send back to the main thread to make UI item with
self.info_to_signal = {
"asset_name": self.asset_name,
"root_path" : self.root_path,
"status" : self._status,
"details" : self._detail,
"icon" : self._icon,
"asset_item" : self.asset_item,
"items_to_sync": self._items_to_sync
}
def get_perforce_sync_dry_reponse(self):
"""
Get a response from perforce about our wish to sync a specific asset root path,
Contextually use response to drive our status that we show the user. 1
"""
if self.root_path and (self.entity.get('type') not in ['PublishedFile']):
self.p4 = self.fw.connection.connect()
arguments = ["-n"]
if self.force_sync:
arguments.append("-f")
sync_response = self.p4.run("sync", arguments, "{}#head".format(self.root_path))
if not sync_response:
self._status = "Not In Depot"
self._icon = "error"
self._detail = "Nothing in depot resolves [{}]".format(self.root_path)
elif len(sync_response) is 1 and type(sync_response[0]) is str:
self._status = "Syncd"
self._icon = "success"
self._detail = "Nothing new to sync for [{}]".format(self.root_path)
else:
# if the response from p4 has items... make UI elements for them
self._items_to_sync = [i for i in sync_response if type(i) != str]
self._status = "{} items to Sync".format(len(self._items_to_sync))
self._icon = "load"
self._detail = self.root_path
if self.entity.get('type') in ['PublishedFile']:
self._items_to_sync = [{"clientFile" : "B:/" + self.entity.get('path_cache')}]
self._status = "Exact Path"
self._detail = "Exact path specified: [{}]".format(self.root_path)
self._icon = "load"
self.fw.log_info(self._items_to_sync)
@QtCore.Slot()
def run(self):
"""
Checks if there are errors in the item, signals that, or if not, gets info regarding what there is to sync.
"""
try:
self.template_resolver = TemplateResolver(app=self.app,
entity=self.entity,
)
self.asset_item = self.template_resolver.entity_info
progress_status_string = ""
self.status_update.emit("Requesting sync information for {}".format(self.asset_name))
#self.fw.log_info(self.asset_item)
self.collect_and_map_info()
self.info_gathered.emit(self.info_to_signal)
if self.status == 'Syncd':
progress_status_string = " (Nothing to sync. Skipping...)"
if self.status != "Error":
if self._items_to_sync:
# make lookup list for SG api call for published files to correlate.
depot_files = [i.get('depotFile') for i in self._items_to_sync]
find_fields = [
"sg_p4_change_number"
"code",
"entity.Asset.code"
"sg_p4_depo_path",
"task.Task.step.Step.code",
"published_file_type.PublishedFileType.code",
]
sg_filter = ["sg_p4_depo_path", "in", depot_files]
if self.entity.get('type') in ["PublishedFile"]:
sg_filter = ["id", "in", self.entity.get('id')]
published_files = self.app.shotgun.find('PublishedFile', [sg_filter], find_fields )
published_file_by_depot_file = {i.get('sg_p4_depo_path'):i for i in published_files}
# self.fw.log_info(published_file_by_depot_file)
for item in self._items_to_sync:
published_file = published_file_by_depot_file.get(item.get('depotFile'))
step = None
file_type = None
if published_file:
#self.fw.log_info(published_file_by_depot_file)
step = published_file.get("task.Task.step.Step.code")
file_type = published_file.get("published_file_type.PublishedFileType.code")
if file_type:
self.includes.emit(("type", file_type))
if step:
self.includes.emit(("step", step))
ext = None
if "." in item.get("clientFile"):
ext = os.path.basename(item.get("clientFile")).split('.')[-1]
self.includes.emit(("ext", ext.lower()))
status = item.get('action')
if self.entity.get('type') in ["PublishedFile"]:
status = "Exact File"
self.item_found_to_sync.emit( {
"asset_name" : self.asset_name,
"item_found" : item,
"step" : step,
"type" : file_type,
"ext" : ext.lower(),
"status" : status
}
)
else:
progress_status_string = " (Encountered error. See details)"
self.fw.log_info(progress_status_string)
except Exception as e:
self.fw.log_error(traceback.format_exc())
self.progress.emit("Gathering info for {} {}".format(self.asset_name, progress_status_string))