Parse bridge blocking info from SQL database.
[tor-bridgedb.git] / scripts / assign-reserve-bridges.py
blob05856347079e0267236eaf88d4004064829a3e0b
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
4 # This file is part of BridgeDB, a Tor bridge distribution system.
6 # :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@torproject.org>
7 # please also see AUTHORS file
8 # :copyright: (c) 2013-2018 Isis Lovecruft
9 # (c) 2007-2018, The Tor Project, Inc.
10 # (c) 2007-2018, all entities within the AUTHORS file
11 # :license: 3-clause BSD, see included LICENSE for information
13 """Assign all unallocated bridges to another distributor."""
15 from __future__ import print_function
17 import argparse
18 import logging
20 import bridgedb.Storage
22 #: The path to the sqlite database file containing bridges.
23 DB_FILENAME = "bridgedist.db.sqlite"
25 logger = logging.getLogger("assign-reserve-bridges")
26 logger.setLevel(logging.DEBUG)
28 def setDBFilename(filename):
29 global DB_FILENAME
30 DB_FILENAME = filename
32 def getDBFilename():
33 return DB_FILENAME
35 def getOptions():
36 parser = argparse.ArgumentParser()
37 parser.add_argument(
38 "distributor", type=type(''),
39 help="The new distributor to assign the unallocated bridges to")
40 parser.add_argument(
41 "-f", "--db-filename", type=type(''),
42 help="The path to the database file")
44 return parser.parse_args()
46 def checkOptions(options):
47 assert options.distributor
48 assert options.distributor in ["https", "email", "moat"]
49 return options
51 def getDB():
52 return bridgedb.Storage.Database(getDBFilename())
54 def assignBridgesToDistributor(db, distributor):
55 unallocated = db.getBridgesForDistributor('unallocated')
57 logging.info("Assigning %d unallocated bridges to distributor %s"
58 % (len(unallocated), distributor))
59 print("Assigning %d unallocated bridges to distributor %s"
60 % (len(unallocated), distributor))
62 for bridge in unallocated:
63 db.updateDistributorForHexKey(distributor, bridge.hex_key)
64 db.commit()
66 remaining_bridges = db.getBridgesForDistributor('unallocated')
67 assert len(remaining_bridges) == 0
69 logging.info("Done!")
70 print("Done!")
73 if __name__ == "__main__":
74 options = checkOptions(getOptions())
76 if options.db_filename:
77 setDBFilename(options.db_filename)
79 db = getDB()
81 assignBridgesToDistributor(db, options.distributor)