Switch to unified view

a/scripts/backup_project.py b/scripts/backup_project.py
...
...
17
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
17
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
18
18
19
def main():
19
def main():
20
    if len(sys.argv) not in (2,3):
20
    if len(sys.argv) not in (2,3):
21
        log.error('Usage: %s <shortname> [<backup_dir>]', sys.argv[0])
21
        log.error('Usage: %s <shortname> [<backup_dir>]', sys.argv[0])
22
        return
22
        return 1
23
    pname = sys.argv[1]
23
    pname = sys.argv[1]
24
    project = M.Project.query.get(shortname=pname)
24
    project = M.Project.query.get(shortname=pname)
25
    if project is None:
25
    if project is None:
26
        log.fatal('Project %s not found', pname)
26
        log.fatal('Project %s not found', pname)
27
        print 'Project %s not found' % pname
27
        print 'Project %s not found' % pname
28
        return
28
        return 2
29
    if len(sys.argv) == 3:
29
    if len(sys.argv) == 3:
30
        backup_dir = sys.argv[2]
30
        backup_dir = sys.argv[2]
31
    else:
31
    else:
32
        pname = project.shortname
32
        pname = project.shortname
33
        gid = project.tool_data.get('sfx', {}).get('group_id', project._id)
33
        gid = project.tool_data.get('sfx', {}).get('group_id', project._id)
34
        dirname = '%s-%s.purge' % (pname, gid)
34
        dirname = '%s-%s.purge' % (pname, gid)
35
        backup_dir = os.path.join(
35
        backup_dir = os.path.join(
36
            os.getcwd(), dirname)
36
            os.getcwd(), dirname)
37
    log.info('Backing up %s to %s', pname, backup_dir)
37
    log.info('Backing up %s to %s', pname, backup_dir)
38
    dump_project(project, backup_dir)
38
    dump_project(project, backup_dir)
39
    return 0
39
40
40
def dump_project(project, dirname):
41
def dump_project(project, dirname):
41
    os.system('%s --db %s -o %s' % (
42
    os.system('%s --db %s -o %s' % (
42
            MONGO_DUMP, project.database, dirname))
43
            MONGO_DUMP, project.database, dirname))
43
    with open(os.path.join(dirname, 'project.json'), 'w') as fp:
44
    with open(os.path.join(dirname, 'project.json'), 'w') as fp:
44
        json.dump(state(project).document, fp, default=default)
45
        json.dump(state(project).document, fp, default=default)
45
46
46
if __name__ == '__main__':
47
if __name__ == '__main__':
47
    main()
48
    sys.exit(main())