Skip to content

Commit a979d6b

Browse files
authored
Support dev environments on Windows (#336)
* Support dev environments on Windows * Fix wrong condition
1 parent b402e0b commit a979d6b

1 file changed

Lines changed: 121 additions & 88 deletions

File tree

bindings/Sofa/package/__init__.py

Lines changed: 121 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Package containg the binding for the core of Sofa
2+
Package containing the binding for the core of Sofa
33
-------------------------------------------------
44
55
Example of use:
@@ -44,14 +44,14 @@
4444
else:
4545
print("Warning: environment variable SOFA_ROOT is empty. Trying to guess it.")
4646
# try a guess from <sofa_root>/plugins/SofaPython3/lib/python3/site-packages/Sofa
47-
sofa_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../../../..')
47+
sofa_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../../../..')
4848
if os.path.isdir(os.path.abspath(sofa_root_guess + '/lib' )):
4949
print("Guessed SOFA_ROOT: " + sofa_root_guess)
5050
sofa_root = sofa_root_guess
5151
os.environ["SOFA_ROOT"] = sofa_root
5252
else:
53-
print("Warning: cannot guess SOFA_ROOT",
54-
"Loading SOFA libraries will likely fail and/or SOFA won't find its resources.")
53+
print("Warning: cannot guess SOFA_ROOT",
54+
"Loading SOFA libraries will likely fail and/or SOFA won't find its resources.")
5555

5656
if sofa_root and sys.platform == 'win32':
5757

@@ -62,31 +62,64 @@
6262
else:
6363
print("Warning: environment variable SOFAPYTHON3_ROOT is empty. Trying to guess it.")
6464
# try a guess from <sofapython3_root>/lib/python3/site-packages/Sofa
65-
sofapython3_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../..')
65+
sofapython3_root_guess = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../../../..')
6666
if os.path.isdir(os.path.abspath(sofapython3_root_guess + '/lib' )):
6767
print("Guessed SOFAPYTHON3_ROOT: " + sofapython3_root_guess)
6868
sofapython3_root = sofapython3_root_guess
6969
os.environ["SOFAPYTHON3_ROOT"] = sofapython3_root
7070
else:
71-
print("Warning: cannot guess SOFAPYTHON3_ROOT",
72-
"Loading SofaPython3 modules will likely fail.")
71+
print("Warning: cannot guess SOFAPYTHON3_ROOT",
72+
"Loading SofaPython3 modules will likely fail.")
7373

74-
# Windows-only: starting from python 3.8, python wont read the env. variable PATH to get SOFA's dlls.
74+
# Windows-only: starting from python 3.8, python wont read the env. variable PATH to get SOFA's dlls.
7575
# os.add_dll_directory() is the new way to add paths for python to get external libraries.
76-
sofa_bin_path = sofa_root + "\\bin"
77-
sofa_file_test = sofa_bin_path + "\\Sofa.Helper.dll"
78-
sofapython3_bin_path = sofapython3_root + "\\bin"
79-
sofapython3_file_test = sofapython3_bin_path + "\\SofaPython3.dll"
80-
76+
sofa_bin_path = os.path.join(sofa_root, "bin")
77+
sofapython3_bin_path = os.path.join(sofapython3_root, "bin")
78+
79+
compilation_modes = ["Release", "RelWithDebInfo", "Debug", "MinSizeRel"]
80+
sofa_bin_compilation_modes = []
81+
sofapython3_bin_compilation_modes = []
82+
for mode in compilation_modes:
83+
if os.path.isdir(os.path.abspath(os.path.join(sofa_bin_path, mode))):
84+
sofa_bin_compilation_modes.append(os.path.join(sofa_bin_path, mode))
85+
if os.path.isdir(os.path.abspath(os.path.join(sofapython3_bin_path, mode))):
86+
sofapython3_bin_compilation_modes.append(os.path.join(sofapython3_bin_path, mode))
87+
88+
if sofa_bin_compilation_modes:
89+
print("Detected SOFA development build")
90+
if sofapython3_bin_compilation_modes:
91+
print("Detected SofaPython3 development build")
92+
93+
sofa_bin_candidates = [sofa_bin_path] + sofa_bin_compilation_modes
94+
sofapython3_bin_candidates = [sofapython3_bin_path] + sofapython3_bin_compilation_modes
95+
96+
for candidate in sofa_bin_candidates:
97+
sofa_file_test = os.path.join(candidate, "Sofa.Helper.dll")
98+
if os.path.isfile(sofa_file_test):
99+
print("Found Sofa.Helper.dll in " + candidate)
100+
sofa_bin_path = candidate
101+
break
102+
103+
sofa_file_test = os.path.join(sofa_bin_path, "Sofa.Helper.dll")
104+
105+
for candidate in sofapython3_bin_candidates:
106+
sofapython3_file_test = os.path.join(candidate, "SofaPython3.dll")
107+
if os.path.isfile(sofapython3_file_test):
108+
print("Found SofaPython3.dll in " + candidate)
109+
sofapython3_bin_path = candidate
110+
break
111+
112+
sofapython3_file_test = os.path.join(sofapython3_bin_path, "SofaPython3.dll")
113+
81114
if not os.path.isfile(sofa_file_test):
82115
print("Warning: environment variable SOFA_ROOT is set but seems invalid.",
83-
"Loading SOFA libraries will likely fail.")
116+
"Loading SOFA libraries will likely fail.")
84117
print("SOFA_ROOT is currently: " + sofa_root)
85118
if not os.path.isfile(sofapython3_file_test):
86119
print("Warning: cannot find SofaPython3.dll at path: " + sofapython3_bin_path)
87-
print("This path will NOT be added to the DLL search path.",
88-
"Loading SofaPython3 python modules will likely fail.")
89-
120+
print("This path will NOT be added to the DLL search path.",
121+
"Loading SofaPython3 python modules will likely fail.")
122+
90123
if sys.version_info.minor >= 8:
91124
# Starting from python3.8 we need to explicitly find SOFA libraries
92125
if os.path.isfile(sofa_file_test):
@@ -216,9 +249,9 @@ def sofaExceptHandler(type, value, tb):
216249

217250
if str(value) != '':
218251
h += ': ' + str(value)
219-
252+
220253
s = ''.join(traceback.format_tb(tb))
221-
254+
222255
Sofa.Helper.msg_error(h + '\n' + s, "line", 7)
223256

224257
sys.excepthook=sofaExceptHandler
@@ -259,75 +292,75 @@ def msg_deprecated(target, message):
259292

260293
import inspect
261294
def PrefabBuilder(f):
262-
frameinfo = inspect.getframeinfo(inspect.currentframe().f_back)
263-
definedloc = (frameinfo.filename, frameinfo.lineno)
264-
265-
def SofaPrefabF(*args, **kwargs):
266-
class InnerSofaPrefab(Sofa.Core.RawPrefab):
267-
def __init__(self, *args, **kwargs):
268-
Sofa.Core.RawPrefab.__init__(self, *args, **kwargs)
269-
self.isValid = True
270-
271-
def doReInit(self):
272-
if not self.isValid:
273-
return
274-
try:
275-
argnames = inspect.getfullargspec(f).args
276-
277-
kkwargs = {}
278-
kkwargs["self"] = self
279-
for name in argnames[:]:
280-
if name != "self":
281-
kkwargs[name] = self.__data__[name].value
282-
283-
self.cb(**kkwargs)
284-
except Exception as e:
285-
self.isValid = False
286-
exc_type, exc_value, exc_tb = sys.exc_info()
287-
Sofa.Helper.msg_error(self, "Unable to build prefab \n "+getSofaFormattedStringFromException(e))
288-
try:
289-
selfnode = None
290-
kwargs["name"] = kwargs.get("name", f.__code__.co_name)
291-
selfnode = InnerSofaPrefab(*args, **kwargs)
292-
selfnode.setDefinitionSourceFileName(definedloc[0])
293-
selfnode.setDefinitionSourceFilePos(definedloc[1])
294-
selfnode.setSourceTracking(definedloc[0])
295-
selfnode.cb = f
296-
## retrieve meta data from decorated class:
297-
selfnode.addData(name="prefabname", value=f.__code__.co_name,
298-
type="string", help="The prefab's name", group="Infos")
299-
selfnode.addData(name="docstring", value=f.__doc__,
300-
type="string", help="This prefab's docstring", group="Infos")
301-
302-
## Now we retrieve all params passed to the prefab and add them as datafields:
303-
argnames = inspect.getfullargspec(f).args
304-
defaults = inspect.getfullargspec(f).defaults
305-
306-
if argnames is None:
307-
argnames = []
308-
defaults = []
309-
310-
if defaults is None:
311-
defaults = []
312-
313-
i = len(argnames) - len(defaults)
314-
for n in range(0, len(defaults)):
315-
if argnames[i+n] not in selfnode.__data__:
316-
if pyType2sofaType(defaults[n]) != None:
317-
selfnode.addPrefabParameter(name=argnames[i+n],
295+
frameinfo = inspect.getframeinfo(inspect.currentframe().f_back)
296+
definedloc = (frameinfo.filename, frameinfo.lineno)
297+
298+
def SofaPrefabF(*args, **kwargs):
299+
class InnerSofaPrefab(Sofa.Core.RawPrefab):
300+
def __init__(self, *args, **kwargs):
301+
Sofa.Core.RawPrefab.__init__(self, *args, **kwargs)
302+
self.isValid = True
303+
304+
def doReInit(self):
305+
if not self.isValid:
306+
return
307+
try:
308+
argnames = inspect.getfullargspec(f).args
309+
310+
kkwargs = {}
311+
kkwargs["self"] = self
312+
for name in argnames[:]:
313+
if name != "self":
314+
kkwargs[name] = self.__data__[name].value
315+
316+
self.cb(**kkwargs)
317+
except Exception as e:
318+
self.isValid = False
319+
exc_type, exc_value, exc_tb = sys.exc_info()
320+
Sofa.Helper.msg_error(self, "Unable to build prefab \n "+getSofaFormattedStringFromException(e))
321+
try:
322+
selfnode = None
323+
kwargs["name"] = kwargs.get("name", f.__code__.co_name)
324+
selfnode = InnerSofaPrefab(*args, **kwargs)
325+
selfnode.setDefinitionSourceFileName(definedloc[0])
326+
selfnode.setDefinitionSourceFilePos(definedloc[1])
327+
selfnode.setSourceTracking(definedloc[0])
328+
selfnode.cb = f
329+
## retrieve meta data from decorated class:
330+
selfnode.addData(name="prefabname", value=f.__code__.co_name,
331+
type="string", help="The prefab's name", group="Infos")
332+
selfnode.addData(name="docstring", value=f.__doc__,
333+
type="string", help="This prefab's docstring", group="Infos")
334+
335+
## Now we retrieve all params passed to the prefab and add them as datafields:
336+
argnames = inspect.getfullargspec(f).args
337+
defaults = inspect.getfullargspec(f).defaults
338+
339+
if argnames is None:
340+
argnames = []
341+
defaults = []
342+
343+
if defaults is None:
344+
defaults = []
345+
346+
i = len(argnames) - len(defaults)
347+
for n in range(0, len(defaults)):
348+
if argnames[i+n] not in selfnode.__data__:
349+
if pyType2sofaType(defaults[n]) != None:
350+
selfnode.addPrefabParameter(name=argnames[i+n],
318351
default=kwargs.get(argnames[i+n], defaults[n]),
319352
type=pyType2sofaType(defaults[n]), help="Undefined")
320-
else:
321-
Sofa.Helper.msg_error("Missing type for parameters: ", argnames[i+n])
322-
selfnode.init()
323-
324-
except Exception as e:
325-
if selfnode is not None:
326-
selfnode.isValid=False
327-
Sofa.Helper.msg_error(selfnode, "Unable to create prefab because: "+getSofaFormattedStringFromException(e))
328-
else:
329-
Sofa.Helper.msg_error("PrefabBuilder", "Unable to create prefab because: "+getSofaFormattedStringFromException(e))
330-
return selfnode
331-
SofaPrefabF.__dict__["__original__"] = f
332-
return SofaPrefabF
353+
else:
354+
Sofa.Helper.msg_error("Missing type for parameters: ", argnames[i+n])
355+
selfnode.init()
356+
357+
except Exception as e:
358+
if selfnode is not None:
359+
selfnode.isValid=False
360+
Sofa.Helper.msg_error(selfnode, "Unable to create prefab because: "+getSofaFormattedStringFromException(e))
361+
else:
362+
Sofa.Helper.msg_error("PrefabBuilder", "Unable to create prefab because: "+getSofaFormattedStringFromException(e))
363+
return selfnode
364+
SofaPrefabF.__dict__["__original__"] = f
365+
return SofaPrefabF
333366

0 commit comments

Comments
 (0)