export: Write stats.txt for ./manage.py export <realm>.

This commit is contained in:
Steve Howell
2016-08-11 17:38:19 -07:00
committed by Tim Abbott
parent 7910a6e134
commit b0e6d20321
2 changed files with 31 additions and 1 deletions

View File

@@ -582,6 +582,31 @@ def export_avatars_from_local(realm, output_dir, local_dir):
with open(os.path.join(output_dir, "records.json"), "w") as records_file:
ujson.dump(records, records_file, indent=4)
def do_write_stats_file_for_realm_export(output_dir):
stats_file = os.path.join(output_dir, 'stats.txt')
realm_file = os.path.join(output_dir, 'realm.json')
message_files = glob.glob(os.path.join(output_dir, 'messages-*.json'))
fns = sorted(message_files + [realm_file])
logging.info('Writing stats file: %s\n' % (stats_file,))
with open(stats_file, 'w') as f:
for fn in fns:
f.write(os.path.basename(fn) +'\n')
payload = open(fn).read()
data = ujson.loads(payload)
for k in sorted(data):
f.write('%5d %s\n' % (len(data[k]), k))
f.write('\n')
avatar_file = os.path.join(output_dir, 'avatars/records.json')
uploads_file = os.path.join(output_dir, 'uploads/records.json')
for fn in [avatar_file, uploads_file]:
f.write(fn+'\n')
payload = open(fn).read()
data = ujson.loads(payload)
f.write('%5d records\n' % len(data))
f.write('\n')
def do_export_realm(realm, output_dir, threads):
# type: (Realm, Path, int) -> None

View File

@@ -11,7 +11,9 @@ import subprocess
import tempfile
import ujson
from zerver.lib.export import do_export_realm
from zerver.lib.export import (
do_export_realm, do_write_stats_file_for_realm_export
)
from zerver.models import get_realm
class Command(BaseCommand):
@@ -122,6 +124,9 @@ class Command(BaseCommand):
do_export_realm(realm, output_dir, threads=num_threads)
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)])