data_import: Support importing from Slack conversions in a directory.

Sometimes the Slack import zip file we get isn't quite the canonical
form that Slack produces -- often because the user has unzip'd it,
looked at it, and re-zip'd it, resulting in extra nested directories
and the like.

For such cases, support passing in a path to an unpacked Slack export
tree.
This commit is contained in:
Alex Vandiver 2021-05-27 18:16:04 -07:00 committed by Tim Abbott
parent 8228ea2a17
commit 94e4f33b29
2 changed files with 19 additions and 9 deletions

View File

@ -1246,7 +1246,7 @@ def fetch_team_icons(
return records
def do_convert_data(slack_zip_file: str, output_dir: str, token: str, threads: int = 6) -> None:
def do_convert_data(original_path: str, output_dir: str, token: str, threads: int = 6) -> None:
# Subdomain is set by the user while running the import command
realm_subdomain = ""
realm_id = 0
@ -1254,15 +1254,20 @@ def do_convert_data(slack_zip_file: str, output_dir: str, token: str, threads: i
check_token_access(token)
slack_data_dir = slack_zip_file.replace(".zip", "")
if not os.path.exists(slack_data_dir):
os.makedirs(slack_data_dir)
os.makedirs(output_dir, exist_ok=True)
if os.listdir(output_dir):
raise Exception("Output directory should be empty!")
subprocess.check_call(["unzip", "-q", slack_zip_file, "-d", slack_data_dir])
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)
subprocess.check_call(["unzip", "-q", original_path, "-d", 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}")
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!")
@ -1335,6 +1340,8 @@ def do_convert_data(slack_zip_file: str, output_dir: str, token: str, threads: i
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)
subprocess.check_call(["tar", "-czf", output_dir + ".tar.gz", output_dir, "-P"])

View File

@ -14,7 +14,10 @@ class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument(
"slack_data_zip", nargs="+", metavar="<Slack data zip>", help="Zipped Slack data"
"slack_data_path",
nargs="+",
metavar="<Slack data path>",
help="Zipped Slack data or directory",
)
parser.add_argument(
@ -48,7 +51,7 @@ class Command(BaseCommand):
if num_threads < 1:
raise CommandError("You must have at least one thread.")
for path in options["slack_data_zip"]:
for path in options["slack_data_path"]:
if not os.path.exists(path):
raise CommandError(f"Slack data directory not found: '{path}'")