1818CONFIG_FILE = 'config.ini'
1919LOG_FILE = 'bwscanner.log'
2020
21- CTX = dict (
22- default_map = read_config (os .path .join (DATA_DIR , CONFIG_FILE ))
23- )
24-
2521
2622class ScanInstance (object ):
2723 """
2824 Store the configuration and state for the CLI tool.
2925 """
30- def __init__ (self , data_dir ):
26+ def __init__ (self , data_dir , measurement_dir = None ):
3127 self .data_dir = data_dir
32- self .measurement_dir = os .path .join (data_dir , 'measurements' )
33- self .tor_dir = os .path .join (data_dir , 'tor_data' )
28+ if measurement_dir is None :
29+ self .measurement_dir = os .path .join (data_dir , 'measurements' )
30+ else :
31+ self .measurement_dir = measurement_dir
32+ self .tor_state = None
3433
3534 def __repr__ (self ):
3635 return '<BWScan %r>' % self .data_dir
@@ -40,26 +39,19 @@ def __repr__(self):
4039
4140
4241# FIXME: change all options to take defaults from CTX, ie config file?
43- @click .group (context_settings = CTX )
42+ @click .group ()
4443@click .option ('--data-dir' , type = click .Path (),
45- default = os .environ .get ("BWSCANNER_DATADIR" ,
46- CTX .get ('data_dir' ,
47- click .get_app_dir (APP_NAME ))),
4844 help = 'Directory where bwscan should stores its measurements and '
4945 'other data.' )
5046@click .option ('-l' , '--loglevel' ,
5147 help = 'The logging level the scanner will use (default: info)' ,
52- default = CTX . get ( 'loglevel' , 'info' ),
53- type = click . Choice ( ['debug' , 'info' , 'warn' , 'error' , 'critical' ]))
48+ type = click . Choice (
49+ ['debug' , 'info' , 'warn' , 'error' , 'critical' ]))
5450@click .option ('-f' , '--logfile' , type = click .Path (),
55- help = 'The file the log will be written to' ,
56- default = os .environ .get ("BWSCANNER_LOGFILE" ,
57- CTX .get ('logfile' , LOG_FILE )))
51+ help = 'The file the log will be written to' )
5852@click .option ('--launch-tor/--no-launch-tor' ,
59- default = CTX .get ('launch_tor' , False ),
6053 help = 'Launch Tor or try to connect to an existing Tor instance.' )
6154@click .option ('--circuit-build-timeout' ,
62- default = CTX .get ('circuit_build_timeout' , 20 ),
6355 help = 'Option passed when launching Tor.' )
6456@click .version_option (BWSCAN_VERSION )
6557@click .pass_context
@@ -69,9 +61,12 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
6961 bandwidth measurements can then be aggregate to create the bandwidth
7062 values used by the Tor bandwidth authorities when creating the Tor consensus.
7163 """
64+ for k ,v in ctx .default_map .items ():
65+ if ctx .params .get (k ) is None :
66+ ctx .params [k ] = v
67+
7268 # Create the data directory if it doesn't exist
73- data_dir = os .path .abspath (data_dir )
74- ctx .obj = ScanInstance (data_dir )
69+ ctx .obj = ScanInstance (ctx .params .get ('data_dir' ))
7570
7671 if not os .path .isdir (ctx .obj .measurement_dir ):
7772 os .makedirs (ctx .obj .measurement_dir )
@@ -81,7 +76,8 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
8176 ctx .obj .tor_dir )
8277
8378 # Set up the logger to only output log lines of level `loglevel` and above.
84- setup_logging (log_level = loglevel , log_name = logfile )
79+ setup_logging (log_level = ctx .params .get ('loglevel' ),
80+ log_name = ctx .params .get ('logfile' ))
8581
8682
8783@cli .command (short_help = "Measure the Tor relays." )
@@ -103,7 +99,8 @@ def scan(scan, partitions, current_partition, timeout, request_limit):
10399
104100 # XXX: check that each run is producing the same input set!
105101 scan_time = str (int (time .time ()))
106- scan_data_dir = os .path .join (scan .measurement_dir , '{}.running' .format (scan_time ))
102+ scan_data_dir = os .path .join (scan .measurement_dir ,
103+ '{}.running' .format (scan_time ))
107104 if not os .path .isdir (scan_data_dir ):
108105 os .makedirs (scan_data_dir )
109106
@@ -178,3 +175,8 @@ def aggregate(scan, scan_name, previous):
178175 scan .tor_state .addErrback (lambda failure : log .failure ("Unexpected error" ))
179176 scan .tor_state .addCallback (lambda _ : reactor .stop ())
180177 reactor .run ()
178+
179+
180+ def start ():
181+ config = read_config (os .path .join (DATA_DIR , CONFIG_FILE ))
182+ return cli (default_map = config )
0 commit comments