[util] Use TemporaryDirectory rather than mkdtemp in vendor.py
This has the same behaviour as the explicit try/finally, but is
probably simpler to understand (especially in the second case where
there is a lot of code between the try and the finally).
Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
diff --git a/util/vendor.py b/util/vendor.py
index 962a589..a555c2a 100755
--- a/util/vendor.py
+++ b/util/vendor.py
@@ -297,8 +297,7 @@
def _export_patches(patchrepo_clone_url, target_patch_dir, upstream_rev,
patched_rev):
- clone_dir = Path(tempfile.mkdtemp())
- try:
+ with tempfile.TemporaryDirectory() as clone_dir:
clone_git_repo(patchrepo_clone_url, clone_dir, patched_rev)
rev_range = 'origin/' + upstream_rev + '..' + 'origin/' + patched_rev
cmd = ['git', 'format-patch', '-o', str(target_patch_dir), rev_range]
@@ -306,9 +305,6 @@
cmd += ['-q']
subprocess.run(cmd, cwd=str(clone_dir), check=True)
- finally:
- shutil.rmtree(str(clone_dir), ignore_errors=True)
-
def import_from_upstream(upstream_path, target_path, exclude_files=[]):
log.info('Copying upstream sources to %s', target_path)
@@ -482,8 +478,7 @@
if args.refresh_patches:
refresh_patches(desc)
- clone_dir = Path(tempfile.mkdtemp())
- try:
+ with tempfile.TemporaryDirectory() as clone_dir:
# clone upstream repository
upstream_new_rev = clone_git_repo(desc.upstream.url, clone_dir, rev=desc.upstream.rev)
@@ -582,9 +577,6 @@
git_add_commit(commit_paths, commit_msg)
- finally:
- shutil.rmtree(str(clone_dir), ignore_errors=True)
-
log.info('Import finished')