示例#1
0
    def connection(self):
        if not self.dbc:
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path, logging.error)
            logging.debug("* Database: %s", self.path)
            self.dbc = sqlite3.connect(self.path)

            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row

            #
            # Migrate MUST be before table creation.  This
            # is safe because table creation always uses
            # the IF NOT EXISTS clause.  And this is needed
            # because otherwise we cannot migrate archived
            # databases (whose version number is old).
            # The exception is the config table which must
            # be present because migrate() looks at it.
            #
            table_config.create(self.dbc)

            migrate.migrate(self.dbc)
            migrate2.migrate(self.dbc)

            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)

        return self.dbc
示例#2
0
def __migrate(connection):

    '''
     This function migrates the database at @connection to the
     current database format.  To do that, this function uses
     directly the Neubot routines written for this purpose.
    '''

    migrate.migrate(connection)
示例#3
0
    def connection(self):
        ''' Return connection to database '''
        if not self.dbc:
            database_xxx.linux_fixup_databasedir()
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path)

            logging.debug("* Database: %s", self.path)
            self.dbc = sqlite3.connect(self.path)

            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row

            #
            # On POSIX systems, neubot (initially) runs as root, to ensure that
            # database location, ownership and permissions are OK (as well as
            # to bind privileged ports).  But neubot can also be started by
            # normal users.  In this case, mark the database as readonly since
            # write operation are going to raise exceptions.
            #
            if not system.has_enough_privs():
                logging.warning('database: opening database in readonly mode')
                self.readonly = True
                return self.dbc

            #
            # Migrate MUST be before table creation.  This
            # is safe because table creation always uses
            # the IF NOT EXISTS clause.  And this is needed
            # because otherwise we cannot migrate archived
            # databases (whose version number is old).
            # The exception is the config table which must
            # be present because migrate() looks at it.
            #
            table_config.create(self.dbc)

            migrate.migrate(self.dbc)
            migrate2.migrate(self.dbc)

            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)
            table_raw.create(self.dbc)

        return self.dbc
示例#4
0
    def connection(self):
        if not self.dbc:
            if self.path != ":memory:":
                self.path = system.check_database_path(self.path, logging.error)
            logging.debug("* Database: %s" % self.path)
            self.dbc = sqlite3.connect(self.path)
            #
            # To avoid the need to map at hand columns in
            # a row with the sql schema, which is as error
            # prone as driving drunk.
            #
            self.dbc.row_factory = sqlite3.Row
            table_config.create(self.dbc)
            table_speedtest.create(self.dbc)
            table_geoloc.create(self.dbc)
            table_bittorrent.create(self.dbc)
            table_log.create(self.dbc)
            migrate.migrate(self.dbc)

        return self.dbc
示例#5
0
def __sqlite3_connect(path):

    '''
     Return a connection to the database at @path.  This function
     takes care of the cases when the database does not exist or
     it is compressed and/or needs to be migrated.
    '''

    # Create new database if nonexistent
    if not os.path.exists(path):
        syslog.syslog(syslog.LOG_INFO, 'Create new: %s' % path)
        manager = DatabaseManager()
        manager.set_path(path)
        connection = manager.connection()
        connection.commit()
        return connection

    # Decompress the database if needed
    if path.endswith('.bz2'):
        inputfp = bz2.BZ2File(path)
        outputfp, npath = tempfile.mkstemp(suffix='.sqlite3', dir='.')
        syslog.syslog(syslog.LOG_INFO, 'Bunzip2: %s -> %s' % (path, npath))
        outputfp = os.fdopen(outputfp, 'w')
        atexit.register(__sqlite3_cleanup, npath)
        chunk = inputfp.read(262144)
        while chunk:
            outputfp.write(chunk)
            chunk = inputfp.read(262144)
        outputfp.close()
        inputfp.close()
    else:
        npath = path

    # Migrate to the latest version
    syslog.syslog(syslog.LOG_INFO, 'Open existing: %s' % npath)
    connection = sqlite3.connect(npath)
    connection.row_factory = sqlite3.Row
    migrate.migrate(connection)
    migrate2.migrate(connection)
    return connection