Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
slack: Clean up expanded zipfiles more consistently.
Alex Vandiverd9f868a163f1
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex 68b46752700..5fc0a7b7379 100644--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -1364,28 +1364,17 @@ def fetch_team_icons(return records-def do_convert_data(+def do_convert_zipfile(original_path: str,output_dir: str,token: str,threads: int = 6,convert_slack_threads: bool = False,) -> None:- # Subdomain is set by the user while running the import command- realm_subdomain = ""- realm_id = 0- domain_name = settings.EXTERNAL_HOST-- check_token_access(token)-- os.makedirs(output_dir, exist_ok=True)- if os.listdir(output_dir):- raise Exception("Output directory should be empty!")-- if os.path.isfile(original_path) and original_path.endswith(".zip"):- slack_data_dir = original_path.replace(".zip", "")- if not os.path.exists(slack_data_dir):- os.makedirs(slack_data_dir)+ assert original_path.endswith(".zip")+ slack_data_dir = original_path.removesuffix(".zip")+ try:+ os.makedirs(slack_data_dir, exist_ok=True)with zipfile.ZipFile(original_path) as zipObj:# Slack's export doesn't set the UTF-8 flag on each@@ -1397,13 +1386,28 @@ def do_convert_data(fileinfo.filename = fileinfo.filename.encode("cp437").decode("utf-8")zipObj.NameToInfo[fileinfo.filename] = fileinfozipObj.extractall(slack_data_dir)- elif os.path.isdir(original_path):- slack_data_dir = original_path- else:- raise ValueError(f"Don't know how to import Slack data from {original_path}")++ do_convert_directory(slack_data_dir, output_dir, token, threads, convert_slack_threads)+ finally:+ # Always clean up the uncompressed directory+ rm_tree(slack_data_dir)+++def do_convert_directory(+ slack_data_dir: str,+ output_dir: str,+ token: str,+ threads: int = 6,+ convert_slack_threads: bool = False,+) -> None:+ check_token_access(token)++ os.makedirs(output_dir, exist_ok=True)+ if os.listdir(output_dir):+ raise Exception("Output directory should be empty!")if not os.path.isfile(os.path.join(slack_data_dir, "channels.json")):- raise ValueError(f"{original_path} does not have the layout we expect from a Slack export!")+ raise ValueError("Import does not have the layout we expect from a Slack export!")# We get the user data from the legacy token method of Slack API, which is depreciated# but we use it as the user email data is provided only in this method@@ -1412,6 +1416,11 @@ def do_convert_data(custom_emoji_list = get_slack_api_data("https://slack.com/api/emoji.list", "emoji", token=token)+ # Subdomain is set by the user while running the import command+ realm_subdomain = ""+ realm_id = 0+ domain_name = settings.EXTERNAL_HOST+(realm,slack_user_id_to_zulip_user_id,@@ -1474,10 +1483,6 @@ def do_convert_data(create_converted_data_files(attachment, output_dir, "/attachment.json")create_converted_data_files(realm_icon_records, output_dir, "/realm_icons/records.json")- # Clean up the directory if we unpacked it ourselves.- if original_path != slack_data_dir:- rm_tree(slack_data_dir)-logging.info("######### DATA CONVERSION FINISHED #########\n")logging.info("Zulip data dump created at %s", output_dir)diff --git a/zerver/management/commands/convert_slack_data.py b/zerver/management/commands/convert_slack_data.pyindex a1117b05a56..ef9c7f7566b 100644--- a/zerver/management/commands/convert_slack_data.py+++ b/zerver/management/commands/convert_slack_data.py@@ -7,7 +7,7 @@ from django.conf import settingsfrom django.core.management.base import CommandError, CommandParserfrom typing_extensions import override-from zerver.data_import.slack import do_convert_data+from zerver.data_import.slack import do_convert_directory, do_convert_zipfilefrom zerver.lib.management import ZulipBaseCommand@@ -67,10 +67,21 @@ class Command(ZulipBaseCommand):print("Converting data ...")convert_slack_threads = not options["no_convert_slack_threads"]- do_convert_data(- path,- output_dir,- token,- threads=num_threads,- convert_slack_threads=convert_slack_threads,- )+ if os.path.isdir(path):+ do_convert_directory(+ path,+ output_dir,+ token,+ threads=num_threads,+ convert_slack_threads=convert_slack_threads,+ )+ elif os.path.isfile(path) and path.endswith(".zip"):+ do_convert_zipfile(+ path,+ output_dir,+ token,+ threads=num_threads,+ convert_slack_threads=convert_slack_threads,+ )+ else:+ raise ValueError(f"Don't know how to import Slack data from {path}")diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.pyindex ae1467faba7..a89d23d9523 100644--- a/zerver/tests/test_slack_importer.py+++ b/zerver/tests/test_slack_importer.py@@ -31,7 +31,7 @@ from zerver.data_import.slack import (channels_to_zerver_stream,check_token_access,convert_slack_workspace_messages,- do_convert_data,+ do_convert_zipfile,fetch_shared_channel_users,get_admin,get_guest,@@ -1324,7 +1324,7 @@ class SlackImporter(ZulipTestCase):with self.assertLogs(level="INFO"), self.settings(EXTERNAL_HOST="zulip.example.com"):# We need to mock EXTERNAL_HOST to be a valid domain because Slack's importer# uses it to generate email addresses for users without an email specified.- do_convert_data(test_slack_zip_file, output_dir, token)+ do_convert_zipfile(test_slack_zip_file, output_dir, token)self.assertTrue(os.path.exists(output_dir))self.assertTrue(os.path.exists(output_dir + "/realm.json"))@@ -1532,4 +1532,4 @@ class SlackImporter(ZulipTestCase):with self.assertLogs(level="INFO"), self.settings(EXTERNAL_HOST="zulip.example.com"):# We need to mock EXTERNAL_HOST to be a valid domain because Slack's importer# uses it to generate email addresses for users without an email specified.- do_convert_data(test_slack_zip_file, output_dir, token)+ do_convert_zipfile(test_slack_zip_file, output_dir, token)