Example #1
0
def generate_schemata(remote_url, username, password, latest=True, download=False, to="schemata.tar", debug=False):
    """ Generate sync data from remote server
    
    remote_url: url of remote server (ip:port)
    username, password: credentials for logging in
    """
    status = rest_util.login(remote_url, username, password)
    if not status:
        print "Sorry. Your credentials were not accepted."
        sys.exit()    
    url = 'http://%s/api/xforms/?format=sync' % remote_url
    if latest:
        xmlns_buffer = rest_util.get_field_as_bz2(FormDefModel, 'target_namespace', debug)
        print "Generating latest remote schemata..."
    else:
        xmlns_buffer = ''
        print "Generating all remote submissions archive..."
    response = rest_util.request(url, username, password, xmlns_buffer)
    print "Generated"

    if download:
        fout = open(to, 'w+b')
        fout.write(response.read())
        fout.close()
        print "Schemata downloaded to %s" % to
    return response 
def generate_submissions(remote_url, username, password, latest=True, debug=False, download=False, to='submissions.tar'):
    """ Generate sync data from remote server
    
    remote_url: url of remote server (ip:port)
    username, password: credentials for logging in
    """
    status = rest_util.login(remote_url, username, password)
    if not status:
        print "Sorry. Your credentials were not accepted."
        sys.exit()
    
    url = 'http://%s/api/submissions/' % remote_url
    if latest:
        MD5_buffer = rest_util.get_field_as_bz2(Submission, 'checksum', debug)
        response = rest_util.request(url, username, password, MD5_buffer)
        print "Generated latest remote submissions"
    else:
        response = urllib2.urlopen(url)
        print "Generated all remote submissions archive"
    if download:
        fout = open(to, 'w+b')
        fout.write(response.read())
        fout.close()
        print "Submissions downloaded to %s" % to
    else:
        # Check for status messages
        # (i think tar payloads always begin 'BZ'...)
        response = response.read(255)
        if response[:2] != "BZ":
            print response
    return response