-
Notifications
You must be signed in to change notification settings - Fork 12
/
startup.py
345 lines (292 loc) · 13.4 KB
/
startup.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
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
# Copyright (c) 2016 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.
from __future__ import absolute_import
import os
import platform
import re
import sgtk
from sgtk import TankError
from sgtk.platform import SoftwareLauncher, SoftwareVersion, LaunchInformation
class FlameLauncher(SoftwareLauncher):
"""
Handles launching Flame executables. Automatically starts up a tk-flame
engine with the current context in the new session of Houdini.
"""
# A lookup to map an executable name to a product. This is critical for
# linux where the product does not show up in the path.
EXECUTABLE_TO_PRODUCT = {
"flame": "Flame",
"flameassist": "Flame Assist",
"flare": "Flare",
"flamepremium": "Flame Premium",
}
# lookup for icons
ICON_LOOKUP = {
"Flame": "icon_256.png",
"Flame Assist": "flame_assist_icon_256.png",
"Flare": "flare_icon_256.png",
"Flame Premium": "icon_256.png",
}
# Named regex strings to insert into the executable template paths when
# matching against supplied versions and products. Similar to the glob
# strings, these allow us to alter the regex matching for any of the
# variable components of the path in one place
COMPONENT_REGEX_LOOKUP = {
"Darwin": {
"version": r"\d.*", # starts with a number followed by anything
"executable": r"[\w]+", # word characters (a-z0-9)
},
"Linux": {
"version": r"\d.*", # starts with a number followed by anything
"executable": r"[\w]+", # word characters (a-z0-9)
},
}
# This dictionary defines a list of executable template strings for each
# of the supported operating systems. The templates are used for both
# globbing and regex matches by replacing the named format placeholders
# with an appropriate glob or regex string.
EXECUTABLE_TEMPLATES = {
"Darwin": [
"/opt/Autodesk/{executable}_{version}/bin/startApplication",
],
"Linux": [
"/usr/discreet/{executable}_{version}/bin/startApplication",
"/opt/Autodesk/{executable}_{version}/bin/startApplication",
],
}
@property
def minimum_supported_version(self):
"""
The minimum supported Flame version.
"""
# 2018 was the first version of flame that shipped with taap included.
return "2018"
def prepare_launch(self, exec_path, args, file_to_open=None):
"""
Prepares the given software for launch
:param str exec_path: Path to DCC executable to launch
:param str args: Command line arguments as strings
:param str file_to_open: (optional) Full path name of a file to open on
launch
:returns: :class:`LaunchInformation` instance
"""
use_builtin_plugin = self.get_setting("use_builtin_plugin")
# If there is a plugin to launch with, we don't have much in the
# way of prep work to do.
if use_builtin_plugin:
# flame comes with toolkit built-in, so no need to
# run any startup logic.
self.logger.debug("Using the builtin plugin on Flame launch.")
env = {
"SHOTGUN_SITE": self.sgtk.shotgun_url,
"SHOTGUN_ENTITY_ID": str(self.context.project["id"]),
"SHOTGUN_ENTITY_TYPE": str(self.context.project["type"]),
"SHOTGUN_ENTITY_NAME": str(self.context.project["name"]),
}
else:
self.logger.debug("Using the legacy bootstrap on Flame launch.")
# We have a list of environment variables that we need to
# gather and return. These are various bits of data that
# the python/startup/app_launcher.py script uses during
# its bootstrapping of SGTK during launch.
env = {
"TOOLKIT_ENGINE_NAME": self.engine_name,
"TOOLKIT_CONTEXT": sgtk.context.serialize(self.context),
}
# We also need to store the various components of the Flame
# version in the environment. The app_launcher.py script that
# launches and bootstraps SGTK registers these with the engine,
# and the engine then logs metrics about what version of Flame
# has been launched.
#
# If defined manually in the Software Entity list, the exec_path is
# can also be a path to the .app that will be launched.
# What we need instead of is the fully-qualified path to the Flame
# startApplication executable, which is what we'll extract the
# version components from. Examples of what this path can be and
# how it's parsed can be found in the docstring of _get_flame_version
# method.
self.logger.debug("Flame app executable: %s", exec_path)
if exec_path.endswith(".app"):
if os.path.islink(exec_path):
exec_path = os.readlink(exec_path)
self.logger.debug(
"Parsing Flame (%s) to determine Flame version...", exec_path
)
major, minor, patch, version_str = self._get_flame_version(exec_path)
self.logger.debug("Found Flame version: %s", version_str)
env.update(
dict(
TOOLKIT_FLAME_VERSION=version_str,
TOOLKIT_FLAME_MAJOR_VERSION=str(major),
TOOLKIT_FLAME_MINOR_VERSION=str(minor),
TOOLKIT_FLAME_PATCH_VERSION=str(patch),
)
)
# The install root of Flame is also used by the app_launcher
# script, which registers it with the engine after bootstrapping.
# The path is used by the engine when submitting render jobs
# to Backburner.
match = re.search("(^.*)/(fla[mr]e[^_]*_[^/]+)/bin", exec_path)
if match:
env["TOOLKIT_FLAME_INSTALL_ROOT"] = match.group(1)
app_folder = match.group(2)
wiretap_path = os.path.join(
env["TOOLKIT_FLAME_INSTALL_ROOT"],
app_folder,
"python",
)
self.logger.debug(
"Adding wiretap root path to PYTHONPATH: %s", wiretap_path
)
sgtk.util.prepend_path_to_env_var("PYTHONPATH", wiretap_path)
else:
raise TankError(
"Cannot extract install root from the path: %s" % exec_path
)
# The Python executable bundled with Flame is used by the engine
# when submitting Backburner jobs.
env["TOOLKIT_FLAME_PYTHON_BINARY"] = "%s/python/%s/bin/python" % (
env["TOOLKIT_FLAME_INSTALL_ROOT"],
version_str,
)
# We need to override the exec_path and args that will be used
# to launch Flame. We launch using the Python bundled with Flame
# and the app_launcher.py script bundled with the engine. That
# app_launcher script will do some prep work prior to launching
# Flame itself.
#
# <flame python> <tk-flame>/python/startup/app_launcher.py dcc_path dcc_args
#
launch_script = os.path.join(
os.path.dirname(__file__),
"python",
"startup",
"app_launcher.py",
)
args = "'%s' %s %s" % (launch_script, exec_path, args)
exec_path = env["TOOLKIT_FLAME_PYTHON_BINARY"]
return LaunchInformation(exec_path, args, env)
def scan_software(self):
"""
Scan the filesystem for flame executables.
:return: A list of :class:`SoftwareVersion` objects.
"""
self.logger.debug("Scanning for Flame executables...")
supported_sw_versions = []
for sw_version in self._find_software():
(supported, reason) = self._is_supported(sw_version)
if supported:
supported_sw_versions.append(sw_version)
else:
self.logger.debug(
"SoftwareVersion %s is not supported: %s" % (sw_version, reason)
)
return supported_sw_versions
def _find_software(self):
# all the executable templates for the current OS
os_name = platform.system()
executable_templates = self.EXECUTABLE_TEMPLATES.get(os_name, [])
executable_regexp = self.COMPONENT_REGEX_LOOKUP.get(os_name, [])
# all the discovered executables
sw_versions = []
for executable_template in executable_templates:
self.logger.debug("Processing template %s.", executable_template)
executable_matches = self._glob_and_match(
executable_template, executable_regexp
)
# Extract all products from that executable.
for (executable_path, key_dict) in executable_matches:
# extract the matched keys form the key_dict (default to None if
# not included)
executable_version = key_dict.get("version")
executable_product = key_dict.get("product")
executable_name = key_dict.get("executable")
executable_app = key_dict.get("app")
# we need a product to match against. If that isn't provided,
# then an executable name should be available. We can map that
# to the proper product.
if not executable_product:
executable_product = self.EXECUTABLE_TO_PRODUCT.get(executable_name)
# Unknown product
if not executable_product:
continue
# Adapt the FlameAssist product name
if executable_product == "FlameAssist":
executable_product = "Flame Assist"
# only include the products that are covered in the EXECUTABLE_TO_PRODUCT dict
if not executable_product.startswith(
"Flame"
) and not executable_product.startswith("Flare"):
self.logger.debug(
"Product '%s' is unrecognized. Skipping."
% (executable_product,)
)
continue
# exclude Technology demo apps
if executable_app and "Technology Demo" in executable_app:
self.logger.debug(
"Ignoring '%s %s - %s'"
% (executable_product, executable_version, executable_app)
)
continue
# figure out which icon to use
icon_path = os.path.join(
self.disk_location,
self.ICON_LOOKUP.get(executable_product, self.ICON_LOOKUP["Flame"]),
)
self.logger.debug("Using icon path: %s" % (icon_path,))
sw_versions.append(
SoftwareVersion(
executable_version,
executable_product,
executable_path,
icon_path,
)
)
return sw_versions
def _get_flame_version(self, flame_path):
"""
Returns the version string for the given Flame path
<INSTALL_ROOT>/flameassist_2016.2/bin/startApplication --> (2016, 2, 0, "2016.2")
<INSTALL_ROOT>/flameassist_2016.3/bin/startApplication --> (2016, 3, 0, "2016.3")
<INSTALL_ROOT>/flameassist_2016.0.3.322/bin/startApplication --> (2016, 0, 3, "2016.0.3.322")
<INSTALL_ROOT>/flameassist_2016.2.pr99/bin/startApplication --> (2016, 2, 0, "2016.2.pr99")
<INSTALL_ROOT>/flame_2016.pr50/bin/start_Flame --> (2016, 0, 0, "2016.pr50")
If the patch, minor or major version cannot be extracted, it will be set to zero.
:param flame_path: path to executable
:returns: (major, minor, patch, full_str)
"""
# do a quick check to ensure that we are running 2015.2 or later
re_match = re.search(r"/fla[mr]e[^_]*_([^/]+)/bin", flame_path)
if not re_match:
raise TankError(
"Cannot extract Flame version number from the path '%s'!" % flame_path
)
version_str = re_match.group(1)
# Examples:
# 2016
# 2016.2
# 2016.pr99
# 2015.2.pr99
major_ver = 0
minor_ver = 0
patch_ver = 0
chunks = version_str.split(".")
if len(chunks) > 0:
if chunks[0].isdigit():
major_ver = int(chunks[0])
if len(chunks) > 1:
if chunks[1].isdigit():
minor_ver = int(chunks[1])
if len(chunks) > 2:
if chunks[2].isdigit():
patch_ver = int(chunks[2])
return (major_ver, minor_ver, patch_ver, version_str)