Example #1
0
    def is_current(self):
        c = self.conn.cursor()
        try:
            c.execute("select version from version")
            version = c.fetchone()[0]
            if version < self.version:
                return False
        except (TypeError, sqlite3.OperationalError):
            # version table wasn't populated in old DB
            return False

        try:
            c.execute("select * from sources_info")
            row = c.fetchone()
            objs_mtime = convert_datetime(row[0])
            mbpi_mtime = convert_datetime(row[1])

            objs = os.path.join(EXTRA_DIR, 'networks.py')
            if ((objs_mtime != get_tz_aware_mtime(objs)) or
                (mbpi_mtime != get_tz_aware_mtime(MBPI))):
                return False
        except (TypeError, sqlite3.OperationalError):
            # it does not exist
            return False

        return True
Example #2
0
    def populate_networks(self):
        """
        Populates the networks database using default methods (currently
        a list of network objects, and from the mobile-broadband-provider-info).
        It turns off autocommit during import for speed
        """
        nick_debug("provider.py - populate_networks")

        try:
            # only will succeed on development
            networks = __import__('resources/extra/networks')
        except ImportError:
            try:
                # this fails on feisty but not on gutsy
                networks = __import__(os.path.join(EXTRA_DIR, 'networks'))
            except ImportError:
                sys.path.insert(0, EXTRA_DIR)
                import networks

        def is_valid(item):
            return not item.startswith(("__", "Base", "NetworkOperator"))

        # turn off autocommit
        self.conn.isolation_level = 'DEFERRED'

        nick_debug("provider.py - populate_networks: "
                   "conn.isolation_level: %s" % self.conn.isolation_level)

        self.populate_networks_from_objs([getattr(networks, item)()
                for item in dir(networks) if is_valid(item)])

        try:
            self.populate_networks_from_mbpi()
        except TypeError:
            pass  # MBPI v1.0 format

        # update timestamps
        objs = os.path.join(EXTRA_DIR, 'networks.py')
        args = (get_tz_aware_mtime(objs), get_tz_aware_mtime(MBPI))
        self.conn.cursor().execute(
                                "insert into sources_info values (?,?)", args)

        self.conn.commit()
        self.conn.isolation_level = None
Example #3
0
    def test_get_tz_aware_mtime(self):
        text = os.urandom(2000)
        path = 'mtime.test'
        fobj = open(path, 'w')
        fobj.write(text)
        fobj.close()

        now1 = get_tz_aware_now()
        now2 = get_tz_aware_mtime(path)

        self.assertNotEqual(now2.tzinfo, None)
        self.failIf(abs(now2 - now1).seconds > 5)

        # tidy up
        os.remove(path)