2016-04-05 00:27:37 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any
|
2016-04-05 00:27:37 +02:00
|
|
|
|
|
|
|
from zerver.lib.export import do_export_user
|
2017-07-08 19:46:49 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-08 19:46:49 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2016-04-05 00:27:37 +02:00
|
|
|
help = """Exports message data from a Zulip user
|
|
|
|
|
2016-08-12 23:35:56 +02:00
|
|
|
This command exports the message history for a single Zulip user.
|
|
|
|
|
|
|
|
Note that this only exports the user's message history and
|
|
|
|
realm-public metadata needed to understand it; it does nothing
|
|
|
|
with (for example) any bots owned by the user."""
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("email", metavar="<email>", help="email of user to export")
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--output", dest="output_dir", help="Directory to write exported data to."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-07-08 19:46:49 +02:00
|
|
|
self.add_realm_args(parser)
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-07-08 19:46:49 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
user_profile = self.get_user(options["email"], realm)
|
2016-04-05 00:27:37 +02:00
|
|
|
|
|
|
|
output_dir = options["output_dir"]
|
|
|
|
if output_dir is None:
|
2019-01-15 03:02:06 +01:00
|
|
|
output_dir = tempfile.mkdtemp(prefix="zulip-export-")
|
2016-04-05 00:27:37 +02:00
|
|
|
if os.path.exists(output_dir):
|
|
|
|
shutil.rmtree(output_dir)
|
|
|
|
os.makedirs(output_dir)
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Exporting user {user_profile.delivery_email}")
|
2016-04-05 00:27:37 +02:00
|
|
|
do_export_user(user_profile, output_dir)
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Finished exporting to {output_dir}; tarring")
|
2021-02-12 08:20:45 +01:00
|
|
|
tarball_path = output_dir.rstrip("/") + ".tar.gz"
|
2016-04-05 00:27:37 +02:00
|
|
|
subprocess.check_call(["tar", "--strip-components=1", "-czf", tarball_path, output_dir])
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Tarball written to {tarball_path}")
|