From 27977eddeb9b43ee418c4ccbf3e98c8bd4ab67ba Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 16 Dec 2021 19:11:46 -0800 Subject: [PATCH] export: Use tar -C to switch directories. Signed-off-by: Anders Kaseorg --- zerver/lib/export.py | 15 ++++++++------- zerver/management/commands/export_single_user.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 2449fb2782..aaa8e9af4c 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -1874,14 +1874,15 @@ def do_export_realm( do_write_stats_file_for_realm_export(output_dir) - # We need to change back to the current working directory after writing - # the tarball to the output directory, otherwise the state is compromised - # for our unit tests. - reset_dir = os.getcwd() tarball_path = output_dir.rstrip("/") + ".tar.gz" - os.chdir(os.path.dirname(output_dir)) - subprocess.check_call(["tar", "-czf", tarball_path, os.path.basename(output_dir)]) - os.chdir(reset_dir) + subprocess.check_call( + [ + "tar", + f"-czf{tarball_path}", + f"-C{os.path.dirname(output_dir)}", + os.path.basename(output_dir), + ] + ) return tarball_path diff --git a/zerver/management/commands/export_single_user.py b/zerver/management/commands/export_single_user.py index 25fca88e3b..61b5203d40 100644 --- a/zerver/management/commands/export_single_user.py +++ b/zerver/management/commands/export_single_user.py @@ -46,7 +46,12 @@ class Command(ZulipBaseCommand): do_export_user(user_profile, output_dir) print(f"Finished exporting to {output_dir}; tarring") tarball_path = output_dir.rstrip("/") + ".tar.gz" - - os.chdir(os.path.dirname(output_dir)) - subprocess.check_call(["tar", "-czf", tarball_path, os.path.basename(output_dir)]) + subprocess.check_call( + [ + "tar", + f"-czf{tarball_path}", + f"-C{os.path.dirname(output_dir)}", + os.path.basename(output_dir), + ] + ) print(f"Tarball written to {tarball_path}")