Skip to content

Commit

Permalink
Merge pull request #185 from jbohren-forks/cache-resultspace
Browse files Browse the repository at this point in the history
optimization: caching resultspace environments
  • Loading branch information
wjwwood committed Apr 7, 2015
2 parents 0895b16 + 866e111 commit d1d2bea
Showing 1 changed file with 17 additions and 3 deletions.
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)

0 comments on commit d1d2bea

Please sign in to comment.