Esempio n. 1
0
    def process_check(self,
                      args,
                      silence=False,
                      preset_values={},
                      namespace=None,
                      **settings):
        p = commandline.ArgumentParser(domain=False, color=False)
        p.plugin = p.add_argument_group('plugin options')
        self.addon_kls.mangle_argparser(p)
        args, unknown_args = p.parse_known_args(args, namespace)
        self.assertEqual(unknown_args, [])
        orig_out, orig_err = None, None
        for attr, val in preset_values.iteritems():
            setattr(args, attr, val)
        try:
            if silence:
                orig_out = sys.stdout
                orig_err = sys.stderr
                sys.stdout = sys.stderr = open("/dev/null", "w")
            self.addon_kls.check_args(p, args)
        finally:
            if silence:
                if orig_out:
                    sys.stdout.close()
                    sys.stdout = orig_out
                if orig_err:
                    sys.stderr.close()
                    sys.stderr = orig_err

        for attr, val in settings.iteritems():
            self.assertEqual(getattr(args, attr),
                             val,
                             msg="for args %r, %s must be %r, got %r" %
                             (args, attr, val, getattr(args, attr)))
        return args
Esempio n. 2
0
    def test_configuration_error(self):
        argparser = commandline.ArgumentParser(suppress=True)

        @argparser.bind_main_func
        def error_main(options, out, err):
            raise errors.ConfigurationError('bork')

        self.assertMain(-10, '', 'Error in configuration:\n bork\n', argparser,
                        [])
Esempio n. 3
0
    def test_argparse_with_invalid_args(self):
        argparser = commandline.ArgumentParser(suppress=True, add_help=False)

        @argparser.bind_main_func
        def main(options, out, err):
            pass

        # This is specifically asserting that if given a positional arg (the
        # '1'), which isn't valid in our argparse setup, it returns exit code 2
        # (standard argparse error() exit status).
        self.assertMain(2, '', '', argparser, ['1'])
Esempio n. 4
0
    def test_tty_detection(self):
        argparser = commandline.ArgumentParser(config=False,
                                               domain=False,
                                               color=True,
                                               debug=False,
                                               quiet=False,
                                               verbose=False,
                                               version=False)

        @argparser.bind_main_func
        def main(options, out, err):
            for f in (out, err):
                name = f.__class__.__name__
                if name.startswith("native_"):
                    name = name[len("native_"):]
                f.write(name, autoline=False)

        for args, out_kind, err_kind in (
            ([], 'TerminfoFormatter', 'PlainTextFormatter'),
            (['--color=n'], 'PlainTextFormatter', 'PlainTextFormatter'),
        ):
            master, out = self._get_pty_pair()
            err, err_getvalue = _stream_and_getvalue()

            try:
                commandline.main(argparser, args, out, err)
            except SystemExit as e:
                # Important, without this reading the master fd blocks.
                out.close()
                self.assertEqual(None, e.args[0])

                # There can be an xterm title update after this.
                #
                # XXX: Workaround py34 making it harder to read all data from a
                # pty due to issue #21090 (http://bugs.python.org/issue21090).
                out_name = ''
                try:
                    while True:
                        out_name += os.read(master.fileno(), 1).decode()
                except OSError as e:
                    if e.errno == errno.EIO:
                        pass
                    else:
                        raise

                master.close()
                self.assertTrue(
                    out_name.startswith(out_kind)
                    or out_name == 'PlainTextFormatter',
                    'expected %r, got %r' % (out_kind, out_name))
                self.assertEqual(err_kind, err_getvalue())
            else:
                self.fail('no exception raised')
Esempio n. 5
0
    def test_method_run(self):
        argparser = commandline.ArgumentParser(suppress=True)
        argparser.add_argument("--foon")

        @argparser.bind_main_func
        def run(options, out, err):
            out.write("args: %s" % (options.foon,))
            return 0

        self.assertMain(
            0, 'args: dar\n', '',
            argparser, args=['--foon', 'dar'])
Esempio n. 6
0
 def process_check(self, args, preset_values={},
                   namespace=None, addon_kls=None, **settings):
     addon_kls = addon_kls if addon_kls is not None else self.addon_kls
     p = commandline.ArgumentParser(domain=False, color=False)
     p.plugin = p.add_argument_group('plugin options')
     addon_kls.mangle_argparser(p)
     args, unknown_args = p.parse_known_args(args, namespace)
     assert unknown_args == []
     orig_out, orig_err = None, None
     for attr, val in preset_values.items():
         setattr(args, attr, val)
     addon_kls.check_args(p, args)
     for attr, val in settings.items():
         assert getattr(args, attr) == val, (
             f"for args {args!r}, {attr} must be {val!r}, got {getattr(args, attr)!r}")
     return args
Esempio n. 7
0
class ModifyConfigTest(TestCase, ArgParseMixin):

    parser = commandline.ArgumentParser(domain=False, version=False)
    parser.add_argument('--trigger', nargs=0, action=_Trigger)

    def parse(self, *args, **kwargs):
        """Overridden to allow the load_config call."""
        # argparse needs a list (it does make a copy, but it uses [:]
        # to do it, which is a noop on a tuple).
        namespace = self.parser.parse_args(list(args))

        # HACK: force skipping the actual config loading. Might want
        # to do something more complicated here to allow testing if
        # --empty-config actually works.
        namespace.empty_config = True

        return namespace

    def test_empty_config(self):
        self.assertTrue(self.parse('--empty-config', '--trigger'))

    def test_modify_config(self):
        namespace = self.parse(
            '--empty-config', '--new-config',
            'foo', 'class', 'pkgcore.test.util.test_commandline.sect',
            '--trigger')
        self.assertTrue(namespace.config.collapse_named_section('foo'))

        namespace = self.parse(
            '--empty-config', '--new-config',
            'foo', 'class', 'pkgcore.test.util.test_commandline.missing',
            '--add-config', 'foo', 'class',
            'pkgcore.test.util.test_commandline.sect',
            '--trigger')
        self.assertTrue(namespace.config.collapse_named_section('foo'))

        namespace = self.parse(
            '--empty-config',
            '--add-config', 'foo', 'inherit', 'missing',
            '--trigger')
        self.assertRaises(
            errors.ConfigurationError,
            namespace.config.collapse_named_section, 'foo')
Esempio n. 8
0
    def process_check(self,
                      args,
                      silence=False,
                      preset_values={},
                      namespace=None,
                      addon_kls=None,
                      **settings):
        addon_kls = addon_kls if addon_kls is not None else self.addon_kls
        p = commandline.ArgumentParser(domain=False, color=False)
        p.plugin = p.add_argument_group('plugin options')
        addon_kls.mangle_argparser(p)
        args, unknown_args = p.parse_known_args(args, namespace)
        assert unknown_args == []
        orig_out, orig_err = None, None
        for attr, val in preset_values.items():
            setattr(args, attr, val)
        try:
            if silence:
                orig_out = sys.stdout
                orig_err = sys.stderr
                sys.stdout = sys.stderr = open("/dev/null", "w")
            addon_kls.check_args(p, args)
        finally:
            if silence:
                if orig_out:
                    sys.stdout.close()
                    sys.stdout = orig_out
                if orig_err:
                    sys.stderr.close()
                    sys.stderr = orig_err

        for attr, val in settings.items():
            assert getattr(args, attr) == val, (
                f"for args {args!r}, {attr} must be {val!r}, got {getattr(args, attr)!r}"
            )
        return args
Esempio n. 9
0
"""collection of tools for Gentoo development

pkgdev provides a collection of tools for Gentoo development.
"""

from pkgcore.util import commandline

argparser = commandline.ArgumentParser(
    description=__doc__, help=False, subcmds=True, script=(__file__, __name__))
Esempio n. 10
0
from pkgcore.ebuild import inspect_profile
from pkgcore.ebuild import portageq as _portageq
from pkgcore.util import commandline

demandload(
    'collections:defaultdict',
    'itertools:groupby,islice',
    'operator:attrgetter,itemgetter',
    'snakeoil.sequences:iflatten_instance,unstable_unique',
    'pkgcore:fetch',
    'pkgcore.package:errors',
    'pkgcore.restrictions:packages',
)

pkgcore_opts = commandline.ArgumentParser(domain=False, script=(__file__, __name__))
argparser = commandline.ArgumentParser(
    suppress=True, description=__doc__, parents=(pkgcore_opts,))
subparsers = argparser.add_subparsers(description="report applets")

pkgsets = subparsers.add_parser(
    "pkgsets", description="pkgset related introspection")
mux = pkgsets.add_mutually_exclusive_group()
mux.add_argument(
    "--all", action='store_true', default=False,
    help="display info on all pkgsets")
mux.add_argument(
    "pkgsets", nargs="*", metavar="pkgset", default=[],
    action=commandline.StoreConfigObject,
    config_type='pkgset', store_name=True,
    help="pkgset to inspect")
Esempio n. 11
0
    'snakeoil.compatibility:IGNORED_EXCEPTIONS',
    'snakeoil.fileutils:AtomicWriteFile',
    'snakeoil.osutils:pjoin,listdir_dirs',
    'snakeoil.sequences:iter_stable_unique',
    'snakeoil.strings:pluralism',
    'pkgcore.ebuild:processor,triggers',
    'pkgcore.fs:contents,livefs',
    'pkgcore.merge:triggers@merge_triggers',
    'pkgcore.operations:observer',
    'pkgcore.package:mutated',
    'pkgcore.repository:multiplex',
    'pkgcore.restrictions:packages',
    'pkgcore.util.parserestrict:parse_match',
)

pkgcore_opts = commandline.ArgumentParser(domain=False,
                                          script=(__file__, __name__))
argparser = commandline.ArgumentParser(suppress=True,
                                       description=__doc__,
                                       parents=(pkgcore_opts, ))
subparsers = argparser.add_subparsers(description="general system maintenance")

shared_options = (commandline.ArgumentParser(config=False,
                                             color=False,
                                             debug=False,
                                             quiet=False,
                                             verbose=False,
                                             version=False,
                                             domain=False,
                                             add_help=False), )
shared_options_domain = (commandline.ArgumentParser(config=False,
                                                    color=False,
Esempio n. 12
0
# Copyright: 2005-2011 Brian Harring <[email protected]
# Copyright: 2006 Marien Zwart <*****@*****.**>
# License: BSD/GPL2

"""repository cache clone utility"""

import time

from pkgcore.util import commandline

argparser = commandline.ArgumentParser(
    domain=False, description=__doc__, script=(__file__, __name__))
argparser.add_argument(
    "source", config_type='cache', priority=20,
    action=commandline.StoreConfigObject,
    help="source cache to copy data from")
argparser.add_argument(
    "target", config_type='cache', priority=21,
    action=commandline.StoreConfigObject, writable=True,
    help="target cache to update.  Must be writable.")


@argparser.bind_main_func
def main(options, out, err):
    if options.target.readonly:
        argparser.error(
            "can't update cache label '%s', it's marked readonly." %
            (options.target,))

    source, target = options.source, options.target
    if not target.autocommits:
Esempio n. 13
0
# Copyright: 2005-2011 Brian Harring <[email protected]
# Copyright: 2006 Marien Zwart <*****@*****.**>
# License: BSD/GPL2

"""low-level ebuild utility"""

from itertools import izip
import os

from pkgcore.ebuild import atom
from pkgcore.ebuild.errors import MalformedAtom
from pkgcore.operations import observer, format
from pkgcore.util import commandline


argparser = commandline.ArgumentParser(description=__doc__)
argparser.add_argument(
    'target', metavar='<atom|ebuild>',
    help="atom or ebuild matching a pkg to execute phases from")
argparser.add_argument('phase', nargs='+', help="phases to run")
phase_opts = argparser.add_argument_group("phase options")
phase_opts.add_argument(
    "--no-auto", action='store_true', default=False,
    help="run just the specified phases; "
         "it's up to the invoker to get the order right")
@argparser.bind_main_func
def main(options, out, err):
    target = options.target
    domain = options.domain
    repo = domain.ebuild_repos_raw
Esempio n. 14
0
    'snakeoil.osutils:pjoin,listdir_dirs',
    'snakeoil.sequences:iter_stable_unique',
    'pkgcore.ebuild:processor,triggers',
    'pkgcore.fs:contents,livefs',
    'pkgcore.merge:triggers@merge_triggers',
    'pkgcore.operations:observer',
    'pkgcore.package:mutated',
    'pkgcore.repository:multiplex',
    'pkgcore.restrictions:packages',
    'pkgcore.util.parserestrict:parse_match',
)

argparser = arghparse.ArgumentParser(
    suppress=True,
    description=__doc__,
    parents=(commandline.ArgumentParser(domain=False), ))
subparsers = argparser.add_subparsers(description="general system maintenance")

shared_options = (commandline.ArgumentParser(config=False,
                                             color=False,
                                             debug=False,
                                             quiet=False,
                                             verbose=False,
                                             version=False,
                                             domain=False,
                                             add_help=False), )
shared_options_domain = (commandline.ArgumentParser(config=False,
                                                    color=False,
                                                    debug=False,
                                                    quiet=False,
                                                    verbose=False,
Esempio n. 15
0
from pkgcore.restrictions.util import collect_package_restrictions
from pkgcore.util import commandline, parserestrict
from snakeoil.cli import arghparse
from snakeoil.cli.exceptions import UserException
from snakeoil.formatters import decorate_forced_wrapping
from snakeoil.osutils import abspath, pjoin
from snakeoil.strings import pluralism

from .. import base, const, objects, pipeline, reporters, results
from ..caches import CachedAddon
from ..addons import init_addon
from ..checks import NetworkCheck, init_checks
from ..cli import ConfigArgumentParser
from ..log import logger

pkgcore_config_opts = commandline.ArgumentParser(script=(__file__, __name__))
argparser = ConfigArgumentParser(suppress=True,
                                 description=__doc__,
                                 parents=(pkgcore_config_opts, ),
                                 script=(__file__, __name__))
# TODO: rework pkgcore's config system to allow more lazy loading
argparser.set_defaults(profile_override=pjoin(pkgcore_const.DATA_PATH,
                                              'stubrepo/profiles/default'))
subparsers = argparser.add_subparsers(description="check applets")

reporter_argparser = commandline.ArgumentParser(suppress=True)
reporter_options = reporter_argparser.add_argument_group('reporter options')
reporter_options.add_argument('-R',
                              '--reporter',
                              action='store',
                              default=None,
Esempio n. 16
0
    'errno',
    'functools:partial',
    'glob',
    're',
    'time',
    'shutil',
    'snakeoil.osutils:listdir_dirs,listdir_files,pjoin',
    'snakeoil.sequences:iflatten_instance',
    'pkgcore:fetch',
    'pkgcore.ebuild:atom',
    'pkgcore.package:errors',
    'pkgcore.repository.util:SimpleTree',
    'pkgcore.util:parserestrict',
)

argparser = commandline.ArgumentParser(description=__doc__)
subparsers = argparser.add_subparsers(description='cleaning applets')


@argparser.bind_delayed_default(10)
def _initialize_opts(namespace, attr):
    namespace.restrict = []
    namespace.filters = Filters()


shared_opts = commandline.ArgumentParser(suppress=True)
cleaning_opts = shared_opts.add_argument_group('generic cleaning options')
cleaning_opts.add_argument(nargs='*',
                           dest='targets',
                           metavar='TARGET',
                           help="packages to target for cleaning")
Esempio n. 17
0
from pkgcore.util import commandline

demandload(
    'collections:defaultdict',
    'itertools:groupby,islice',
    'operator:attrgetter,itemgetter',
    'snakeoil.sequences:iflatten_instance,unstable_unique',
    'pkgcore:fetch',
    'pkgcore.package:errors',
    'pkgcore.restrictions:packages',
)

argparser = arghparse.ArgumentParser(
    suppress=True,
    description=__doc__,
    parents=(commandline.ArgumentParser(domain=False), ))
subparsers = argparser.add_subparsers(description="report applets")

pkgsets = subparsers.add_parser("pkgsets",
                                description="pkgset related introspection")
mux = pkgsets.add_mutually_exclusive_group()
mux.add_argument("--all",
                 action='store_true',
                 default=False,
                 help="display info on all pkgsets")
mux.add_argument("pkgsets",
                 nargs="*",
                 metavar="pkgset",
                 default=[],
                 action=commandline.StoreConfigObject,
                 config_type='pkgset',
        try:
            self.dump(result, self.out, self.protocol)
        except TypeError as t:
            raise TypeError(result, str(t))


class BinaryPickleStream(PickleStream):
    """Dump a binary pickle stream (highest protocol).

    For details of the stream, see PickleStream
    """
    priority = -1002
    protocol = -1


argparser = commandline.ArgumentParser(
    config=False, domain=False, color=False, description=__doc__)
argparser.add_argument(
    dest='pickle_file', help='pickled results file')
argparser.add_argument(
    dest='reporter', help='python namespace path reporter to replay it into')
argparser.add_argument(
    '--out', default=None, help='redirect reporters output to a file')


@argparser.bind_final_check
def check_args(parser, namespace):
    if not os.path.isfile(namespace.pickle_file):
        parser.error("pickle file doesn't exist: %r" % namespace.pickle_file)


def replay_stream(stream_handle, reporter, debug=None):
Esempio n. 19
0
try:
    from pkgcore.util import commandline
    from pkgcore.restrictions.boolean import OrRestriction
except ImportError:
    print('Cannot import pkgcore!', file=sys.stderr)
    print(
        'Verify it is properly installed and/or PYTHONPATH is set correctly.',
        file=sys.stderr)
    if '--debug' not in sys.argv:
        print('Add --debug to the commandline for a traceback.',
              file=sys.stderr)
    else:
        raise
    sys.exit(1)

argparser = commandline.ArgumentParser(color=False, version=False)
argparser.add_argument('target', nargs='+', help='target package atoms')
argparser.add_argument('--repo',
                       action=commandline.StoreRepoObject,
                       help='repo to use (default from domain if omitted).')
argparser.add_argument('--print_type',
                       '-t',
                       default="cpvstr",
                       choices=("slotted_atom", "versioned_atom", "cpvstr"),
                       help='''type of atom to output:
                'versioned_atom' : a valid versioned atom,
                'slotted_atom'   : a valid slotted atom,
                'cpvstr'         : the cpv of the package''')


@argparser.bind_final_check
Esempio n. 20
0
demandload(
    'logging',
    'os',
    'sys',
    'textwrap',
    'pkgcore.ebuild:repository',
    'pkgcore.restrictions:packages',
    'pkgcore.restrictions.values:StrExactMatch',
    'pkgcore.repository:multiplex',
    'snakeoil.osutils:abspath',
    'snakeoil.sequences:iflatten_instance',
    'pkgcheck:errors',
)

argparser = commandline.ArgumentParser(domain=False,
                                       color=False,
                                       description=__doc__,
                                       project=__name__.split('.')[0])
# These are all set based on other options, so have no default setting.
argparser.set_defaults(repo_bases=[])
argparser.set_defaults(guessed_target_repo=False)
argparser.set_defaults(guessed_suite=False)
argparser.set_defaults(default_suite=False)
argparser.add_argument('targets',
                       metavar='TARGET',
                       nargs='*',
                       help='optional target atom(s)')

main_options = argparser.add_argument_group('main options')
main_options.add_argument('-r',
                          '--repo',
                          metavar='REPO',
Esempio n. 21
0
from pkgcore.ebuild import inspect_profile
from pkgcore.ebuild import portageq as _portageq
from pkgcore.util import commandline

demandload(
    'collections:defaultdict',
    'itertools:groupby,islice',
    'operator:attrgetter,itemgetter',
    'snakeoil.sequences:iflatten_instance,unstable_unique',
    'pkgcore:fetch',
    'pkgcore.package:errors',
    'pkgcore.restrictions:packages',
)

pkgcore_opts = commandline.ArgumentParser(domain=False, script=(__file__, __name__))
argparser = arghparse.ArgumentParser(
    suppress=True, description=__doc__, parents=(pkgcore_opts,))
subparsers = argparser.add_subparsers(description="report applets")

pkgsets = subparsers.add_parser(
    "pkgsets", description="pkgset related introspection")
mux = pkgsets.add_mutually_exclusive_group()
mux.add_argument(
    "--all", action='store_true', default=False,
    help="display info on all pkgsets")
mux.add_argument(
    "pkgsets", nargs="*", metavar="pkgset", default=[],
    action=commandline.StoreConfigObject,
    config_type='pkgset', store_name=True,
    help="pkgset to inspect")
Esempio n. 22
0
# Copyright: 2005-2011 Brian Harring <[email protected]
# Copyright: 2006 Marien Zwart <*****@*****.**>
# License: BSD/GPL2

"""repository cache clone utility"""

import time

from pkgcore.util import commandline

argparser = commandline.ArgumentParser(domain=False, description=__doc__)
argparser.add_argument(
    "source", config_type='cache', priority=20,
    action=commandline.StoreConfigObject,
    help="source cache to copy data from")
argparser.add_argument(
    "target", config_type='cache', priority=21,
    action=commandline.StoreConfigObject, writable=True,
    help="target cache to update.  Must be writable.")


@argparser.bind_main_func
def main(options, out, err):
    if options.target.readonly:
        argparser.error(
            "can't update cache label '%s', it's marked readonly." %
            (options.target,))

    source, target = options.source, options.target
    if not target.autocommits:
        target.sync_rate = 1000
Esempio n. 23
0
from pkgcore.repository import multiplex
from pkgcore.restrictions import boolean, packages
from pkgcore.restrictions.util import collect_package_restrictions
from pkgcore.util import commandline, parserestrict
from snakeoil.cli import arghparse
from snakeoil.cli.exceptions import UserException
from snakeoil.formatters import decorate_forced_wrapping
from snakeoil.osutils import abspath, pjoin
from snakeoil.strings import pluralism as _pl

from .. import base, const, pipeline, reporters, results
from ..addons import init_addon
from ..checks import init_checks
from ..log import logger

pkgcore_config_opts = commandline.ArgumentParser(script=(__file__, __name__))
argparser = commandline.ArgumentParser(suppress=True,
                                       description=__doc__,
                                       parents=(pkgcore_config_opts, ),
                                       script=(__file__, __name__))
# TODO: rework pkgcore's config system to allow more lazy loading
argparser.set_defaults(profile_override=pjoin(pkgcore_const.DATA_PATH,
                                              'stubrepo/profiles/default'))
subparsers = argparser.add_subparsers(description="check applets")

reporter_argparser = commandline.ArgumentParser(suppress=True)
reporter_options = reporter_argparser.add_argument_group('reporter options')
reporter_options.add_argument('-R',
                              '--reporter',
                              action='store',
                              default=None,