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

Use context manager when creating temporary directory #12559

Merged
merged 1 commit into from
Aug 1, 2022
Merged
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: 18 additions & 21 deletions datadog_checks_downloader/datadog_checks/downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,30 +206,27 @@ 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.
tempdir = tempfile.mkdtemp(dir=REPOSITORIES_DIR)
with tempfile.TemporaryDirectory(dir=REPOSITORIES_DIR) as tempdir:
# Copy files over into temp dir.
for abs_path in inspection_packet:
shutil.copy(abs_path, tempdir)

# Copy files over into temp dir.
for abs_path in inspection_packet:
shutil.copy(abs_path, tempdir)
# Switch to the temp dir.
os.chdir(tempdir)

# Switch to the temp dir.
os.chdir(tempdir)
# 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)

# 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)
# Delete temp dir.
shutil.rmtree(tempdir)
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)

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