export: Use tar -C to switch directories.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-12-16 19:11:46 -08:00 committed by Tim Abbott
parent 6855df0abb
commit 27977eddeb
2 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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}")