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
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
):
30 DB_FILENAME
= filename
36 parser
= argparse
.ArgumentParser()
38 "distributor", type=type(''),
39 help="The new distributor to assign the unallocated bridges to")
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"]
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
)
66 remaining_bridges
= db
.getBridgesForDistributor('unallocated')
67 assert len(remaining_bridges
) == 0
73 if __name__
== "__main__":
74 options
= checkOptions(getOptions())
76 if options
.db_filename
:
77 setDBFilename(options
.db_filename
)
81 assignBridgesToDistributor(db
, options
.distributor
)