export.py: Have do_export_realm handle the export tarball.

This change is preliminary refactoring in order to improve the test
mocking strategy related to `test_realm_export.py`.

What this allows is the ability to simply mock a return value from
`do_export_realm`.  We can then use that value as a dummy url to
ensure a file has been served and can be retrieved.
This commit is contained in:
Wyatt Hoodes
2019-06-21 08:30:00 -10:00
committed by Tim Abbott
parent 45f87ff44b
commit 8efb7b903b

View File

@@ -1396,7 +1396,7 @@ def do_write_stats_file_for_realm_export(output_dir: Path) -> None:
def do_export_realm(realm: Realm, output_dir: Path, threads: int,
exportable_user_ids: Optional[Set[int]]=None,
public_only: bool=False,
consent_message_id: Optional[int]=None) -> None:
consent_message_id: Optional[int]=None) -> str:
response = {} # type: TableData
# We need at least one thread running to export
@@ -1457,6 +1457,18 @@ def do_export_realm(realm: Realm, output_dir: Path, threads: int,
logging.info("Finished exporting %s" % (realm.string_id,))
create_soft_link(source=output_dir, in_progress=False)
do_write_stats_file_for_realm_export(output_dir)
# We need to change back to the current working directory after writing
# the tarball to the output directory, otherwise the state is compromised
# for our unit tests.
reset_dir = os.getcwd()
tarball_path = output_dir.rstrip('/') + '.tar.gz'
os.chdir(os.path.dirname(output_dir))
subprocess.check_call(["tar", "-czf", tarball_path, os.path.basename(output_dir)])
os.chdir(reset_dir)
return tarball_path
def export_attachment_table(realm: Realm, output_dir: Path, message_ids: Set[int]) -> None:
response = {} # type: TableData
fetch_attachment_data(response=response, realm_id=realm.id, message_ids=message_ids)
@@ -1668,15 +1680,10 @@ def export_realm_wrapper(realm: Realm, output_dir: str,
public_only: bool,
delete_after_upload: bool,
consent_message_id: Optional[int]=None) -> Optional[str]:
do_export_realm(realm=realm, output_dir=output_dir, threads=threads,
public_only=public_only, consent_message_id=consent_message_id)
print("Finished exporting to %s; tarring" % (output_dir,))
do_write_stats_file_for_realm_export(output_dir)
tarball_path = output_dir.rstrip('/') + '.tar.gz'
os.chdir(os.path.dirname(output_dir))
subprocess.check_call(["tar", "-czf", tarball_path, os.path.basename(output_dir)])
tarball_path = do_export_realm(realm=realm, output_dir=output_dir,
threads=threads, public_only=public_only,
consent_message_id=consent_message_id)
print("Finished exporting to %s" % (output_dir,))
print("Tarball written to %s" % (tarball_path,))
if not upload: