Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 4af4292

Browse files
committed
Replace click context settings by default_map
* Create start function to call cli with parsed config * Parse bw_files section * Convert parsed config values to their type
1 parent 03978c9 commit 4af4292

3 files changed

Lines changed: 43 additions & 28 deletions

File tree

bwscanner/configutil.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@
66

77

88
def read_config(cfg_path):
9-
log.debug('reading config %s' % cfg_path)
9+
log.debug('Reading config %s' % cfg_path)
1010
if not config_exists(cfg_path):
1111
copy_config(cfg_path)
1212
parser = SafeConfigParser()
1313
parser.read([cfg_path])
14-
# FIXME: handle section names
15-
section = 'default'
16-
return dict(parser.items(section))
17-
14+
cfg_dict = dict(parser.items('default'))
15+
int_keys = cfg_dict['int_keys'].split(' ')
16+
bool_keys = cfg_dict['bool_keys'].split(' ')
17+
for k in int_keys:
18+
cfg_dict[k] = int(cfg_dict[k])
19+
for i in bool_keys:
20+
cfg_dict[k] = bool(cfg_dict[k])
21+
bw_files = dict(parser.items('bw_files'))
22+
cfg_bw_files = {}
23+
for k, v in bw_files.items():
24+
print(k, v)
25+
if 'm' in k:
26+
number = k.rstrip('m')
27+
size = 1024 * int(number)
28+
cfg_bw_files[size] = (k.upper(), v)
29+
cfg_dict['bw_files'] = cfg_bw_files
30+
return cfg_dict
1831

1932
def config_exists(cfg_path):
2033
return os.path.isfile(cfg_path)

bwscanner/scanner.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818
CONFIG_FILE = 'config.ini'
1919
LOG_FILE = 'bwscanner.log'
2020

21-
CTX = dict(
22-
default_map=read_config(os.path.join(DATA_DIR, CONFIG_FILE))
23-
)
24-
2521

2622
class 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)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
include_package_data=True,
4343
entry_points={
4444
"console_scripts": [
45-
'bwscan = bwscanner.scanner:cli',
45+
'bwscan = bwscanner.scanner:start',
4646
]},
4747
)

0 commit comments

Comments
 (0)