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

Commit

Permalink
Add config file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
juga0 committed Feb 2, 2018
1 parent 943e003 commit ff90b33
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
28 changes: 28 additions & 0 deletions bwscanner/configutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os.path
from shutil import copyfile
from ConfigParser import SafeConfigParser

from bwscanner.logger import log


def read_config(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))


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


def copy_config(cfg_path, cfg_default_path=None):
if cfg_default_path is None:
cfg_default_path = os.path.join(os.path.dirname(os.path.dirname(
os.path.abspath(__file__))), 'data', 'config.ini')
log.debug("cfg_default_path %s" % cfg_default_path)
copyfile(cfg_default_path, cfg_path)
29 changes: 22 additions & 7 deletions bwscanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
from twisted.internet import reactor

from bwscanner.attacher import connect_to_tor
from bwscanner.configutil import read_config
from bwscanner.logger import setup_logging, log
from bwscanner.measurement import BwScan
from bwscanner.aggregate import write_aggregate_data


BWSCAN_VERSION = '0.0.1'
APP_NAME = 'bwscanner'
DATA_DIR = os.environ.get("BWSCANNER_DATADIR", click.get_app_dir(APP_NAME))
CONFIG_FILE = 'config.ini'
LOG_FILE = 'bwscanner.log'

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


class ScanInstance(object):
Expand All @@ -28,16 +37,22 @@ def __repr__(self):

pass_scan = click.make_pass_decorator(ScanInstance)


@click.group()
# FIXME: change all options to take defaults from CTX, ie config file?
@click.group(context_settings=CTX)
@click.option('--data-dir', type=click.Path(),
default=os.environ.get("BWSCANNER_DATADIR", click.get_app_dir('bwscanner')),
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='info', 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", 'bwscanner.log'))
@click.option('-l', '--loglevel',
help='The logging level the scanner will use (default: info)',
default='info',
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)))
@click.option('--launch-tor/--no-launch-tor', default=False,
help='Launch Tor or try to connect to an existing Tor instance.')
@click.option('--circuit-build-timeout', default=20,
Expand Down

0 comments on commit ff90b33

Please sign in to comment.