-
Notifications
You must be signed in to change notification settings - Fork 1
/
integrate_assets.py
104 lines (78 loc) · 3.54 KB
/
integrate_assets.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
import os
import shutil
import pyblish.api
import pyblish_magenta.api
import pyblish_magenta.schema
class IntegrateAssets(pyblish.api.Integrator):
"""Name and position instances on disk"""
label = "Assets"
def process(self, context, instance):
self.log.debug("Source file: %s" % context.data("currentFile"))
current_file = context.data("currentFile").replace("\\", "/")
publish_dir = self.compute_publish_directory(current_file)
versions_dir = os.path.join(publish_dir,
instance.data("family"),
instance.data("name"))
# Compute next version for this instance
try:
existing_versions = os.listdir(versions_dir)
version = pyblish_magenta.api.find_next_version(existing_versions)
except OSError:
version = 1
version = pyblish_magenta.api.format_version(version)
version_dir = "{versions}/{version}".format(
versions=versions_dir,
version=version)
# Copy files/directories from the temporary
# extraction directory to the integration directory.
extract_dir = instance.data("extractDir")
if not extract_dir:
return self.log.debug("Skipping %s; no files found" % instance)
for fname in os.listdir(extract_dir):
src = os.path.join(extract_dir, fname)
dst = version_dir
if os.path.isfile(src):
try:
os.makedirs(dst)
except OSError:
pass
# Assembly fully-qualified name
# E.g. thedeal_seq01_1000_animation_ben01_v002.ma
_, ext = os.path.splitext(fname)
filename = "{topic}_{version}_{instance}".format(
topic="_".join(os.environ["TOPICS"].split()),
instance=instance.data("name"),
version=version) + ext
dst = os.path.join(dst, filename)
self.log.info("Copying file \"%s\" to \"%s\"" % (src, dst))
shutil.copy(src, dst)
else:
dst = os.path.join(dst, fname)
self.log.info("Copying directory \"%s\" to \"%s\""
% (src, dst))
shutil.copytree(src, dst)
# Store reference for further integration
instance.set_data("integrationDir", version_dir)
self.log.info("Integrated to directory \"{0}\"".format(version_dir))
def compute_publish_directory(self, path):
"""Given the current file, determine where to publish
Arguments:
path (str): Absolute path to the current working file
"""
self.log.debug("Loading schema..")
schema = pyblish_magenta.schema.load()
self.log.debug("Parsing with current file: %s" % path)
data, template = schema.parse(path)
# TOPICS are a space-separated list of user-supplied topics
# E.g. "thedeal seq01 1000 animation"
task = os.environ["TOPICS"].split()[-1]
assert task == data["task"], (
"Task set in environment ({env}) is not the same as "
"the one parsed ({data})".format(
env=task,
data=data["task"]))
self.log.info("Retrieving template from schema..")
pattern = schema.get(template.name.rsplit(
".work", 1)[0] + ".publish")
self.log.info("Got \"%s\": formatting with %s" % (pattern, data))
return pattern.format(data)