/usr/lib/python3/dist-packages/plainbox/impl/commands/checkbox.py is in python3-plainbox 0.5.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | # This file is part of Checkbox.
#
# Copyright 2012-2013 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox 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 Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`plainbox.impl.commands.checkbox` -- mix-in for checkbox commands
======================================================================
.. warning::
THIS MODULE DOES NOT HAVE STABLE PUBLIC API
"""
import os
from argparse import FileType
from logging import getLogger
import itertools
from plainbox.i18n import gettext as _
from plainbox.impl.secure.qualifiers import RegExpJobQualifier
from plainbox.impl.secure.qualifiers import WhiteList
from plainbox.impl.secure.qualifiers import select_jobs
from plainbox.impl.secure.rfc822 import FileTextSource
logger = getLogger("plainbox.commands.checkbox")
class CheckBoxInvocationMixIn:
def __init__(self, provider_list, config):
self.provider_list = provider_list
self.config = config
def get_job_list(self, ns):
"""
Load and return a list of JobDefinition instances
"""
return list(
itertools.chain(*[
p.load_all_jobs()[0] for p in self.provider_list]))
def get_whitelist_from_file(self, filename, stream=None):
"""
Load a whitelist from a file, with special behavior.
:param filename:
name of the file to load
:param stream:
(optional) pre-opened stream pointing at the whitelist
:returns:
The loaded whitelist or None if loading fails for any reason
This function implements special loading behavior for whitelists that
makes them inherit the implicit namespace of the provider they may be a
part of. Before loading the whitelist directly from the file, all known
providers are interrogated to see if any of them has a whitelist that
was loaded from the same file (as indicated by os.path.realpath())
The stream argument can be provided if the caller already has an open
file object, which is typically the case when working with argparse.
"""
# Look up a whitelist with the same name in any of the providers
wanted_realpath = os.path.realpath(filename)
for provider in self.provider_list:
for whitelist in provider.get_builtin_whitelists():
if (whitelist.origin is not None
and whitelist.origin.source is not None
and isinstance(whitelist.origin.source,
FileTextSource)
and os.path.realpath(
whitelist.origin.source.filename) ==
wanted_realpath):
logger.debug(
_("Using whitelist %r obtained from provider %r"),
whitelist.name, provider)
return whitelist
# Or load it directly
try:
if stream is not None:
return WhiteList.from_string(stream.read(), filename=filename)
else:
return WhiteList.from_file(filename)
except Exception as exc:
logger.warning(
_("Unable to load whitelist %r: %s"), filename, exc)
def _get_matching_job_list(self, ns, job_list):
logger.debug("_get_matching_job_list(%r, %r)", ns, job_list)
qualifier_list = []
# Add whitelists
for whitelist_file in ns.whitelist:
qualifier = self.get_whitelist_from_file(
whitelist_file.name, whitelist_file)
if qualifier is not None:
qualifier_list.append(qualifier)
# Add all the --include jobs
for pattern in ns.include_pattern_list:
try:
qualifier = RegExpJobQualifier(
'^{}$'.format(pattern), inclusive=True)
except Exception as exc:
logger.warning(
_("Incorrect pattern %r: %s"), pattern, exc)
else:
qualifier_list.append(qualifier)
# Add all the --exclude jobs
for pattern in ns.exclude_pattern_list:
try:
qualifier = RegExpJobQualifier(
'^{}$'.format(pattern), inclusive=False)
except Exception as exc:
logger.warning(
_("Incorrect pattern %r: %s"), pattern, exc)
else:
qualifier_list.append(qualifier)
logger.debug("select_jobs(%r, %r)", job_list, qualifier_list)
return select_jobs(job_list, qualifier_list)
class CheckBoxCommandMixIn:
"""
Mix-in class for plainbox commands that want to discover and load checkbox
jobs
"""
def enhance_parser(self, parser):
"""
Add common options for job selection to an existing parser
"""
group = parser.add_argument_group(title=_("job definition options"))
group.add_argument(
'-i', '--include-pattern', action="append",
metavar=_('PATTERN'), default=[], dest='include_pattern_list',
# TRANSLATORS: this is in imperative form
help=_("include jobs matching the given regular expression"))
group.add_argument(
'-x', '--exclude-pattern', action="append",
metavar=_("PATTERN"), default=[], dest='exclude_pattern_list',
# TRANSLATORS: this is in imperative form
help=_("exclude jobs matching the given regular expression"))
# TODO: Find a way to handle the encoding of the file
group.add_argument(
'-w', '--whitelist',
action="append",
metavar=_("WHITELIST"),
default=[],
type=FileType("rt"),
# TRANSLATORS: this is in imperative form
help=_("load whitelist containing run patterns"))
|