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

Commit

Permalink
Replace click context settings by default_map
Browse files Browse the repository at this point in the history
* Create start function to call cli with parsed config
* Parse bw_files section
* Convert parsed config values to their type
  • Loading branch information
juga0 committed Mar 23, 2018
1 parent e9642bb commit 388e824
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
23 changes: 18 additions & 5 deletions bwscanner/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@


def read_config(cfg_path):
log.debug('reading config %s' % cfg_path)
log.debug('Reading config %s' % cfg_path)
if not config_exists(cfg_path):
copy_config(cfg_path)
parser = SafeConfigParser()
parser.read([cfg_path])
# FIXME: handle section names
section = 'default'
return dict(parser.items(section))

cfg_dict = dict(parser.items('default'))
int_keys = cfg_dict['int_keys'].split(' ')
bool_keys = cfg_dict['bool_keys'].split(' ')
for k in int_keys:
cfg_dict[k] = int(cfg_dict[k])
for i in bool_keys:
cfg_dict[k] = bool(cfg_dict[k])
bw_files = dict(parser.items('bw_files'))
cfg_bw_files = {}
for k, v in bw_files.items():
print(k, v)
if 'm' in k:
number = k.rstrip('m')
size = 1024 * int(number)
cfg_bw_files[size] = (k.upper(), v)
cfg_dict['bw_files'] = cfg_bw_files
return cfg_dict

def config_exists(cfg_path):
return os.path.isfile(cfg_path)
Expand Down
45 changes: 24 additions & 21 deletions bwscanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
CONFIG_FILE = 'config.ini'
LOG_FILE = 'bwscanner.log'

CTX = dict(
default_map=read_config(os.path.join(DATA_DIR, CONFIG_FILE))
)


class ScanInstance(object):
"""
Store the configuration and state for the CLI tool.
"""
def __init__(self, data_dir):
def __init__(self, data_dir, measurement_dir=None):
self.data_dir = data_dir
self.measurement_dir = os.path.join(data_dir, 'measurements')
if measurement_dir is None:
self.measurement_dir = os.path.join(data_dir, 'measurements')
else:
self.measurement_dir = measurement_dir
self.tor_state = None

def __repr__(self):
return '<BWScan %r>' % self.data_dir
Expand All @@ -39,26 +39,19 @@ def __repr__(self):


# FIXME: change all options to take defaults from CTX, ie config file?
@click.group(context_settings=CTX)
@click.group()
@click.option('--data-dir', type=click.Path(),
default=os.environ.get("BWSCANNER_DATADIR",
CTX.get('data_dir',
click.get_app_dir(APP_NAME))),
help='Directory where bwscan should stores its measurements and '
'other data.')
@click.option('-l', '--loglevel',
help='The logging level the scanner will use (default: info)',
default=CTX.get('loglevel', 'info'),
type=click.Choice(['debug', 'info', 'warn', 'error', 'critical']))
type=click.Choice(
['debug', 'info', 'warn', 'error', 'critical']))
@click.option('-f', '--logfile', type=click.Path(),
help='The file the log will be written to',
default=os.environ.get("BWSCANNER_LOGFILE",
CTX.get('logfile', LOG_FILE)))
help='The file the log will be written to')
@click.option('--launch-tor/--no-launch-tor',
default=CTX.get('launch_tor', False),
help='Launch Tor or try to connect to an existing Tor instance.')
@click.option('--circuit-build-timeout',
default=CTX.get('circuit_build_timeout', 20),
help='Option passed when launching Tor.')
@click.version_option(BWSCAN_VERSION)
@click.pass_context
Expand All @@ -68,9 +61,12 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
bandwidth measurements can then be aggregate to create the bandwidth
values used by the Tor bandwidth authorities when creating the Tor consensus.
"""
for k,v in ctx.default_map.items():
if ctx.params.get(k) is None:
ctx.params[k] = v

# Create the data directory if it doesn't exist
data_dir = os.path.abspath(data_dir)
ctx.obj = ScanInstance(data_dir)
ctx.obj = ScanInstance(ctx.params.get('data_dir'))

if not os.path.isdir(ctx.obj.measurement_dir):
os.makedirs(ctx.obj.measurement_dir)
Expand All @@ -79,7 +75,8 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
ctx.obj.tor_state = connect_to_tor(launch_tor, circuit_build_timeout)

# Set up the logger to only output log lines of level `loglevel` and above.
setup_logging(log_level=loglevel, log_name=logfile)
setup_logging(log_level=ctx.params.get('loglevel'),
log_name=ctx.params.get('logfile'))


@cli.command(short_help="Measure the Tor relays.")
Expand All @@ -101,7 +98,8 @@ def scan(scan, partitions, current_partition, timeout, request_limit):

# XXX: check that each run is producing the same input set!
scan_time = str(int(time.time()))
scan_data_dir = os.path.join(scan.measurement_dir, '{}.running'.format(scan_time))
scan_data_dir = os.path.join(scan.measurement_dir,
'{}.running'.format(scan_time))
if not os.path.isdir(scan_data_dir):
os.makedirs(scan_data_dir)

Expand Down Expand Up @@ -176,3 +174,8 @@ def aggregate(scan, scan_name, previous):
scan.tor_state.addErrback(lambda failure: log.failure("Unexpected error"))
scan.tor_state.addCallback(lambda _: reactor.stop())
reactor.run()


def start():
config = read_config(os.path.join(DATA_DIR, CONFIG_FILE))
return cli(default_map=config)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
include_package_data=True,
entry_points={
"console_scripts": [
'bwscan = bwscanner.scanner:cli',
'bwscan = bwscanner.scanner:start',
]},
)

0 comments on commit 388e824

Please sign in to comment.