From bafcf3c664aaaa320ba296b71e2258f76e8eca2e Mon Sep 17 00:00:00 2001 From: Wyatt Hoodes Date: Mon, 25 Mar 2019 13:36:37 -1000 Subject: [PATCH] export.py: Add 'delete after upload' option for removing tarball. This allows removal of the local tarball upon a succesful s3 upload. A part of the public-only-realm-export webapp feature. --- zerver/lib/export.py | 10 ++++++++-- zerver/management/commands/export.py | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 2adc524d2d..c2f6d2c294 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -1597,8 +1597,10 @@ def get_analytics_config() -> Config: return analytics_config -def export_realm_wrapper(realm: Realm, output_dir: str, threads: int, - upload_to_s3: bool, public_only: bool) -> None: + +def export_realm_wrapper(realm: Realm, output_dir: str, + threads: int, upload_to_s3: bool, + public_only: bool, delete_after_upload: bool) -> None: do_export_realm(realm=realm, output_dir=output_dir, threads=threads, public_only=public_only) print("Finished exporting to %s; tarring" % (output_dir,)) @@ -1634,3 +1636,7 @@ def export_realm_wrapper(realm: Realm, output_dir: str, threads: int, bucket=bucket.name, key=key.key) print("Uploaded to %s" % (public_url,)) + + if delete_after_upload: + os.remove(tarball_path) + print("Successfully deleted the tarball at %s" % (tarball_path,)) diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index cbc5455629..100c148807 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -95,6 +95,9 @@ class Command(ZulipBaseCommand): parser.add_argument('--upload-to-s3', action="store_true", help="Whether to upload resulting tarball to s3") + parser.add_argument('--delete-after-upload', + action="store_true", + help='Automatically delete the local tarball after a successful export') self.add_realm_args(parser, True) def handle(self, *args: Any, **options: Any) -> None: @@ -117,4 +120,5 @@ class Command(ZulipBaseCommand): # Allows us to trigger exports separately from command line argument parsing export_realm_wrapper(realm=realm, output_dir=output_dir, threads=num_threads, upload_to_s3=options['upload_to_s3'], - public_only=options["public_only"]) + public_only=options["public_only"], + delete_after_upload=options["delete_after_upload"])