def __init__(self, mountpoint, uri=None, *args, **kw):
     fuse.Fuse.__init__(self, *args, **kw)
     self.fuse_args.mountpoint = mountpoint
     if uri is not None:
         self.server = Server(uri)
     else:
         self.server = Server()
Exemple #2
0
    def _verify_token_in_couch(self, uuid, token):
        """
        Query couchdb to decide if C{token} is valid for C{uuid}.

        @param uuid: The user uuid.
        @type uuid: str
        @param token: The token.
        @type token: str

        @raise InvalidAuthTokenError: Raised when token received from user is
                                      either missing in the tokens db or is
                                      invalid.
        """
        server = Server(url=self._app.state.couch_url)
        dbname = self.TOKENS_DB
        db = server[dbname]
        # lookup key is a hash of the token to prevent timing attacks.
        token = db.get(sha512(token).hexdigest())
        if token is None:
            raise InvalidAuthTokenError()
        # we compare uuid hashes to avoid possible timing attacks that
        # might exploit python's builtin comparison operator behaviour,
        # which fails immediatelly when non-matching bytes are found.
        couch_uuid_hash = sha512(token[self.TOKENS_USER_ID_KEY]).digest()
        req_uuid_hash = sha512(uuid).digest()
        if token[self.TOKENS_TYPE_KEY] != self.TOKENS_TYPE_DEF \
                or couch_uuid_hash != req_uuid_hash:
            raise InvalidAuthTokenError()
        return True
Exemple #3
0
    def test_view(self):
        # This test does quite a bit.  First, create 4 test records.
        # Then, create a view that will emit those records and insert that into
        # the db.  Finally, call our cushion.view object and compare results.

        self._save_some_data({'foo': 1, 'bar': 'a'})
        self._save_some_data({'foo': 2, 'bar': 'a'})
        self._save_some_data({'foo': 3, 'bar': 'b'})
        self._save_some_data({'foo': 4, 'bar': 'b'})

        fake_map = """ function (doc) { emit(doc['bar'], doc); } """

        # we're going to use python-couchdb's dynamic view loader stuff here
        from couchdb.design import ViewDefinition
        from couchdb.client import Server
        global baseurl
        cdb = Server(baseurl)
        couchdb = cdb[self.dbname]

        view_defn = ViewDefinition('test',
                                   'view',
                                   map_fun=fake_map,
                                   language='javascript')
        view_defn.sync(couchdb)

        self.cushion.view(self.dbname, 'test/view', self.stop, key='b')
        records = self.wait()

        self.assertTrue(len(records) == 2)
Exemple #4
0
 def __init__(self):
     """Initialises a new connection to couch and
     creates a new mis databse if nonexistent"""
     LOG.info('initialising database')
     database_name = CONFIG.get('couchdb', 'database')
     host = CONFIG.get('couchdb', 'hostname')
     port = CONFIG.get('couchdb', 'port')
     database_uri = 'http://' + host + ':' + port + '/'
     self.server = Server(database_uri)
     try:
         # This statement appears to do nothing, but is used to
         # make sure the database is reachable.
         self.server.version
     except AttributeError as error:
         if not test_tcp_connection(host, int(port)):
             LOG.critical("couchdb cannot be reached at " + database_uri)
             exit(1)
         else:
             LOG.error('unknown AttributeError thrown')
             raise error
     try:
         LOG.debug('opening database')
         self.database = self.server[database_name]
     except (ResourceNotFound):
         LOG.info('creating database')
         # The database didn't exist. Lets create it.
         self.database = self.server.create(database_name)
         self.create_views()
Exemple #5
0
def connector(collection):
    # Make a server connection
    svr = Server()
    if collection not in svr:
        return svr.create(collection)
    else:
        return svr[collection]
Exemple #6
0
def test(request):
    server = Server('http://43.240.96.132:5984/')
    documents = server['testdb']
    if request.method == 'GET':
        return render(request, 'test.html')
    if request.method == 'POST':
        x = {'haha': 'sdfgdfgf'}
        return HttpResponse(json.dumps(x))
Exemple #7
0
def copy_couch_database_for_test(test, db):
    port = str(test.wrapper.port)
    couch_url = 'http://localhost:' + port
    new_dbname = db._replica_uid + '_copy'
    new_db = couch.CouchDatabase.open_database(urljoin(couch_url, new_dbname),
                                               create=True,
                                               replica_uid=db._replica_uid
                                               or 'test')
    # copy all docs
    session = couch.Session()
    old_couch_db = Server(couch_url, session=session)[db._replica_uid]
    new_couch_db = Server(couch_url, session=session)[new_dbname]
    for doc_id in old_couch_db:
        doc = old_couch_db.get(doc_id)
        # bypass u1db_config document
        if doc_id == 'u1db_config':
            pass
        # copy design docs
        elif doc_id.startswith('_design'):
            del doc['_rev']
            new_couch_db.save(doc)
        # copy u1db docs
        elif 'u1db_rev' in doc:
            new_doc = {
                '_id': doc['_id'],
                'u1db_transactions': doc['u1db_transactions'],
                'u1db_rev': doc['u1db_rev']
            }
            attachments = []
            if ('u1db_conflicts' in doc):
                new_doc['u1db_conflicts'] = doc['u1db_conflicts']
                for c_rev in doc['u1db_conflicts']:
                    attachments.append('u1db_conflict_%s' % c_rev)
            new_couch_db.save(new_doc)
            # save conflict data
            attachments.append('u1db_content')
            for att_name in attachments:
                att = old_couch_db.get_attachment(doc_id, att_name)
                if (att is not None):
                    new_couch_db.put_attachment(new_doc,
                                                att,
                                                filename=att_name)
    # cleanup connections to prevent file descriptor leaking
    return new_db
Exemple #8
0
 def tearDown(self):
     # if current test is `test_close` we have to use saved objects to
     # delete the database because the close() method will have removed the
     # references needed to do it using the CouchDatabase.
     if self.id().endswith('test_couch.CouchTests.test_close(couch)'):
         session = couch.Session()
         server = Server(url=self._url, session=session)
         del (server[self._dbname])
     else:
         self.db.delete_database()
     test_backends.AllDatabaseTests.tearDown(self)
Exemple #9
0
Fichier : db.py Projet : rndD/boxus
    def __init__(self, config_path=None):
        self.server = Server()

        if config_path:
            self.config = yaml.load(open(config_path, 'r').read())

        if not all(db_name in self.server
                   for db_name in self.config['schema'].values()):
            self.setup()

        self.connect()
Exemple #10
0
def init_boards():
    """

    :rtype : object
    """
    server = Server()
    try:
        db = server.create('boards')
    except Exception:
        db = server['boards']
    return db
Exemple #11
0
def main():
    couch_uri = 'http://localhost:5984/'
    db_name = 'redditron-comments'

    server = Server(couch_uri)
    db = get_db(server, db_name)

    if (len(sys.argv) == 1):
        poll_collect(db, 4 * 60)
    else:
        collect_comments(db,
                         flatten_comments(fetch_link_comments(sys.argv[1])))
Exemple #12
0
    def __init__(self):

        from couchdb.client import Server

        couch = Server('http://localhost:5984')
        print (couch)
        try:
            self.db = couch.create('run-layer')
        except:
            self.db = couch['run-layer']

        print (self.db)
Exemple #13
0
    def GetDatabase(
        cls,
        server = GetConfig('COUCHDB_SERVER'),
        database = GetConfig('COUCHDB_DATABASE')
    ):
        if server is None:
            return None

        from couchdb.client import Server
        couchdb_server = Server(server)

        return couchdb_server[database]
Exemple #14
0
    def __init__(self, db_uri):
        local.application = self

        server = Server(db_uri)
        try:
            db = server.create("urls")
        except Exception:
            db = server["urls"]
        self.dispatch = SharedDataMiddleware(self.dispatch,
                                             {"/static": STATIC_PATH})

        URL.db = db
Exemple #15
0
    def __init__(self, db_uri):
        local.application = self

        server = Server(db_uri)
        try:
            db = server.create('urls')
        except:
            db = server['urls']
        self.dispatch = SharedDataMiddleware(self.dispatch, {
            '/static':    STATIC_PATH
        })

        URL.db = db
Exemple #16
0
    def __init__(self, host, db_name, id_generator):
        self.server = Server(host)
        self.db_name = db_name

        try:
            self.create_db_if_not_exist()
        except socket.error as e:
            raise CouchDBBackendConnectionError(
                "Unable to connect to the CouchDB server: %s - %s" % (host, e))

        self._db = self.server[self.db_name]
        self.sync_views()
        self._generate_id = id_generator
Exemple #17
0
 def db(self):
     if hasattr(request, "_couchdb_db"):
         db = request._couchdb_db
     else:
         dburi = config.get("couchdb.dburi")
         server = Server(dburi)
         username = config.get("couchdb.username")
         password = config.get("couchdb.password")
         if username:
             server.resource.http.add_credentials(username, password)
         db = server[config.get("couchdb.database")]
         request._couchdb_db = db
     return db
Exemple #18
0
def main():
    couch_uri = 'http://localhost:5984/'
    db_name = 'redditron-comments'

    server = Server(couch_uri)
    db = server[db_name]

    c = build_chain(db)
    outputs = []
    for i in range(0, 100):
        outputs.append(c.generate_text())

    print "\n---\n".join(outputs)
Exemple #19
0
    def __init__(self):
        try:
            self.__server = Server()
        except:
            print 'can not connect to Couchdb:%s' % (settings.c['db_url'])

        self.__db = {}
        self.__db_name = settings.c['db_name']
        DEBUG.p(self.__db_name.items())
        for (k, v) in self.__db_name.items():
            try:
                self.__db[v] = self.__server.create(v)
            except:
                self.__db[v] = self.__server[v]
Exemple #20
0
def couch_server(url):
    """
    Provide a connection to a couch server and cleanup after use.

    For database creation and deletion we use an ephemeral connection to the
    couch server. That connection has to be properly closed, so we provide it
    as a context manager.

    :param url: The URL of the Couch server.
    :type url: str
    """
    session = Session(timeout=COUCH_TIMEOUT)
    server = Server(url=url, full_commit=False, session=session)
    yield server
Exemple #21
0
 def open_database(cls, url, create):
     """Open a U1DB database using CouchDB as backend."""
     # get database from url
     m = re.match('(^https?://[^/]+)/(.+)$', url)
     if not m:
         raise InvalidURLError
     url = m.group(1)
     dbname = m.group(2)
     server = Server(url=url)
     try:
         server[dbname]
     except ResourceNotFound:
         if not create:
             raise DatabaseDoesNotExist()
     return cls(url, dbname)
Exemple #22
0
 def __init__(self,
              url,
              database,
              replica_uid=None,
              full_commit=True,
              session=None):
     """Create a new Couch data container."""
     self._url = url
     self._full_commit = full_commit
     self._session = session
     self._server = Server(url=self._url,
                           full_commit=self._full_commit,
                           session=self._session)
     self._dbname = database
     # this will ensure that transaction and sync logs exist and are
     # up-to-date.
     try:
         self._database = self._server[database]
     except ResourceNotFound:
         self._server.create(database)
         self._database = self._server[database]
     super(CouchDatabase, self).__init__(replica_uid=replica_uid,
                                         document_factory=LeapDocument)
Exemple #23
0
    def __init__(self, basedir, url) : 
        self.basedir = basedir 
        self.language = "javascript" 

        # parse out the design document id, etc
        regex = re.compile( r'(.*)/([^\s/]*)/(_design/[^\s/]*)/?$' )
        match = regex.match( url )
        if match is None:
            _usage("no match on url of design document")

        (self.couchurl, self.dbname, self.designdoc) = [ match.group(i) for i in [1,2,3] ]

        self.server = Server(self.couchurl)
        if self.dbname not in self.server:
            _usage( "dbname %s doesnt exist in database %s" % (self.dbname, self.couchurl) ) 
    
        self.db = self.server[self.dbname]

        self.designDocFound = False 
        if self.designdoc in self.db:
            self.ddoc = self.db[ self.designdoc ] 
            self.designDocFound = True
        else :
            self.ddoc = { "language" : "javascript" } 
Exemple #24
0
    def _verify_token_in_couch(self, uuid, token):
        """
        Query couchdb to decide if C{token} is valid for C{uuid}.

        @param uuid: The user uuid.
        @type uuid: str
        @param token: The token.
        @type token: str

        @raise InvalidAuthTokenError: Raised when token received from user is
                                      either missing in the tokens db or is
                                      invalid.
        """
        server = Server(url=self._app.state.couch_url)
        # the tokens db rotates every 30 days, and the current db name is
        # "tokens_NNN", where NNN is the number of seconds since epoch divided
        # by the rotate period in seconds. When rotating, old and new tokens
        # db coexist during a certain window of time and valid tokens are
        # replicated from the old db to the new one. See:
        # https://leap.se/code/issues/6785
        dbname = self.TOKENS_DB_PREFIX + \
            str(int(time.time() / self.TOKENS_DB_EXPIRE))
        db = server[dbname]
        # lookup key is a hash of the token to prevent timing attacks.
        token = db.get(sha512(token).hexdigest())
        if token is None:
            raise InvalidAuthTokenError()
        # we compare uuid hashes to avoid possible timing attacks that
        # might exploit python's builtin comparison operator behaviour,
        # which fails immediatelly when non-matching bytes are found.
        couch_uuid_hash = sha512(token[self.TOKENS_USER_ID_KEY]).digest()
        req_uuid_hash = sha512(uuid).digest()
        if token[self.TOKENS_TYPE_KEY] != self.TOKENS_TYPE_DEF \
                or couch_uuid_hash != req_uuid_hash:
            raise InvalidAuthTokenError()
        return True
Exemple #25
0
def fire_em_all(skip, limit):
    collection = 'pantip'
    svr = Server()
    if collection not in svr:
        src = svr.create(collection)
    else:
        src = svr[collection]

    # Iterate through the collection and fire
    n = 0
    n_processed = 0
    for _id in src:
        n += 1
        rec = src.get(_id)
        if n < skip: continue

        if n_processed > limit:
            print(colored('Out of ammo!', 'red'))
            return

        # Fire a single request
        print(colored('Firing #{0}'.format(_id)))
        fire_request(rec)
        n_processed += 1
Exemple #26
0
 def __init__(self):
     server = Server(config.get('couchdb', 'host'))
     self.conn = server[config.get('couchdb', 'db')]
Exemple #27
0
 def clear(self):
     s = Server()
     del s['python-tests']
     db = s.create('python-tests')
Exemple #28
0
 def __init__(self, app):
     manager = CouchDBManager()
     # ...add document types and view definitions...
     manager.setup(app)
     server = Server()
     self.db = server['active_server_cp']
def _get_server(url):
    resource = Resource(url.geturl(),
                        Session(retry_delays=[1, 2, 4, 8], timeout=10))
    return Server(url=resource)
Exemple #30
0
    def __init__(self, server_url, shares_db, config_db):
        self.server = Server(server_url)
        self.shares_db = self.server[shares_db]
        self.config_db = self.server[config_db]

        self.worker_dict = {}