pvl.args: parser(config=False) to disable --config, in case app defines its own --config
"""
CLI argument handling; common stuff: logging
"""
import codecs
import grp
import logging
import optparse
import os
import pwd
import sys
import logging; log = logging.getLogger('pvl.args')
CONFDIR = '/etc/pvl'
def parser (parser, setuid=None, config=True) :
"""
Return an optparse.OptionGroup.
"""
if setuid is None :
# autodetect: only if we will be capable of
# XXX: use linux capabilities?
setuid = (os.geteuid() == 0)
general = optparse.OptionGroup(parser, "General options")
general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.ERROR, help="Less output")
general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output")
general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
general.add_option('--log-file', help="Log to file")
general.add_option('--debug-module', action='append', metavar='MODULE',
help="Enable logging for the given logger/module name")
if config:
parser.add_option('--config', metavar='PATH', action='append',
help="Read option defaults from config")
parser.add_option('--config-encoding', metavar='CHARSET', default='utf-8',
help="Unicode decoding for config file")
if setuid :
general.add_option('--uid', help="Change uid")
general.add_option('--gid', help="Change gid")
# defaults
parser.set_defaults(
setuid = setuid,
logname = parser.prog,
loglevel = logging.WARN,
debug_module = [],
config = [],
)
return general
def options (**options) :
"""
Synthensise options.
"""
return optparse.Values(options)
def apply_setid (options, rootok=None) :
"""
Drop privileges if running as root.
XXX: this feature isn't very useful (import-time issues etc), but in certain cases (syslog-ng -> python),
it's difficult to avoid this without some extra wrapper tool..?
"""
# --uid -> pw
if not options.uid :
pw = None
elif options.uid.isdigit() :
pw = pwd.getpwuid(int(options.uid))
else :
pw = pwd.getpwnam(options.uid)
# --gid -> gr
if not options.gid and not pw :
gr = None
elif not options.gid :
gr = grp.getgrgid(pw.pw_gid)
elif options.gid.isdigit() :
gr = grp.getgrgid(str(options.gid))
else :
gr = grp.getgrnam(options.gid)
if gr :
# XXX: secondary groups? seem to get cleared
log.info("setgid: %s: %s", gr.gr_name, gr.gr_gid)
os.setgid(gr.gr_gid)
if pw :
log.info("setuid: %s: %s", pw.pw_name, pw.pw_uid)
os.setuid(pw.pw_uid)
elif os.getuid() == 0 :
if rootok :
log.info("running as root")
else :
log.error("refusing to run as root, use --uid 0 to override")
sys.exit(2)
def apply_file (path=None, mode='r', charset=None) :
"""
Open (unicode-enabled) file from path, with - using stdio.
"""
if not path or path == '-' :
# use stdin/out based on mode
stream, func = {
'r': (sys.stdin, codecs.getreader),
'w': (sys.stdout, codecs.getwriter),
}[mode[0]]
if charset :
return func(charset)(stream)
else :
return stream
else :
if charset :
return codecs.open(path, mode, charset)
else :
return open(path, mode)
def apply_files (paths, *args, **opts) :
"""
Open one or more files from given paths, defaulting to stdio.
"""
if paths :
return [apply_file(path, *args, **opts) for path in paths]
else:
return [apply_file(None, *args, **opts)]
import configobj
import copy
import optparse
class Options (object) :
"""
Custom optparse.Values implementation.
Passed to OptionParser.parse_args(), called by Option.take_action():
setattr(values, dest, ...)
values.ensure_value(dest, ...)
Implements _merge(...) to merge in options from multiple sources.
"""
def __init__ (self, parser, defaults={ }) :
self._parser = parser
self._defaults = defaults
self._options = { }
def __setattr__ (self, name, value) :
if name.startswith('_') :
self.__dict__[name] = value
else :
self._options[name] = value
def ensure_value (self, name, default) :
if name in self._options :
pass
elif name in self._defaults :
self._options[name] = copy.copy(self._defaults[name])
else :
self._options[name] = default
return self._options[name]
def _merge (self, options) :
"""
Merge in options from given Options.
"""
# TODO: lists?
self._options.update(options._options)
def __getattr__ (self, name) :
if name.startswith('_') :
raise AttributeError(name)
if name in self._options :
return self._options[name]
elif name in self._defaults :
return self._defaults[name]
else :
raise AttributeError(name)
def error (self, msg) :
"""
Raises an optparse error.
"""
self._parser.error(msg)
def apply_config (options, parser, config, encoding=None) :
"""
Load options from config.
"""
import configobj
config = configobj.ConfigObj(config,
encoding = options.config_encoding if encoding is None else encoding,
)
config_options = Options(parser, options._defaults)
# load scalars
for scalar in config.scalars :
# option from config
option = parser._long_opt.get('--' + scalar)
if not option :
raise optparse.BadOptionError(scalar)
# value from config
if option.takes_value() :
value = config[scalar]
else :
# ignore
value = None
# apply
option.process(scalar, value, config_options, parser)
# apply in actual options
config_options._merge(options)
return config_options
def apply_core (options) :
"""
Apply the optparse options.
"""
# configure
logging.basicConfig(
# XXX: log Class.__init__ as Class, not __init__?
format = '%(levelname)8s %(name)20s.%(funcName)s: %(message)s',
level = options.loglevel,
filename = options.log_file,
)
# enable debugging for specific targets
for logger in options.debug_module :
logging.getLogger(logger).setLevel(logging.DEBUG)
def parse (parser, argv, package=None, module=None) :
"""
Parse options, args from argv.
"""
prog = os.path.basename(argv[0])
options, args = parser.parse_args(argv[1:], values=Options(parser, parser.defaults))
# XXX: apply the core logging parts...
apply_core(options)
if package and module:
conf_path = os.path.join(CONFDIR, package, module + '.conf')
if os.path.exists(conf_path):
log.info("%s", conf_path)
options = apply_config(options, parser, conf_path)
else:
log.debug("%s: skip", conf_path)
for config in options.config :
log.info("%s", config)
options = apply_config(options, parser, config)
return options, args
def apply (options, logname=None, rootok=True) :
"""
Apply the optparse options.
"""
if logname :
prefix = options.logname + ': '
else :
prefix = ''
# configure
logging.basicConfig(
# XXX: log Class.__init__ as Class, not __init__?
format = prefix + '%(levelname)8s %(name)20s.%(funcName)s: %(message)s',
level = options.loglevel,
filename = options.log_file,
)
# TODO: use --quiet for stdout output?
options.quiet = options.loglevel > logging.WARN
if options.setuid :
if options.uid or options.gid or not rootok :
# set uid/gid
apply_setid(options, rootok=rootok)
# enable debugging for specific targets
for logger in options.debug_module :
logging.getLogger(logger).setLevel(logging.DEBUG)
def main (main) :
"""
Run given main func.
"""
sys.exit(main(sys.argv))