Exemple #1
0
    def fill_database(self):
        logging.basicConfig(level=logging.INFO)
        database = MirrorDatabase()
        helper = Helpers()
        database.create_database()
        downloader = MirrorDownloader()
        if not (database.conn.number_of_rows('mirrors')):
            mirrors = downloader.download_mirrors()
            ip, location = helper.get_location()
            for distro in mirrors:
                database.conn.executemany('INSERT INTO mirrors (Distro, Mirror)  VALUES (?, ?)', [(distro['name'], m) for m in distro['mirrors']])
                database.conn.commit()
        else:
            database.log.warn('No mirrors were added')

        hosts = database.resolve_mirrors()
        for mirror_id, addresses in hosts:
                for address in addresses:
                    if not database.get_mirror_by_ip(address):
                        database.log.info("IP %s was new", address)
                        location = helper.get_mirror_location(address)
                        country = location['country']['names']['en']
                        city = location['city']['names']['en'] if 'city' in location else ""
                        area = ", ".join([s['names']['en'] for s in location['subdivisions']]) if 'subdivisions' in location else ""
                        database.conn.execute("INSERT INTO mirror_ip (MirrorID, IP, Country, Area, City) VALUES (?, ?, ?, ?, ?)", [mirror_id, address, country, area, city])
                    else:
                        database.log.info("IP %s was already in the database", address)
        database.conn.commit()
        database.conn.close()
Exemple #2
0
class DatabaseTestCase(unittest.TestCase):
    def setUp(self):
        self.database = MirrorDatabase('test.db')
        self.downloader = MirrorDownloader()

    def test_database_creation(self):
        database = MirrorDatabase('test.db')
        database.create_database()
        assert database.conn.table_exists('mirrors')
        assert database.conn.table_exists('mirror_ip')
        database.conn.close()

    def test_get_mirrors(self):
        lists = self.downloader.get_mirrors_from_modules()
        self.assertIsNotNone(lists)

    def tearDown(self):
        self.database.conn.close()
        os.remove('test.db')
Exemple #3
0
 def setUp(self):
     self.database = MirrorDatabase('test.db')
     self.downloader = MirrorDownloader()