Files
zulip/zerver/management/commands/convert_hipchat_data.py
Vishnu Ks 123bcea518 management: Don't use sys.exit(1).
Using sys.exit in a management command makes it impossible
to unit test the code in question.  The correct approach to do the same
thing in Django management commands is to raise CommandError.

Followup of b570c0dafa
2019-05-03 14:20:39 -07:00

83 lines
2.9 KiB
Python

import argparse
import os
from typing import Any
'''
Example usage for testing purposes:
Move the data:
rm -Rf ~/hipchat-data
mkdir ~/hipchat-data
./manage.py convert_hipchat_data ~/hipchat-31028-2018-08-08_23-23-22.tar --output ~/hipchat-data
./manage.py import --destroy-rebuild-database hipchat ~/hipchat-data
Test out the realm:
./tools/run-dev.py
go to browser and use your dev url
spec:
https://confluence.atlassian.com/hipchatkb/
exporting-from-hipchat-server-or-data-center-for-data-portability-950821555.html
'''
from django.core.management.base import BaseCommand, CommandParser, CommandError
from zerver.data_import.hipchat import do_convert_data
class Command(BaseCommand):
help = """Convert the Hipchat data into Zulip data format."""
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument('hipchat_tar', nargs='+',
metavar='<hipchat data tarfile>',
help="tar of Hipchat data")
parser.add_argument('--output', dest='output_dir',
action="store",
help='Directory to write exported data to.')
parser.add_argument('--mask', dest='masking_content',
action="store_true",
help='Mask the content for privacy during QA.')
parser.add_argument('--slim-mode', dest='slim_mode',
action="store_true",
help="Default to no public stream subscriptions if no token is available." +
" See import docs for details.")
parser.add_argument('--token', dest='api_token',
action="store",
help='API token for the HipChat API for fetching subscribers.')
parser.formatter_class = argparse.RawTextHelpFormatter
def handle(self, *args: Any, **options: Any) -> None:
output_dir = options["output_dir"]
if output_dir is None:
raise CommandError("You need to specify --output <output directory>")
if os.path.exists(output_dir) and not os.path.isdir(output_dir):
raise CommandError(output_dir + " is not a directory")
os.makedirs(output_dir, exist_ok=True)
if os.listdir(output_dir):
raise CommandError('Output directory should be empty!')
output_dir = os.path.realpath(output_dir)
for path in options['hipchat_tar']:
if not os.path.exists(path):
raise CommandError("Tar file not found: '%s'" % (path,))
print("Converting Data ...")
do_convert_data(
input_tar_file=path,
output_dir=output_dir,
masking_content=options.get('masking_content', False),
slim_mode=options['slim_mode'],
api_token=options.get("api_token"),
)