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

Fixed inconsistent hash_overwrite after copy_append and join_right #97

Merged
merged 2 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
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
39 changes: 26 additions & 13 deletions sisyphus/job_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def __init__(self, path, creator=None, cached=False, hash_overwrite=None, tags=N

self._available = available

@property
def hash_overwrite(self):
return self._hash_overwrite

@hash_overwrite.setter
def hash_overwrite(self, value):
if value is not None:
assert_msg = "sis_hash for path must be str or tuple of length 2"
if isinstance(value, tuple):
assert len(value) == 2, assert_msg
else:
assert isinstance(value, str), assert_msg
value = (None, value)
self._hash_overwrite = value

def keep_value(self, value):
if self.creator:
self.creator.keep_value(value)
Expand Down Expand Up @@ -107,17 +122,9 @@ def _sis_hash(self):
creator = self.creator
path = self.path
else:
overwrite = self.hash_overwrite
assert_msg = "sis_hash for path must be str or tuple of length 2"
if isinstance(overwrite, tuple):
assert len(overwrite) == 2, assert_msg
creator, path = overwrite
else:
assert isinstance(overwrite, str), assert_msg
creator = None
path = overwrite
creator, path = self.hash_overwrite
if hasattr(creator, '_sis_id'):
creator = os.path.join(creator._sis_id(), gs.JOB_OUTPUT)
creator = f"{creator._sis_id()}/{gs.JOB_OUTPUT}"
return b'(Path, ' + tools.sis_hash_helper((creator, path)) + b')'

@finished_results_cache.caching(get_key=lambda self, debug_info=None: ('available', self.rel_path()))
Expand Down Expand Up @@ -170,7 +177,7 @@ def get_path(self) -> str:
if os.path.isabs(path):
return path
else:
return os.path.join(gs.BASE_DIR, path)
return f"{gs.BASE_DIR}/{path}"

def get_cached_path(self) -> str:
if Path.cacheing_enabled and self.cached:
Expand Down Expand Up @@ -264,13 +271,19 @@ def copy(self):
def copy_append(self, suffix):
""" Returns a copy of this path with the given suffix appended to it """
new = self.copy()
if self.hash_overwrite:
c, o = self.hash_overwrite
new.hash_overwrite = (c, o + suffix)
new.path += suffix
return new

def join_right(self, other):
""" Joins local path with given string using os.path.join """
""" Joins local path with given string using '/' """
new = self.copy()
new.path = os.path.join(new.path, other)
if self.hash_overwrite:
c, o = self.hash_overwrite
new.hash_overwrite = (c, f"{o}/{other}")
new.path = f"{new.path}/{other}"
Comment on lines 282 to +286
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join_right could also simply be implemented as:

return self.copy_append("/" + other)

return new

def size(self):
Expand Down
23 changes: 23 additions & 0 deletions tests/job_path_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ def test_overwrite_hash(self):
self.assertEqual(path._sis_hash(),
b"(Path, (tuple, (str, 'foo'), (str, 'bar')))")

def test_hash_overwrite_modify(self):
path = Path('out', hash_overwrite='foo')
path_join = path.join_right('bar')
path_append = path.copy_append('bar')
self.assertEqual(path._sis_hash(),
b"(Path, (tuple, (NoneType), (str, 'foo')))")
self.assertEqual(path_join._sis_hash(),
b"(Path, (tuple, (NoneType), (str, 'foo/bar')))")
self.assertEqual(path_append._sis_hash(),
b"(Path, (tuple, (NoneType), (str, 'foobar')))")

mjob = MockJob('test/me.1234')
path = Path('lm.gz', mjob, hash_overwrite=('foo', 'bar'))
path_join = path.join_right('baz')
path_append = path.copy_append('baz')

self.assertEqual(path._sis_hash(),
b"(Path, (tuple, (str, 'foo'), (str, 'bar')))")
self.assertEqual(path_join._sis_hash(),
b"(Path, (tuple, (str, 'foo'), (str, 'bar/baz')))")
self.assertEqual(path_append._sis_hash(),
b"(Path, (tuple, (str, 'foo'), (str, 'barbaz')))")

def test_pickle(self):
def pickle_and_check(path):
with tk.mktemp() as pickle_path:
Expand Down