-
Notifications
You must be signed in to change notification settings - Fork 171
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
Set ROS_PYTHON_VERSION if unset #708
Merged
+344
−68
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0bfa27
Set ROS_PYTHON_VERSION if unset
sloretz 08df723
Move functions unchanged to new file
sloretz d72c4a3
Respect python_version in rosdistro index
sloretz f32222f
Depend rosdistro >= 0.7.5
sloretz 830db97
Add tests for MetaDatabase
sloretz 8a2f954
Fix tests
sloretz 5a3e8e8
Disable debug print()
sloretz dd4bf62
Ignore env when ROS_DISTRO and --rosdistro differ
sloretz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright (c) 2012, Willow Garage, Inc. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# * Neither the name of the Willow Garage, Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import hashlib | ||
import os | ||
import tempfile | ||
|
||
from .core import CachePermissionError | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
PICKLE_CACHE_EXT = '.pickle' | ||
|
||
|
||
def compute_filename_hash(key_filenames): | ||
sha_hash = hashlib.sha1() | ||
if isinstance(key_filenames, list): | ||
for key in key_filenames: | ||
sha_hash.update(key.encode()) | ||
else: | ||
sha_hash.update(key_filenames.encode()) | ||
return sha_hash.hexdigest() | ||
|
||
|
||
def write_cache_file(source_cache_d, key_filenames, rosdep_data): | ||
""" | ||
:param source_cache_d: directory to write cache file to | ||
:param key_filenames: filename (or list of filenames) to be used in hashing | ||
:param rosdep_data: dictionary of data to serialize as YAML | ||
:returns: name of file where cache is stored | ||
:raises: :exc:`OSError` if cannot write to cache file/directory | ||
:raises: :exc:`IOError` if cannot write to cache file/directory | ||
""" | ||
if not os.path.exists(source_cache_d): | ||
os.makedirs(source_cache_d) | ||
key_hash = compute_filename_hash(key_filenames) | ||
filepath = os.path.join(source_cache_d, key_hash) | ||
try: | ||
write_atomic(filepath + PICKLE_CACHE_EXT, pickle.dumps(rosdep_data, 2), True) | ||
except OSError as e: | ||
raise CachePermissionError('Failed to write cache file: ' + str(e)) | ||
try: | ||
os.unlink(filepath) | ||
except OSError: | ||
pass | ||
return filepath | ||
|
||
|
||
def write_atomic(filepath, data, binary=False): | ||
# write data to new file | ||
fd, filepath_tmp = tempfile.mkstemp(prefix=os.path.basename(filepath) + '.tmp.', dir=os.path.dirname(filepath)) | ||
|
||
if (binary): | ||
fmode = 'wb' | ||
else: | ||
fmode = 'w' | ||
|
||
with os.fdopen(fd, fmode) as f: | ||
f.write(data) | ||
f.close() | ||
|
||
try: | ||
# switch file atomically (if supported) | ||
os.rename(filepath_tmp, filepath) | ||
except OSError: | ||
# fall back to non-atomic operation | ||
try: | ||
os.unlink(filepath) | ||
except OSError: | ||
pass | ||
try: | ||
os.rename(filepath_tmp, filepath) | ||
except OSError: | ||
os.unlink(filepath_tmp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Copyright (c) 2019, Open Source Robotics Foundation, Inc. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# * Neither the name of the Willow Garage, Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import copy | ||
import os | ||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
try: | ||
FileNotFoundError | ||
except NameError: | ||
# Python 2 compatibility | ||
# https://stackoverflow.com/questions/21367320/ | ||
FileNotFoundError = IOError | ||
|
||
import rospkg | ||
|
||
from ._version import __version__ | ||
from .cache_tools import compute_filename_hash | ||
from .cache_tools import write_cache_file | ||
from .cache_tools import PICKLE_CACHE_EXT | ||
|
||
""" | ||
Rosdep needs to store data that isn't used to resolve rosdep keys, but needs to be cached during | ||
`rosdep update`. | ||
""" | ||
|
||
META_CACHE_DIR = 'meta.cache' | ||
|
||
|
||
def get_meta_cache_dir(): | ||
"""Return storage location for cached meta data.""" | ||
ros_home = rospkg.get_ros_home() | ||
return os.path.join(ros_home, 'rosdep', META_CACHE_DIR) | ||
|
||
|
||
class CacheWrapper(object): | ||
"""Make it possible to introspect cache in case some future bug needs to be worked around.""" | ||
|
||
def __init__(self, category, data): | ||
# The version of rosdep that wrote the category | ||
self.rosdep_version = __version__ | ||
# The un-hashed name of the category | ||
self.category_name = category | ||
# The stuff being stored | ||
self.data = data | ||
|
||
@property | ||
def data(self): | ||
# If cached data type is mutable, don't allow modifications to what's been loaded | ||
return copy.deepcopy(self.__data) | ||
|
||
@data.setter | ||
def data(self, value): | ||
self.__data = copy.deepcopy(value) | ||
|
||
|
||
class MetaDatabase: | ||
""" | ||
Store and retrieve metadata from rosdep cache. | ||
|
||
This data is fetched during `rosdep update`, but is not a source for resolving rosdep keys. | ||
""" | ||
|
||
def __init__(self, cache_dir=None): | ||
if cache_dir is None: | ||
cache_dir = get_meta_cache_dir() | ||
|
||
self._cache_dir = cache_dir | ||
self._loaded = {} | ||
|
||
def set(self, category, metadata): | ||
"""Add or overwrite metadata in the cache.""" | ||
wrapper = CacheWrapper(category, metadata) | ||
# print(category, metadata) | ||
write_cache_file(self._cache_dir, category, wrapper) | ||
self._loaded[category] = wrapper | ||
|
||
def get(self, category): | ||
"""Return metadata in the cache, or None if there is no cache entry.""" | ||
if category not in self._loaded: | ||
self._load_from_cache(category, self._cache_dir) | ||
|
||
if category in self._loaded: | ||
return self._loaded[category].data | ||
|
||
def _load_from_cache(self, category, cache_dir): | ||
filename = compute_filename_hash(category) + PICKLE_CACHE_EXT | ||
try: | ||
with open(os.path.join(self._cache_dir, filename), 'rb') as cache_file: | ||
self._loaded[category] = pickle.loads(cache_file.read()) | ||
except FileNotFoundError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
MetaDatabase().get('ROS_PYTHON_VERSION')
can returnNone
here, it's causing my CI builds to fail after I upgraded to rosdep 0.16.2: #720There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix proposed in #721