X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=hippotatlib%2F__init__.py;h=ab404fab9ac0188e955528e2764bde428eccd1de;hb=80e963a152d44e4a795d654e8e01a11f64909ed4;hp=48ece836b33ad624248618b93f6d91b900b8d489;hpb=5a37bac8d49a3735c29e211c1642b101e2ff9517;p=hippotat.git diff --git a/hippotatlib/__init__.py b/hippotatlib/__init__.py index 48ece83..ab404fa 100644 --- a/hippotatlib/__init__.py +++ b/hippotatlib/__init__.py @@ -1,4 +1,26 @@ # -*- python -*- +# +# Hippotat - Asinine IP Over HTTP program +# hippotatlib/__init__.py - common library code +# +# Copyright 2017 Ian Jackson +# +# GPLv3+ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program, in the file GPLv3. If not, +# see . + import signal signal.signal(signal.SIGINT, signal.SIG_DFL) @@ -40,6 +62,7 @@ class DBG(twisted.python.constants.Names): CONFIG = NamedConstant() ROUTE = NamedConstant() DROP = NamedConstant() + OWNSOURCE = NamedConstant() FLOW = NamedConstant() HTTP = NamedConstant() TWISTED = NamedConstant() @@ -77,22 +100,27 @@ def log_debug(dflag, msg, idof=None, d=None): msg += ' ' + d + trunc log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg) +def logevent_is_boringtwisted(event): + try: + if event.get('log_level') != LogLevel.info: + return False + dflag = event.get('dflag') + if dflag is False : return False + if dflag in debug_set: return False + if dflag is None and DBG.TWISTED in debug_set: return False + return True + except Exception: + print(traceback.format_exc(), file=org_stderr) + return False + @implementer(twisted.logger.ILogFilterPredicate) class LogNotBoringTwisted: def __call__(self, event): - yes = twisted.logger.PredicateResult.yes - no = twisted.logger.PredicateResult.no - try: - if event.get('log_level') != LogLevel.info: - return yes - dflag = event.get('dflag') - if dflag is False : return yes - if dflag in debug_set: return yes - if dflag is None and DBG.TWISTED in debug_set: return yes - return no - except Exception: - print(traceback.format_exc(), file=org_stderr) - return yes + return ( + twisted.logger.PredicateResult.no + if logevent_is_boringtwisted(event) else + twisted.logger.PredicateResult.yes + ) #---------- default config ---------- @@ -523,7 +551,7 @@ def common_startup(process_cfg): # is a directory log('directory') re = regexp.compile('[^-A-Za-z0-9_]') - for f in os.listdir(cdir): + for f in os.listdir(pathname): if re.search(f): continue subpath = pathname + '/' + f try: @@ -539,12 +567,26 @@ def common_startup(process_cfg): need_defcfg = False readconfig(value) + def oc_extra_config(od,os, value, op): + readconfig(value) + + def read_defconfig(): + readconfig('/etc/hippotat/config.d', False) + readconfig('/etc/hippotat/passwords.d', False) + readconfig('/etc/hippotat/master.cfg', False) + + def oc_defconfig(od,os, value, op): + nonlocal need_defcfg + need_defcfg = False + read_defconfig(value) + def dfs_less_detailed(dl): return [df for df in DBG.iterconstants() if df <= dl] def ds_default(od,os,dl,op): global debug_set - debug_set = set(dfs_less_detailed(debug_def_detail)) + debug_set.clear + debug_set |= set(dfs_less_detailed(debug_def_detail)) def ds_select(od,os, spec, op): for it in spec.split(','): @@ -601,22 +643,34 @@ just `+': all DFLAGs. action='callback', callback= oc_config) + optparser.add_option('--extra-config', + nargs=1, + type='string', + metavar='CONFIGFILE', + dest='configfile', + action='callback', + callback= oc_extra_config) + + optparser.add_option('--default-config', + action='callback', + callback= oc_defconfig) + (opts, args) = optparser.parse_args() if len(args): optparser.error('no non-option arguments please') if need_defcfg: - readconfig('/etc/hippotat/config', False) - readconfig('/etc/hippotat/config.d', False) + read_defconfig() try: (pss, pcs) = _cfg_process_putatives() - process_cfg(pss, pcs) + process_cfg(opts, pss, pcs) except (configparser.Error, ValueError): traceback.print_exc(file=sys.stderr) print('\nInvalid configuration, giving up.', file=sys.stderr) sys.exit(12) - #print(repr(debug_set), file=sys.stderr) + + #print('X', debug_set, file=sys.stderr) log_formatter = twisted.logger.formatEventAsClassicLogText stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter) @@ -625,15 +679,16 @@ just `+': all DFLAGs. stdsomething_obs = twisted.logger.FilteringLogObserver( stderr_obs, [pred], stdout_obs ) - log_observer = twisted.logger.FilteringLogObserver( + global file_log_observer + file_log_observer = twisted.logger.FilteringLogObserver( stdsomething_obs, [LogNotBoringTwisted()] ) #log_observer = stdsomething_obs twisted.logger.globalLogBeginner.beginLoggingTo( - [ log_observer, crash_on_critical ] + [ file_log_observer, crash_on_critical ] ) def common_run(): log_debug(DBG.INIT, 'entering reactor') if not _crashing: reactor.run() - print('CRASHED (end)', file=sys.stderr) + print('ENDED', file=sys.stderr)