Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization: caching resultspace environments #185

Merged
merged 2 commits into from
Apr 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions catkin_tools/resultspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
CMAKE_EXEC = which('cmake')
SORT_EXEC = which('sort')

# Cache for result-space environments
_resultspace_env_cache = {}

def get_resultspace_environment(result_space_path, quiet=False):

def get_resultspace_environment(result_space_path, quiet=False, cached=True):
"""Get the environemt variables which result from sourcing another catkin
workspace's setup files as the string output of `cmake -E environment`.
This command is used to be as portable as possible.
Expand All @@ -19,9 +22,16 @@ def get_resultspace_environment(result_space_path, quiet=False):
:type result_space_path: str
:param quiet: don't throw exceptions, ``bool``
:type quiet: bool
:param cached: use the cached environment
:type cached: bool

:returns: a dictionary of environment variables and their values
"""

# Check the cache first
if cached and result_space_path in _resultspace_env_cache:
return _resultspace_env_cache[result_space_path]

# Check to make sure result_space_path is a valid directory
if not os.path.isdir(result_space_path):
if quiet:
Expand Down Expand Up @@ -94,15 +104,19 @@ def get_resultspace_environment(result_space_path, quiet=False):
print("WARNING: Failed to extract environment from resultspace: %s: %s" % (result_space_path, str(err)))
return {}

_resultspace_env_cache[result_space_path] = env_dict

return env_dict


def load_resultspace_environment(result_space_path):
def load_resultspace_environment(result_space_path, cached=True):
"""Load the environemt variables which result from sourcing another
workspace path into this process's environment.

:param result_space_path: path to a Catkin result-space whose environment should be loaded, ``str``
:type result_space_path: str
:param cached: use the cached environment
:type cached: bool
"""
env_dict = get_resultspace_environment(result_space_path)
env_dict = get_resultspace_environment(result_space_path, cached=cached)
os.environ.update(env_dict)