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

Integrate python version into the hash key #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 17 additions & 9 deletions memestra/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def check_deprecated(data):


class CacheKeyFactoryBase(object):

py_version = ".".join(map(str, sys.version_info)).encode()

class CacheKeyBase(object):
def __init__(self):
self._hasher = hashlib.sha256()
self._hasher.update(CacheKeyFactoryBase.py_version)

def __init__(self, keycls):
self.keycls = keycls
self.created = dict()
Expand All @@ -154,14 +162,15 @@ class CacheKeyFactory(CacheKeyFactoryBase):
Only the content of the module is taken into account
'''

class CacheKey(object):
class CacheKey(CacheKeyFactoryBase.CacheKeyBase):

def __init__(self, module_path, _):
super(CacheKeyFactory.CacheKey, self).__init__()

with open(module_path, 'rb') as fd:
module_content = fd.read()
module_hash = hashlib.sha256(module_content).hexdigest()
self.module_hash = module_hash
self._hasher.update(module_content)
self.module_hash = self._hasher.hexdigest()

@property
def path(self):
Expand All @@ -179,9 +188,10 @@ class RecursiveCacheKeyFactory(CacheKeyFactoryBase):
key.
'''

class CacheKey(object):
class CacheKey(CacheKeyFactoryBase.CacheKeyBase):

def __init__(self, module_path, factory):
super(RecursiveCacheKeyFactory.CacheKey, self).__init__()
assert module_path not in factory.created or factory.created[module_path] is None

with open(module_path, 'rb') as fd:
Expand All @@ -196,9 +206,7 @@ def __init__(self, module_path, factory):
if factory.get(dep, 1) is not None:
new_deps.append(dep)

module_hash = hashlib.sha256(module_content).hexdigest()

hashes = [module_hash]
self._hasher.update(module_content)

for new_dep in sorted(new_deps):
try:
Expand All @@ -207,9 +215,9 @@ def __init__(self, module_path, factory):
# better?
except UnicodeDecodeError:
continue
hashes.append(new_dep_key.module_hash)
self._hasher.update(new_dep_key.module_hash.encode())

self.module_hash = hashlib.sha256("".join(hashes).encode("ascii")).hexdigest()
self.module_hash = self._hasher.hexdigest()

@property
def path(self):
Expand Down