Skip to content

Commit

Permalink
Split out code into function for added paths to environment variables
Browse files Browse the repository at this point in the history
Change how tool paths are indicated for OSR so that paths with spaces can be used.
Remove debug messages from serializer test options
Fix bug in create_windows_import test
  • Loading branch information
langmm committed Jun 27, 2024
1 parent f8857b9 commit a4701c3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
5 changes: 3 additions & 2 deletions tests/drivers/test_CompiledModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ def test_create_windows_import_gcc():
default=None)
if gcc:
kws = {'toolname': 'gcc'}
filetype = 'library'
if platform._is_win:
kws['libtype'] = 'shared'
dll = CModelDriver.libraries.getfile('python', 'library', **kws)
filetype = 'shared'
dll = CModelDriver.libraries.getfile('python', filetype, **kws)
if platform._is_win:
assert dll.endswith('.dll')
CompiledModelDriver.create_windows_import(dll, for_gnu=True,
Expand Down
25 changes: 11 additions & 14 deletions yggdrasil/drivers/CompiledModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2775,6 +2775,14 @@ def specialized(self, **kwargs):
out.specialization = DependencySpecialization(self)
out.specialization.update(out, **kwargs)
if out.origin == 'language':
if out.name != out.basetool.standard_library:
out.logInfo(f"Dependency {out.name} is language related, "
f"but not the {out.basetool.tooltype}'s "
f"standard library "
f"\"{out.basetool.standard_library}\" "
f"({out.basetool.tooltype} = "
f"{out.basetool.toolname})",
level=logging.ERROR)
assert out.name == out.basetool.standard_library
if target_dep:
out.parameters['target_dep'] = target_dep.specialized(
Expand Down Expand Up @@ -3754,20 +3762,9 @@ def _shared_path_env(self, to_update=None, out=None,
paths_to_add += self.tool('linker').get_search_path(
env_only=True)
if env_var and paths_to_add:
path_list = []
prev_path = to_update.get(env_var, '')
prev_path_list = []
if prev_path:
prev_path_list += prev_path.split(os.pathsep)
path_list.append(prev_path)
for x in paths_to_add:
if x and x not in prev_path_list:
if add_to_front:
path_list.insert(0, x)
else:
path_list.append(x)
if path_list:
out[env_var] = os.pathsep.join(path_list)
tools.update_path_env(env_var, paths_to_add, env=out,
add_to_front=add_to_front,
src_env=to_update)
return out

@classmethod
Expand Down
4 changes: 3 additions & 1 deletion yggdrasil/drivers/MakeModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,7 @@ def fix_path(cls, path, for_env=False, is_gnu=False, **kwargs):
if platform._is_win and for_env:
if is_gnu:
out = out.replace('\\', re.escape('/'))
out = f'"{out}"'
assert ' ' not in out
if ' ' in out:
out = f'"{out}"'
return out
11 changes: 7 additions & 4 deletions yggdrasil/drivers/OSRModelDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ def compile_dependencies(cls, target='OpenSimRootYgg', toolname=None,
logger.info(f"OSR compiler: {cl_path}\n"
f"OSR linker: {link_path}")
if cl_path:
msvc_bin = os.path.dirname(cl_path)
env['YGG_OSR_CXX'] = cl_path
env['YGG_OSR_LINK'] = os.path.join(
msvc_bin, 'link.exe')
paths_to_add = os.pathsep.join(list(set([
os.path.dirname(cl_path),
os.path.dirname(link_path)])))
env['YGG_OSR_CXX'] = os.path.basename(cl_path)
env['YGG_OSR_LINK'] = os.path.basename(link_path)
tools.update_path_env("PATH", paths_to_add,
env=env, add_to_front=True)
for k in ['YGG_OSR_CXX', 'YGG_OSR_LINK',
'CL', '_CL_']:
v = env.get(k, None)
Expand Down
4 changes: 0 additions & 4 deletions yggdrasil/serialize/SerializeBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ def get_testing_options(cls, table_example=False, array_columns=False,
arr = np.array(rows, dtype=dtype)
if no_names:
arr = [arr[n] for n in arr.dtype.names]
import pprint
print("BEFORE POSSIBLE SEGFAULT")
pprint.pprint(arr)
pprint.pprint(out['datatype'])
lst = rapidjson.normalize(arr, out['datatype'])
out['objects'] = [lst, lst]
else:
Expand Down
35 changes: 35 additions & 0 deletions yggdrasil/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,41 @@ def is_git_repository(path):
return True


def update_path_env(env_var, paths_to_add, env=None, add_to_front=False,
src_env=None):
r"""Add a set of paths to an environment variable.
Args:
env_var (str): Environment variable to update
paths_to_add (list): Paths to add if not present.
env (dict, optional): Dictionary of environment variables to
update if not provided, os.environ will be used.
add_to_front (bool, optional): If True, added paths should be
added before any existing paths.
src_env (dict, optional): Environment variable set that existing
paths should be pulled from if different from env.
"""
if env is None:
env = os.environ
if src_env is None:
src_env = env
path_list = []
prev_path = src_env.get(env_var, '')
prev_path_list = []
if prev_path:
prev_path_list += prev_path.split(os.pathsep)
path_list.append(prev_path)
for x in paths_to_add:
if x and x not in prev_path_list:
if add_to_front:
path_list.insert(0, x)
else:
path_list.append(x)
if path_list:
env[env_var] = os.pathsep.join(path_list)


def display_source(fname, number_lines=False, return_lines=False):
r"""Display source code with syntax highlighting (if available).
Expand Down

0 comments on commit a4701c3

Please sign in to comment.