Example #1
0
    def _open_db(self):
        if self._debug.should('dataio'):
            self._debug.write("Opening data file {!r}".format(self._filename))
        self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
        with self._dbs[get_thread_id()] as db:
            try:
                schema_version, = db.execute("select version from coverage_schema").fetchone()
            except Exception as exc:
                raise CoverageException(
                    "Data file {!r} doesn't seem to be a coverage data file: {}".format(
                        self._filename, exc
                    )
                )
            else:
                if schema_version != SCHEMA_VERSION:
                    raise CoverageException(
                        "Couldn't use data file {!r}: wrong schema: {} instead of {}".format(
                            self._filename, schema_version, SCHEMA_VERSION
                        )
                    )

            for row in db.execute("select has_lines, has_arcs from meta"):
                self._has_lines, self._has_arcs = row

            for path, id in db.execute("select path, id from file"):
                self._file_map[path] = id
Example #2
0
 def _connect(self):
     if get_thread_id() not in self._dbs:
         if os.path.exists(self._filename):
             self._open_db()
         else:
             self._create_db()
     return self._dbs[get_thread_id()]
Example #3
0
 def _connect(self):
     if get_thread_id() not in self._dbs:
         if os.path.exists(self._filename):
             self._open_db()
         else:
             self._create_db()
     return self._dbs[get_thread_id()]
Example #4
0
    def _open_db(self):
        if self._debug.should('dataio'):
            self._debug.write("Opening data file {!r}".format(self._filename))
        self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
        with self._dbs[get_thread_id()] as db:
            try:
                schema_version, = db.execute("select version from coverage_schema").fetchone()
            except Exception as exc:
                raise CoverageException(
                    "Data file {!r} doesn't seem to be a coverage data file: {}".format(
                        self._filename, exc
                    )
                )
            else:
                if schema_version != SCHEMA_VERSION:
                    raise CoverageException(
                        "Couldn't use data file {!r}: wrong schema: {} instead of {}".format(
                            self._filename, schema_version, SCHEMA_VERSION
                        )
                    )

            for row in db.execute("select has_lines, has_arcs from meta"):
                self._has_lines, self._has_arcs = row

            for path, id in db.execute("select path, id from file"):
                self._file_map[path] = id
Example #5
0
 def _connect(self):
     """Get the SqliteDb object to use."""
     if get_thread_id() not in self._dbs:
         if os.path.exists(self._filename):
             self._open_db()
         else:
             self._create_db()
     return self._dbs[get_thread_id()]
Example #6
0
 def _create_db(self):
     if self._debug.should('dataio'):
         self._debug.write("Creating data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
     with self._dbs[get_thread_id()] as db:
         for stmt in SCHEMA.split(';'):
             stmt = stmt.strip()
             if stmt:
                 db.execute(stmt)
         db.execute("insert into coverage_schema (version) values (?)", (SCHEMA_VERSION,))
         db.execute(
             "insert into meta (has_lines, has_arcs, sys_argv) values (?, ?, ?)",
             (self._has_lines, self._has_arcs, str(getattr(sys, 'argv', None)))
         )
Example #7
0
 def _create_db(self):
     if self._debug.should('dataio'):
         self._debug.write("Creating data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = Sqlite(self._filename, self._debug)
     with self._dbs[get_thread_id()] as db:
         for stmt in SCHEMA.split(';'):
             stmt = " ".join(stmt.strip().split())
             if stmt:
                 db.execute(stmt)
         db.execute("insert into coverage_schema (version) values (?)", (SCHEMA_VERSION,))
         db.execute(
             "insert into meta (has_lines, has_arcs, sys_argv) values (?, ?, ?)",
             (self._has_lines, self._has_arcs, str(getattr(sys, 'argv', None)))
         )
Example #8
0
    def loads(self, data):
        """Deserialize data from :meth:`dumps`

        Use with a newly-created empty :class:`CoverageData` object.  It's
        undefined what happens if the object already has data in it.

        Arguments:
            data: A byte string of serialized data produced by :meth:`dumps`.

        .. versionadded:: 5.0

        """
        if self._debug.should('dataio'):
            self._debug.write("Loading data into data file {!r}".format(
                self._filename))
        if data[:1] != b'z':
            raise CoverageException(
                "Unrecognized serialization: {!r} (head of {} bytes)".format(
                    data[:40], len(data)))
        script = to_string(zlib.decompress(data[1:]))
        self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug)
        with db:
            db.executescript(script)
        self._read_db()
        self._have_used = True
Example #9
0
    def _read_db(self):
        """Read the metadata from a database so that we are ready to use it."""
        with self._dbs[get_thread_id()] as db:
            try:
                schema_version, = db.execute("select version from coverage_schema").fetchone()
            except Exception as exc:
                raise CoverageException(
                    "Data file {!r} doesn't seem to be a coverage data file: {}".format(
                        self._filename, exc
                    )
                )
            else:
                if schema_version != SCHEMA_VERSION:
                    raise CoverageException(
                        "Couldn't use data file {!r}: wrong schema: {} instead of {}".format(
                            self._filename, schema_version, SCHEMA_VERSION
                        )
                    )

            for row in db.execute("select value from meta where key = 'has_arcs'"):
                self._has_arcs = bool(int(row[0]))
                self._has_lines = not self._has_arcs

            for path, file_id in db.execute("select path, id from file"):
                self._file_map[path] = file_id
Example #10
0
 def __nonzero__(self):
     if (get_thread_id() not in self._dbs and not os.path.exists(self._filename)):
         return False
     try:
         with self._connect() as con:
             rows = con.execute("select * from file limit 1")
             return bool(list(rows))
     except CoverageException:
         return False
Example #11
0
 def __nonzero__(self):
     if (get_thread_id() not in self._dbs and not os.path.exists(self._filename)):
         return False
     try:
         with self._connect() as con:
             rows = con.execute("select * from file limit 1")
             return bool(list(rows))
     except CoverageException:
         return False
Example #12
0
 def _create_db(self):
     if self._debug.should('dataio'):
         self._debug.write("Creating data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug)
     with db:
         db.executescript(SCHEMA)
         db.execute("insert into coverage_schema (version) values (?)", (SCHEMA_VERSION,))
         db.execute(
             "insert into meta (has_lines, has_arcs, sys_argv) values (?, ?, ?)",
             (self._has_lines, self._has_arcs, str(getattr(sys, 'argv', None)))
         )
Example #13
0
 def loads(self, data):
     if self._debug.should('dataio'):
         self._debug.write("Loading data into data file {!r}".format(self._filename))
     if data[:1] != b'z':
         raise CoverageException("Unrecognized serialization: {!r} (head of {} bytes)".format(data[:40], len(data)))
     script = to_string(zlib.decompress(data[1:]))
     self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug)
     with db:
         db.executescript(script)
     self._read_db()
     self._have_used = True
Example #14
0
 def _create_db(self):
     if self._debug.should('dataio'):
         self._debug.write("Creating data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug)
     with db:
         db.executescript(SCHEMA)
         db.execute("insert into coverage_schema (version) values (?)",
                    (SCHEMA_VERSION, ))
         db.executemany("insert into meta (key, value) values (?, ?)", [
             ('sys_argv', str(getattr(sys, 'argv', None))),
             ('version', __version__),
             ('when',
              datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
         ])
Example #15
0
 def _open_db(self):
     """Open an existing db file, and read its metadata."""
     if self._debug.should('dataio'):
         self._debug.write("Opening data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = SqliteDb(self._filename, self._debug)
     self._read_db()
Example #16
0
 def _open_db(self):
     if self._debug.should('dataio'):
         self._debug.write("Opening data file {!r}".format(self._filename))
     self._dbs[get_thread_id()] = SqliteDb(self._filename, self._debug)
     self._read_db()