Ejemplo n.º 1
0
 def parseResult(self, result):
     """
     Parse JSON result from the DB.
     """
     return json.loads(result)
Ejemplo n.º 2
0
    def doLater(self, args):

        c = self.getRootCommand()

        try:
            url = args[0]
        except IndexError:
            self.stdout.write('Please give a database to replicate with.\n')
            return

        # urlparse really needs a scheme there
        if not url.startswith('http'):
            url = 'http://' + url

        server = c.getNewServer()
        # FIXME: don't poke privately
        client = server._couch

        # if a username was given, but no password, ask for it
        parsed = urlparse.urlparse(url)
        password = None
        if parsed.username and not parsed.password:
            password = c.getPassword(
                prompt='\nPassword for target database %s: ' % url)

        jane = urlrewrite.rewrite(url, hostname=HOST, port=PORT,
            password=password, path='/' + DB)

        dbs = [
          c.dbName,
          jane,
        ]


        for source, target in [(dbs[0], dbs[1]), (dbs[1], dbs[0])]:
            s = json.dumps({
              "source": source,
              "target": target,
              "continuous": True})
            self.info('replicating from %s to %s',
                urlrewrite.rewrite_safe(source),
                urlrewrite.rewrite_safe(target))

            try:
                d = client.post('/_replicate', s)
            except Exception, e:
                self.stdout.write('Exception %r\n', e)
                self.stdout.write(
                    'FAILED: local server failed for source %s\n' %
                        source.encode('utf-8'))
                self.stdout.write('Is the server running ?\n')
                defer.returnValue(e)
                return

            error = None # set with a non-newline string in case of error

            try:
                result = yield d
            except twerror.Error, e:
                error = 'CouchDB returned error response %r' % e.status
                try:
                    self.debug('CouchDB message: %r', e.message)
                    r = json.loads(e.message)
                    error = 'CouchDB returned error reason: %s' % r['reason']
                except:
                    pass
Ejemplo n.º 3
0
 def parseResult(self, result):
     """
     Parse JSON result from the DB.
     """
     return json.loads(result)
Ejemplo n.º 4
0
            try:
                result = yield d
            except twerror.Error, e:
                error = 'CouchDB returned error response %r' % e.status
                try:
                    self.debug('CouchDB message: %r', e.message)
                    r = json.loads(e.message)
                    error = 'CouchDB returned error reason: %s' % r['reason']
                except:
                    pass
            except Exception, e:
                error = log.getExceptionMessage(e)

            if not error:
                r = json.loads(result)

                try:
                    if r['ok']:
                        self.stdout.write('+ Replicating %s to %s\n' % (
                            urlrewrite.rewrite_safe(source.encode('utf-8')),
                            urlrewrite.rewrite_safe(target.encode('utf-8'))))
                    else:
                        error = r
                except Exception, e:
                    error = 'Exception: %r\n' % e

            if error:
                self.stdout.write('- Failed to replicate %s to %s:\n' % (
                    urlrewrite.rewrite_safe(source.encode('utf-8')),
                    urlrewrite.rewrite_safe(target.encode('utf-8'))))
Ejemplo n.º 5
0
 def testUnicodeToUnicode(self):
     u = json.loads(u'"str"')
     self.assertEquals(u, u'str')
     self.assertEquals(type(u), unicode)
Ejemplo n.º 6
0
    def doLater(self, args):
        root = self.getRootCommand()
        sourceUri = '/' + root.getDatabase()
        client = self.getRootCommand().getClient()

        # figure out target
        try:
            url = args[0]
        except IndexError:
            self.stdout.write('Please give a database to replicate to.\n')
            return

        # urlparse really needs a scheme there
        if not url.startswith('http'):
            url = 'http://' + url

        # if a username was given, but no password, ask for it
        parsed = urlparse.urlparse(url)
        self.log('url %s parsed to %r', url, parsed)

        # if password specified in URL, use it
        password = parsed.password

        # if password file given, use that
        if self.options.password_file:
            try:
                with open(self.options.password_file, "r") as handle:
                    password = handle.read().strip()
            except:
                self.stderr.write(
                    "ERROR: Could not read password from file %s\n" % (
                        self.options.password_file, ))

        # if still no password, prompt for it if we have a username too
        if parsed.username and not password:
            password = root.getPassword(
                prompt='\nPassword for target database %s: ' % url)

        # default to same db name on different host
        jane = urlrewrite.rewrite(url, hostname=HOST, port=PORT,
            password=password, path=sourceUri)

        self.log('remote url rewritten to %s', jane)

        # figure out source
        # sourceUri = '/' + root.getDatabase()
        # tarzan = client.url_template % sourceUri
        tarzan = root.getDatabase()

        self.log('local url rewritten to %s', tarzan)

        dbs = []
        if self.options.direction in ['forward', 'both']:
            dbs.append((tarzan, jane))
        if self.options.direction in ['backward', 'both']:
            dbs.append((jane, tarzan))


        for source, target in dbs:
            s = json.dumps({
              "source": source,
              "target": target,
              "continuous": self.options.type == 'continuous'})
            self.info('replicating from %s to %s',
                urlrewrite.rewrite_safe(source),
                urlrewrite.rewrite_safe(target))

            try:
                d = client.post('/_replicate', s)
            except Exception, e:
                self.stdout.write('Exception %r\n' % e)
                self.stdout.write(
                    'FAILED: local server failed for source %s\n' %
                        source.encode('utf-8'))
                self.stdout.write('Is the server running ?\n')
                defer.returnValue(e)
                return

            error = None # set with a non-newline string in case of error

            try:
                result = yield d
            except twerror.Error, e:
                error = 'CouchDB returned error response %r' % e.status
                try:
                    self.debug('CouchDB message: %r', e.message)
                    r = json.loads(e.message)
                    error = 'CouchDB returned error reason: %s' % r['reason']
                except:
                    pass
Ejemplo n.º 7
0
 def parseException(self, line):
     obj = pjson.loads(line)
     if isinstance(obj, list):
         if obj[0] == 'log':
             message = obj[1]
             self.parseMessage(message)
Ejemplo n.º 8
0
 def testUnicodeToUnicode(self):
     u = json.loads(u'"str"')
     self.assertEquals(u, u'str')
     self.assertEquals(type(u), unicode)