Skip to content

Instantly share code, notes, and snippets.

@chaoflow
Created November 6, 2017 21:34
Show Gist options
  • Save chaoflow/02a1be706cf4948a9f4d7f1fd66d6c73 to your computer and use it in GitHub Desktop.
Save chaoflow/02a1be706cf4948a9f4d7f1fd66d6c73 to your computer and use it in GitHub Desktop.
Dump database of MARV 16.10 in order to restore in 17.11
#!/usr/bin/env python2
from __future__ import print_function
from datetime import datetime
import json
import os
import sys
try:
root = sys.argv[1]
except IndexError:
root = '.'
basedir = os.path.join(root, 'aggroot')
if not os.access(basedir, os.X_OK):
print('Usage: {} <sitedir>'.format(sys.argv[0]))
sys.exit(1)
def export(basedir, uuid):
setdir = os.path.join(basedir, uuid)
# files
fileset = json.load(open(os.path.join(setdir, 'fileset.json')))
assert fileset['type'] == 'marv_robotics.bagset'
files = [f['path'] for f in fileset['files']]
meta = json.load(open(os.path.join(setdir, 'meta.json')))
time_added = int((datetime.strptime(meta['fileset']['time_started']['$datetime'], '%Y-%m-%dT%H:%M:%S.%f') -
datetime.utcfromtimestamp(0)).total_seconds() * 1000)
# tags
try:
tags = json.load(open(os.path.join(setdir, 'tags.json')))
except IOError:
tags = []
# comments
try:
comments = [{
'text': comment['comment'],
'time_added': int((datetime.strptime(comment['timestamp']['$datetime'], '%Y-%m-%dT%H:%M:%S.%f') -
datetime.utcfromtimestamp(0)).total_seconds() * 1000),
'author': comment['username'],
} for comment in json.load(open(os.path.join(setdir, 'comments.json')))]
except IOError:
comments = []
return {'files': files, 'tags': tags, 'comments': comments, "time_added": time_added}
users = json.load(open('users'))
result = {
'datasets': {'DEFAULT_COLLECTION': [export(basedir, uuid) for uuid in os.listdir(basedir)]},
'users': [{'name': x, 'password': ''} for x in users.keys()],
}
print(json.dumps(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment