Skip to content

Commit 78c1569

Browse files
authored
Replace cache checksumdir pypi pkg with internal _dirhash() (#61)
* rm checksumdir from requirements * add _dirhash() * pep8 changes
1 parent 57513b5 commit 78c1569

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

DEV-REQUIREMENTS.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
autopep8
2-
pytest>=5.2.2
3-
checksumdir
2+
pytest>=5.2.2

tftest.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from hashlib import sha1
4343
from pathlib import Path
4444
from typing import List
45-
from checksumdir import dirhash
4645

4746
__version__ = '1.7.4'
4847

@@ -382,6 +381,21 @@ def _abspath(self, path):
382381
"""Make relative path absolute from base dir."""
383382
return path if os.path.isabs(path) else os.path.join(self._basedir, path)
384383

384+
def _dirhash(self, directory, hash, ignore_hidden=False, excluded_extensions=[]):
385+
assert Path(directory).is_dir()
386+
for path in sorted(Path(directory).iterdir(), key=lambda p: str(p).lower()):
387+
if path.is_file():
388+
if not ignore_hidden and path.basename().startswith("."):
389+
continue
390+
if path.suffix in excluded_extensions:
391+
continue
392+
with open(path, "rb") as f:
393+
for chunk in iter(lambda: f.read(4096), b""):
394+
hash.update(chunk)
395+
elif path.is_dir():
396+
hash = self._dirhash(path, hash)
397+
return hash
398+
385399
def _cache(func):
386400

387401
def cache(self, **kwargs):
@@ -428,13 +442,13 @@ def cache(self, **kwargs):
428442
# creates hash of all file content within tfdir
429443
# excludes hidden files from being used within hash (ignores .terraform/ or .terragrunt-cache/)
430444
# and excludes any local tfstate files
431-
params["tfdir"] = dirhash(
432-
self.tfdir, 'sha1', ignore_hidden=True, excluded_extensions=['backup', 'tfstate'])
445+
446+
params["tfdir"] = self._dirhash(
447+
self.tfdir, sha1(), ignore_hidden=True, excluded_extensions=['.backup', '.tfstate']).hexdigest()
433448

434449
hash_filename = sha1(
435450
json.dumps(params, sort_keys=True,
436451
default=str).encode("cp037")).hexdigest() + ".pickle"
437-
438452
cache_key = cache_dir / hash_filename
439453
_LOGGER.debug("Cache key: %s", cache_key)
440454

0 commit comments

Comments
 (0)