From 5cadc6365a39f24013a892075b9f98b2ba130d72 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Sat, 6 Jan 2018 18:42:18 +0000 Subject: [PATCH] slack importer: Extract data into directory of zip file. Currently the zip file is extracted to the root of zulip directory no matter where the the zip file. The extracted data is not useful after running the command which pollutes the zulip directory. It make more sense to extract it to the same directory of zip file especially when the zip file gets downloaded to /temp like in the tests. --- zerver/lib/slack_data_to_zulip_data.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/zerver/lib/slack_data_to_zulip_data.py b/zerver/lib/slack_data_to_zulip_data.py index 013d8d191d..b372280c6d 100755 --- a/zerver/lib/slack_data_to_zulip_data.py +++ b/zerver/lib/slack_data_to_zulip_data.py @@ -26,7 +26,7 @@ def get_model_id(model: Any) -> int: else: return 1 -def users_to_zerver_userprofile(slack_dir: str, realm_id: int, timestamp: Any, +def users_to_zerver_userprofile(slack_data_dir: str, realm_id: int, timestamp: Any, domain_name: str) -> Tuple[List[ZerverFieldsT], AddedUsersT]: """ Returns: @@ -35,7 +35,7 @@ def users_to_zerver_userprofile(slack_dir: str, realm_id: int, timestamp: Any, user id """ print('######### IMPORTING USERS STARTED #########\n') - users = json.load(open(slack_dir + '/users.json')) + users = json.load(open(slack_data_dir + '/users.json')) zerver_userprofile = [] added_users = {} user_id_count = get_model_id(UserProfile) @@ -138,7 +138,7 @@ def users_to_zerver_userprofile(slack_dir: str, realm_id: int, timestamp: Any, print('######### IMPORTING USERS FINISHED #########\n') return zerver_userprofile, added_users -def channels_to_zerver_stream(slack_dir: str, realm_id: int, added_users: AddedUsersT, +def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: AddedUsersT, zerver_userprofile: List[ZerverFieldsT]) -> Tuple[List[ZerverFieldsT], List[ZerverFieldsT], AddedChannelsT, @@ -153,7 +153,7 @@ def channels_to_zerver_stream(slack_dir: str, realm_id: int, added_users: AddedU 5. zerver_recipient, which is a list of the recipients """ print('######### IMPORTING CHANNELS STARTED #########\n') - channels = json.load(open(slack_dir + '/channels.json')) + channels = json.load(open(slack_data_dir + '/channels.json')) added_channels = {} zerver_stream = [] @@ -277,10 +277,11 @@ def channels_to_zerver_stream(slack_dir: str, realm_id: int, added_users: AddedU return zerver_defaultstream, zerver_stream, added_channels, zerver_subscription, zerver_recipient def do_convert_data(slack_zip_file: str, realm_name: str, output_dir: str) -> None: - slack_dir = slack_zip_file.replace('.zip', '') - subprocess.check_call(['unzip', slack_zip_file]) + slack_data_dir = slack_zip_file.replace('.zip', '') + zip_file_dir = os.path.dirname(slack_data_dir) + subprocess.check_call(['unzip', slack_zip_file, '-d', zip_file_dir]) # with zipfile.ZipFile(slack_zip_file, 'r') as zip_ref: - # zip_ref.extractall(slack_dir) + # zip_ref.extractall(slack_data_dir) # TODO fetch realm config from zulip config DOMAIN_NAME = "zulipchat.com" @@ -319,13 +320,13 @@ def do_convert_data(slack_zip_file: str, realm_name: str, output_dir: str) -> No zerver_realmfilter=[], zerver_realmemoji=[]) - zerver_userprofile, added_users = users_to_zerver_userprofile(slack_dir, + zerver_userprofile, added_users = users_to_zerver_userprofile(slack_data_dir, REALM_ID, int(NOW), DOMAIN_NAME) realm['zerver_userprofile'] = zerver_userprofile - channels_to_zerver_stream_fields = channels_to_zerver_stream(slack_dir, + channels_to_zerver_stream_fields = channels_to_zerver_stream(slack_data_dir, REALM_ID, added_users, zerver_userprofile) @@ -367,7 +368,7 @@ def do_convert_data(slack_zip_file: str, realm_name: str, output_dir: str) -> No json.dump(attachment, open(attachment_file, 'w')) # remove slack dir - rm_tree(slack_dir) + rm_tree(slack_data_dir) subprocess.check_call(["tar", "-czf", output_dir + '.tar.gz', output_dir, '-P']) print('######### DATA CONVERSION FINISHED #########\n')