Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
export: Only print .s with percent_callback to console.
Tim Abbott6130a61be0b7
diff --git a/zerver/lib/export.py b/zerver/lib/export.pyindex 8c0aa671b04..81bf2c20300 100644--- a/zerver/lib/export.py+++ b/zerver/lib/export.py@@ -1757,6 +1757,7 @@ def export_realm_wrapper(realm: Realm, output_dir: str,threads: int, upload: bool,public_only: bool,delete_after_upload: bool,+ percent_callback: Optional[Callable[[Any], None]]=None,consent_message_id: Optional[int]=None) -> Optional[str]:tarball_path = do_export_realm(realm=realm, output_dir=output_dir,threads=threads, public_only=public_only,@@ -1771,7 +1772,8 @@ def export_realm_wrapper(realm: Realm, output_dir: str,# without additional configuration. We'll likely want to change# that in the future.print("Uploading export tarball...")- public_url = zerver.lib.upload.upload_backend.upload_export_tarball(realm, tarball_path)+ public_url = zerver.lib.upload.upload_backend.upload_export_tarball(+ realm, tarball_path, percent_callback=percent_callback)print()print(f"Uploaded to {public_url}")diff --git a/zerver/lib/upload.py b/zerver/lib/upload.pyindex 8a0052c3d56..7dbd550f5ae 100644--- a/zerver/lib/upload.py+++ b/zerver/lib/upload.py@@ -6,12 +6,11 @@ import osimport randomimport reimport shutil-import sysimport unicodedataimport urllibfrom datetime import timedeltafrom mimetypes import guess_extension, guess_type-from typing import Any, Optional, Tuple+from typing import Any, Callable, Optional, Tupleimport boto3import botocore@@ -253,7 +252,8 @@ class ZulipUploadBackend:def get_emoji_url(self, emoji_file_name: str, realm_id: int) -> str:raise NotImplementedError()- def upload_export_tarball(self, realm: Realm, tarball_path: str) -> str:+ def upload_export_tarball(self, realm: Realm, tarball_path: str,+ percent_callback: Optional[Callable[[Any], None]]=None) -> str:raise NotImplementedError()def delete_export_tarball(self, path_id: str) -> Optional[str]:@@ -588,11 +588,8 @@ class S3UploadBackend(ZulipUploadBackend):emoji_file_name=emoji_file_name)return f"{self.avatar_bucket_url}/{emoji_path}"- def upload_export_tarball(self, realm: Optional[Realm], tarball_path: str) -> str:- def percent_callback(bytes_transferred: Any) -> None:- sys.stdout.write('.')- sys.stdout.flush()-+ def upload_export_tarball(self, realm: Optional[Realm], tarball_path: str,+ percent_callback: Optional[Callable[[Any], None]]=None) -> str:# We use the avatar bucket, because it's world-readable.key = self.avatar_bucket.Object(os.path.join("exports", generate_random_token(32),os.path.basename(tarball_path)))@@ -817,7 +814,8 @@ class LocalUploadBackend(ZulipUploadBackend):"/user_avatars",RealmEmoji.PATH_ID_TEMPLATE.format(realm_id=realm_id, emoji_file_name=emoji_file_name))- def upload_export_tarball(self, realm: Realm, tarball_path: str) -> str:+ def upload_export_tarball(self, realm: Realm, tarball_path: str,+ percent_callback: Optional[Callable[[Any], None]]=None) -> str:path = os.path.join('exports',str(realm.id),@@ -902,8 +900,10 @@ def upload_message_image_from_request(request: HttpRequest, user_file: File,return upload_message_file(uploaded_file_name, uploaded_file_size,content_type, user_file.read(), user_profile)-def upload_export_tarball(realm: Realm, tarball_path: str) -> str:- return upload_backend.upload_export_tarball(realm, tarball_path)+def upload_export_tarball(realm: Realm, tarball_path: str,+ percent_callback: Optional[Callable[[Any], None]]=None) -> str:+ return upload_backend.upload_export_tarball(realm, tarball_path,+ percent_callback=percent_callback)def delete_export_tarball(path_id: str) -> Optional[str]:return upload_backend.delete_export_tarball(path_id)diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.pyindex 57ded815aba..6a1a0305aba 100644--- a/zerver/management/commands/export.py+++ b/zerver/management/commands/export.py@@ -1,4 +1,5 @@import os+import sysimport tempfilefrom argparse import ArgumentParserfrom typing import Any@@ -168,9 +169,14 @@ class Command(ZulipBaseCommand):print(f"\033[94mNumber of users that reacted outbox:\033[0m {len(reactions)}\n")+ def percent_callback(bytes_transferred: Any) -> None:+ sys.stdout.write('.')+ sys.stdout.flush()+# Allows us to trigger exports separately from command line argument parsingexport_realm_wrapper(realm=realm, output_dir=output_dir,threads=num_threads, upload=options['upload'],public_only=public_only,delete_after_upload=options["delete_after_upload"],+ percent_callback=percent_callback,consent_message_id=consent_message_id)diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 6a57eeba66d..2e532d839db 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -1883,7 +1883,16 @@ class S3Test(ZulipTestCase):with open(tarball_path, 'w') as f:f.write('dummy')- uri = upload_export_tarball(user_profile.realm, tarball_path)+ total_bytes_transferred = 0++ def percent_callback(bytes_transferred: int) -> None:+ nonlocal total_bytes_transferred+ total_bytes_transferred += bytes_transferred++ uri = upload_export_tarball(user_profile.realm, tarball_path,+ percent_callback=percent_callback)+ # Verify the percent_callback API works+ self.assertEqual(total_bytes_transferred, 5)result = re.search(re.compile(r"([0-9a-fA-F]{32})"), uri)if result is not None: