예제 #1
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
예제 #2
0
def open_dbm(path):
    dbm = DatabaseManager()
    dbm.set_path(path)
    dbm.connect()
    dbm.dbc.row_factory = sqlite3.Row   #XXX
    return dbm