Skip to content

Commit

Permalink
Revert #12559 to make it compatible with python2 (#12647)
Browse files Browse the repository at this point in the history
* Revert "Use context manager when creating temporary directory in datadog-checks-downloader (#12559)"

This reverts commit 675fe0d.

* Make sure the tempdir is removed in case an exception is raised
  • Loading branch information
FlorentClarret authored Aug 1, 2022
1 parent a04b30b commit 5673b5d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions datadog_checks_downloader/datadog_checks/downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ def __handle_in_toto_verification_exception(self, target_relpath, e):

def __in_toto_verify(self, inspection_packet, target_relpath):
# Make a temporary directory in a parent directory we control.
with tempfile.TemporaryDirectory(dir=REPOSITORIES_DIR) as tempdir:
tempdir = tempfile.mkdtemp(dir=REPOSITORIES_DIR)

try:
# Copy files over into temp dir.
for abs_path in inspection_packet:
shutil.copy(abs_path, tempdir)
Expand All @@ -226,16 +228,17 @@ def __in_toto_verify(self, inspection_packet, target_relpath):
# Load the root layout and public keys in this temp dir.
root_layout, root_layout_pubkeys, root_layout_params = self.__load_root_layout(target_relpath)

try:
verifylib.in_toto_verify(root_layout, root_layout_pubkeys, substitution_parameters=root_layout_params)
except Exception as e:
self.__handle_in_toto_verification_exception(target_relpath, e)
else:
logger.info('in-toto verified %s', target_relpath)
finally:
# Switch back to a parent directory we control, so that we can
# safely delete temp dir.
os.chdir(REPOSITORIES_DIR)
verifylib.in_toto_verify(root_layout, root_layout_pubkeys, substitution_parameters=root_layout_params)
except Exception as e:
self.__handle_in_toto_verification_exception(target_relpath, e)
else:
logger.info('in-toto verified %s', target_relpath)
finally:
# Switch back to a parent directory we control, so that we can
# safely delete temp dir.
os.chdir(REPOSITORIES_DIR)
# Delete temp dir.
shutil.rmtree(tempdir)

def __download_and_verify_in_toto_metadata(self, target_relpath, target_abspath, target):
# First, get our in-toto root layout.
Expand Down

0 comments on commit 5673b5d

Please sign in to comment.