-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathutil.py
More file actions
432 lines (336 loc) · 14.1 KB
/
Copy pathutil.py
File metadata and controls
432 lines (336 loc) · 14.1 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
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
# Copyright (c) 2017 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
import os
import pprint
import sys
import sgtk
# create a logger to use throughout
logger = sgtk.platform.get_logger(__name__)
# ---- file/path util functions
def get_version_path(path, version):
"""
Given a path without a version number, return the path with the supplied
version number.
If a version number is detected in the supplied path, the path will be
returned as-is.
:param path: The path to inject a version number.
:param version: The version number to inject.
:return: The modified path with the supplied version number inserted.
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_version_path",
path=path,
version=version
)
def get_next_version_path(path):
"""
Given a file path, return a path to the next version.
This is typically used by auto-versioning logic in plugins that need to
save the current work file to the next version number.
If no version can be identified in the supplied path, ``None`` will be
returned, indicating that the next version path can't be determined.
:param path: The path to a file, likely one to be published.
:return: The path to the next version of the supplied path.
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_next_version_path",
path=path
)
def get_file_path_components(path):
"""
Convenience method for determining file components for a given path.
:param str path: The path to the file to componentize.
Returns file path components in the form::
# path="/path/to/the/file/my_file.v001.ext"
{
"path": "/path/to/the/file/my_file.v001.ext",
"folder": "/path/to/the/file" ,
"filename": "my_file.v001.ext",
"extension": "ext",
}
# path="/path/to/the/folder"
{
"path": "/path/to/the/folder",
"folder": "/path/to/the" ,
"filename": "folder",
"extension": None,
}
"""
# get the path in a normalized state. no trailing separator, separators are
# appropriate for current os, no double separators, etc.
path = sgtk.util.ShotgunPath.normalize(path)
logger.debug("Getting file path components for path: '%s'..." % (path,))
# break it up into the major components
(folder, filename) = os.path.split(path)
if os.path.isdir(path):
# folder
extension = None
else:
# file. extract the extension and remove the "."
(_, extension) = os.path.splitext(filename)
if extension:
extension = extension.lstrip(".")
else:
# prevent extension = ""
extension = None
file_info = dict(
path=path,
folder=folder,
filename=filename,
extension=extension,
)
logger.debug(
"Extracted components from path '%s': %s" %
(path, file_info)
)
return file_info
def get_frame_sequence_path(path, frame_spec=None):
"""
Given a path with a frame number, return the sequence path where the frame
number is replaced with a given frame specification such as ``{FRAME}`` or
``%04d`` or ``$F``.
:param path: The input path with a frame number
:param frame_spec: The frame specification to replace the frame number with.
:return: The full frame sequence path
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_frame_sequence_path",
path=path,
frame_spec=frame_spec
)
def get_frame_sequences(folder, extensions=None, frame_spec=None):
"""
Given a folder, inspect the contained files to find what appear to be
files with frame numbers.
:param folder: The path to a folder potentially containing a sequence of
files.
:param extensions: A list of file extensions to retrieve paths for.
If not supplied, the extension will be ignored.
:param frame_spec: A string to use to represent the frame number in the
return sequence path.
:return: A list of tuples for each identified frame sequence. The first
item in the tuple is a sequence path with the frame number replaced
with the supplied frame specification. If no frame spec is supplied,
a python string format spec will be returned with the padding found
in the file.
Example::
get_frame_sequences(
"/path/to/the/folder",
["exr", "jpg"],
frame_spec="{FRAME}"
)
[
(
"/path/to/the/supplied/folder/key_light1.{FRAME}.exr",
[<frame_1_path>, <frame_2_path>, ...]
),
(
"/path/to/the/supplied/folder/fill_light1.{FRAME}.jpg",
[<frame_1_path>, <frame_2_path>, ...]
)
]
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_frame_sequences",
folder=folder,
extensions=extensions,
frame_spec=frame_spec
)
def get_publish_name(path, sequence=False):
"""
Given a file path, return the display name to use for publishing.
Typically, this is a name where the path and any version number are removed
in order to keep the publish name consistent as subsequent versions are
published.
Example::
in: /path/to/the/file/my_file.v001.jpg
out: my_file.jpg
:param path: The path to a file, likely one to be published.
:param sequence: If True, treat the path as a sequence name and replace
the frame number with placeholder
:return: A publish display name for the provided path.
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_publish_name",
path=path,
sequence=sequence
)
def get_version_number(path):
"""
Extract a version number from the supplied path.
This is used by plugins that need to know what version number to associate
with the file when publishing.
Example::
in: /path/to/the/file/my_file.v001.jpg
out: 1
:param path: The path to a file, likely one to be published.
:return: An integer representing the version number in the supplied path.
If no version found, ``None`` will be returned.
"""
# the logic for this method lives in a hook that can be overridden by
# clients. exposing the method here in the publish utils api prevents
# clients from having to call other hooks directly in their
# collector/publisher hook implementations.
publisher = sgtk.platform.current_bundle()
return publisher.execute_hook_method(
"path_info",
"get_version_number",
path=path
)
# ---- publish util functions
def get_conflicting_publishes(context, path, publish_name, filters=None):
"""
Returns a list of SG published file dicts for any existing publishes that
match the supplied context, path, and publish_name.
:param context: The context to search publishes for
:param path: The path to match against previous publishes
:param publish_name: The name of the publish.
:param filters: A list of additional SG find() filters to apply to the
publish search.
:return: A list of ``dict``s representing existing publishes that match
the supplied arguments. The paths returned are the standard "id", and
"type" as well as the "path" field.
This method is typically used by publish plugin hooks to determine if there
are existing publishes for a given context, publish_name, and path and
warning appropriately.
"""
publisher = sgtk.platform.current_bundle()
logger.debug(
"Getting conflicting publishes for context: %s, path: %s, name: %s" %
(context, path, publish_name)
)
# ask core to do a dry_run of a publish with the supplied criteria. this is
# a workaround for our inability to filter publishes by path. so for now,
# get a dictionary of data that would be used to create a matching publish
# and use that to get publishes via a call to find(). Then we'll filter
# those by their path field. Once we have the ability in SG to filter by
# path, we can replace this whole method with a simple call to find().
publish_data = sgtk.util.register_publish(
publisher.sgtk,
context,
path,
publish_name,
version_number=None,
dry_run=True
)
logger.debug("Publish dry run data: %s" % (publish_data,))
# now build up the filters to match against
publish_filters = [filters] if filters else []
for field in ["code", "entity", "name", "project", "task"]:
publish_filters.append([field, "is", publish_data[field]])
logger.debug("Build publish filters: %s" % (publish_filters,))
# run the
publishes = publisher.shotgun.find(
"PublishedFile",
publish_filters,
["path"]
)
# ensure the path is normalized for comparison
normalized_path = sgtk.util.ShotgunPath.normalize(path)
# on windows compare paths case insensitively
if sys.platform == "win32":
normalized_path = normalized_path.lower()
# next, extract the publish path from each of the returned publishes and
# compare it against the supplied path. if the paths match, we add the
# publish to the list of publishes to return.
logger.debug("Comparing publish paths...")
matching_publishes = []
for publish in publishes:
publish_path = sgtk.util.resolve_publish_path(publisher.sgtk, publish)
if publish_path:
# ensure the published path is normalized for comparison
normalized_publish_path = sgtk.util.ShotgunPath.normalize(
publish_path)
if sys.platform == "win32":
normalized_publish_path = normalized_publish_path.lower()
if normalized_path == normalized_publish_path:
matching_publishes.append(publish)
return matching_publishes
def clear_status_for_conflicting_publishes(context, publish_data):
"""
Clear the status of any conflicting publishes matching the supplied publish
data.
The loader app respects the status of publishes to determine which are
available for the user to load in their DCC. Because it is possible to
create a version entry in SG with the same path multiple times, this method
provides an easy way to clear the status of previous publishes for a given
path.
The publish data supplied should be the fully populated publish data
returned by a call to ``sgtk.util.register_publish()``.
:param publish_data: Dictionary of the current publish data (i.e. the
publish entry whose status will not be cleared).
"""
publisher = sgtk.platform.current_bundle()
logger.debug("Clearing the status of any conflicting publishes.")
# determine the path from the publish data. this will match the path that
# was used to register the publish
path = sgtk.util.resolve_publish_path(publisher.sgtk, publish_data)
name = publish_data["name"]
# get a list of all publishes matching this criteria
publishes = get_conflicting_publishes(
context,
path,
name,
filters=["sg_status_list", "is_not", None]
)
if not publishes:
# no conflicting publishes. nothing to do.
logger.debug("No conflicting publishes detected for path: %s" % (path,))
return
# do a batch update of the conflicting publishes to clear their status
batch_data = []
for publish in publishes:
# make sure we don't update the supplied publish
if publish["id"] == publish_data["id"]:
continue
# add the update info to the batch data list
batch_data.append({
"request_type": "update",
"entity_type": publish["type"],
"entity_id": publish["id"],
"data": {"sg_status_list": None} # will clear the status
})
if batch_data:
logger.debug(
"Batch updating publish data: %s" %
(pprint.pformat(batch_data),)
)
# execute all the updates!
publisher.shotgun.batch(batch_data)