forked from GafferHQ/gaffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaffer.py
executable file
·180 lines (154 loc) · 6.65 KB
/
gaffer.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
#!/usr/bin/env python2.6
##########################################################################
#
# Copyright (c) 2011, John Haddon. All rights reserved.
# Copyright (c) 2011-2012, Image Engine Design Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with
# the distribution.
#
# * Neither the name of John Haddon nor the names of
# any other contributors to this software may be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################
import os
import sys
import ctypes
import signal
import warnings
# Work around cross module rtti errors on linux.
sys.setdlopenflags( sys.getdlopenflags() | ctypes.RTLD_GLOBAL )
# Get rid of the annoying signal handler which turns Ctrl-C into a KeyboardInterrupt exception
signal.signal( signal.SIGINT, signal.SIG_DFL )
# Reenable deprecation warnings - Python2.7 turns them off by default so otherwise we'd never get
# to catch all the naughty deprecated things we do.
warnings.simplefilter( "default", DeprecationWarning )
import IECore
helpText = """Usage :
gaffer -help Print this message
gaffer -help appName Print help specific to named application
gaffer appName [flags] Run named application with specified flags
gaffer fileName.gfr [flags] Run gui application with specified script and flags
gaffer [flags] Run gui application with specified flags
"""
appLoader = IECore.ClassLoader.defaultLoader( "GAFFER_APP_PATHS" )
applicationText = "Installed applications :\n\n" + "\n".join( " " + x for x in appLoader.classNames() ) + "\n"
def loadApp( appName ) :
if appName in appLoader.classNames() :
return appLoader.load( appName )()
else :
sys.stderr.write( "ERROR : Application \"%s\" not installed on GAFFER_APP_PATHS\n" % appName )
sys.stderr.write( "\n" + applicationText )
sys.exit( 1 )
def checkCleanExit() :
# Get the Gaffer and GafferUI modules, but only if the app actually
# imported them. We don't want to force their importation because it's
# just a waste of time if they weren't used.
Gaffer = sys.modules.get( "Gaffer" )
GafferUI = sys.modules.get( "GafferUI" )
if Gaffer is None and GafferUI is None :
return
# Clean up any garbage left behind by Cortex's wrapper mechanism - because
# the Gaffer.Application itself is derived from IECore.Parameterised, which
# as far as I can tell is wrapped unnecessarily, we must call this to allow
# the application to be deleted at all. Note that we're deliberately not also
# calling gc.collect() - our intention here isn't to clean up on shutdown, but
# to highlight problems caused by things not cleaning up after themselves during
# execution. We aim to eliminate all circular references from our code, to avoid
# garbage collection overhead and to avoid problems caused by referencing Qt widgets
# which were long since destroyed in C++.
## \todo Reevaluate the need for this call after Cortex 9 development.
IECore.RefCounted.collectGarbage()
# Importing here rather than at the top of the file prevents false
# positives being reported in gc.get_objects() below. I have no idea why,
# but if not imported here, get_objects() will report objects which have
# nothing referring to them and which should be dead, even with an
# explicit call to gc.collect() beforehand.
import gc
# Check for things that shouldn't exist at shutdown, and
# warn of anything we find.
scriptNodes = []
widgets = []
for o in gc.get_objects() :
if Gaffer is not None and isinstance( o, Gaffer.ScriptNode ) :
scriptNodes.append( o )
elif GafferUI is not None and isinstance( o, GafferUI.Widget ) :
widgets.append( o )
if scriptNodes :
IECore.msg(
IECore.Msg.Level.Debug,
"Gaffer shutdown", "%d remaining ScriptNode%s detected. Debugging with objgraph is recommended." % (
len( scriptNodes ),
"s" if len( scriptNodes ) > 1 else "",
)
)
if widgets :
count = {}
for widget in widgets :
widgetType = widget.__class__.__name__
count[widgetType] = count.get( widgetType, 0 ) + 1
summaries = [ "%s (%d)" % ( k, count[k] ) for k in sorted( count.keys() ) ]
IECore.msg(
IECore.Msg.Level.Debug,
"Gaffer shutdown", "%d remaining Widget%s detected : \n\n%s\n\nDebugging with objgraph is recommended." % (
len( widgets ),
"s" if len( widgets ) > 1 else "",
"\t" + "\n\t".join( summaries )
)
)
args = sys.argv[1:]
if args and args[0] in ( "-help", "-h", "--help", "-H" ) :
if len( args ) > 1 :
app = loadApp( args[1] )
formatter = IECore.WrappedTextFormatter( sys.stdout )
formatter.paragraph( "Name : " + app.path )
if app.description :
formatter.paragraph( app.description + "\n" )
if len( app.parameters().values() ):
formatter.heading( "Parameters" )
formatter.indent()
for p in app.parameters().values() :
IECore.formatParameterHelp( p, formatter )
formatter.unindent()
sys.exit( 0 )
else :
sys.stdout.write( helpText )
sys.stdout.write( "\n" + applicationText )
sys.exit( 0 )
else :
appName = "gui"
appArgs = args
if len( args ) :
if not args[0].startswith( "-" ) and not args[0].endswith( ".gfr" ) :
appName = args[0]
appArgs = args[1:]
app = loadApp( appName )
IECore.ParameterParser().parse( appArgs, app.parameters() )
result = app.run()
del app
checkCleanExit()
sys.exit( result )