Skip to content

Instantly share code, notes, and snippets.

@jbohren
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save jbohren/ff52b3b5d73831f80c36 to your computer and use it in GitHub Desktop.

Select an option

Save jbohren/ff52b3b5d73831f80c36 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
use_catkin = True
if use_catkin:
from catkin.builder import run_command
else:
from catkin_tools.verbs.catkin_build.common import run_command
import sys
import os
import re
def load_workspace_environment(ws_path):
'''
Load the environemt variables which result from sourcing another
workspace path into this process's environment.
:param ws_path: The path to a workspace containing catkin setup files
'''
env_dict = get_workspace_environment(ws_path)
os.environ.update(env_dict)
print(env_dict)
def get_workspace_environment(ws_path):
'''
Get the environemt variables which result from sourcing another workspace
path as the string output of `cmake -E environment`.
:param ws_path: path to the workspace whose environment should be loaded,
``str``
:returns: a carriage-return-delimted string of environment variables, ``str``
'''
# Check to make sure ws_path is a valid directory
if not os.path.isdir(ws_path):
raise IOError(
"Cannot load setup file from path \"%s\" because it is not a"
"directory." % ws_path
)
# Check to make sure ws_path contains a `.catkin` file
if not os.path.exists(os.path.join(ws_path,'.catkin')):
raise IOError(
"Cannot load setup file from path \"%s\" because it is not a catkin"
"workspace (missing .catkin file)." % ws_path
)
# Determine the shell to use to source the setup file
shell_path = os.environ['SHELL']
(_, shell_name) = os.path.split(shell_path)
# TODO: Check shell_name against a list of supported shells
# Check to make sure ws_path contains the appropriate setup file
setup_file_path = os.path.join(ws_path,'setup.%s' % shell_name)
if not os.path.exists(setup_file_path):
raise IOError(
"Cannot load setup file \"%s\" because it does not exist." % setup_file_path
)
# Construct a command list which sources the setup file and prints the env to stdout
norc_flags = {'bash':'--norc', 'zsh':'-f'}
subcommand = 'source %s; cmake -E environment' % (setup_file_path)
command = [
shell_path,
norc_flags[shell_name],
'-c', subcommand]
# Run the command to source the other environment and output all environment variables
if use_catkin:
# Catkin run_command
env_str = run_command(command, os.getcwd(), quiet=True)
env_regex = re.compile('(.+?)=(.*)$', re.M)
env_dict = dict(env_regex.findall(env_str))
else:
# catkin_tools run_command
blacklisted_keys = ('_','PWD')
env_regex = re.compile('(.+?)=(.*)$', re.M)
env_dict = dict()
for line in run_command(command, cwd=os.getcwd()):
if isinstance(line,str):
matches = env_regex.findall(line)
env_dict.update(dict(matches))
for (key,value) in matches:
if key not in blacklisted_keys:
env_dict[key] = value.rstrip()
return env_dict
def main():
if len(sys.argv) < 2:
print('usage: %s WS_PATH' % sys.argv[0])
sys.exit(-1)
load_workspace_environment(sys.argv[1])
print('CMAKE_PREFIX_PATH is: %s' % os.getenv('CMAKE_PREFIX_PATH'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment