def test_tty_detection(self): 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'), (['--nocolor'], 'PlainTextFormatter', 'PlainTextFormatter'), ]: master, out = self._get_pty_pair() err, err_getvalue = _stream_and_getvalue() try: commandline.main({None: (commandline.OptionParser, main)}, args, out, err) except commandline.MySystemExit as e: # Important, without this master.read() blocks. out.close() self.assertEqual(None, e.args[0]) # There can be an xterm title update after this. out_name = master.read() 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')
def test_tty_detection(self): 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'), (['--nocolor'], 'PlainTextFormatter', 'PlainTextFormatter'), ]: master, out = self._get_pty_pair() err, err_getvalue = _stream_and_getvalue() try: commandline.main( {None: (commandline.OptionParser, main)}, args, out, err) except commandline.MySystemExit as e: # Important, without this master.read() blocks. out.close() self.assertEqual(None, e.args[0]) # There can be an xterm title update after this. out_name = master.read() 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')
def assertMain(self, status, outtext, errtext, *args, **kwargs): out, out_getvalue = _stream_and_getvalue() err, err_getvalue = _stream_and_getvalue() try: commandline.main(outfile=out, errfile=err, *args, **kwargs) except commandline.MySystemExit, e: self.assertEqual(errtext, err_getvalue()) self.assertEqual(outtext, out_getvalue()) self.assertEqual(status, e.args[0], msg="expected status %r, got %r" % (status, e.args[0]))
def assertMain(self, status, outtext, errtext, subcmds, *args, **kwargs): out, out_getvalue = _stream_and_getvalue() err, err_getvalue = _stream_and_getvalue() try: commandline.main(subcmds, outfile=out, errfile=err, *args, **kwargs) except SystemExit as e: assert errtext == err_getvalue() assert outtext == out_getvalue() assert status == e.args[0], f"expected status {status!r}, got {e.args[0]!r}" else: self.fail('no exception raised')
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')
def assertMain(self, status, outtext, errtext, subcmds, *args, **kwargs): out, out_getvalue = _stream_and_getvalue() err, err_getvalue = _stream_and_getvalue() try: commandline.main(subcmds, outfile=out, errfile=err, *args, **kwargs) except SystemExit as e: self.assertEqual(errtext, err_getvalue()) self.assertEqual(outtext, out_getvalue()) self.assertEqual( status, e.args[0], msg="expected status %r, got %r" % (status, e.args[0])) else: self.fail('no exception raised')
def assertMain(self, status, outtext, errtext, *args, **kwargs): out, out_getvalue = _stream_and_getvalue() err, err_getvalue = _stream_and_getvalue() try: commandline.main(outfile=out, errfile=err, *args, **kwargs) except commandline.MySystemExit as e: self.assertEqual(errtext, err_getvalue()) self.assertEqual(outtext, out_getvalue()) self.assertEqual( status, e.args[0], msg="expected status %r, got %r" % (status, e.args[0])) else: self.fail('no exception raised')
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')
def assertMain(self, status, outtext, errtext, subcmds, *args, **kwargs): out, out_getvalue = _stream_and_getvalue() err, err_getvalue = _stream_and_getvalue() try: commandline.main(subcmds, outfile=out, errfile=err, *args, **kwargs) except SystemExit as e: assert errtext == err_getvalue() assert outtext == out_getvalue() assert status == e.args[ 0], f"expected status {status!r}, got {e.args[0]!r}" else: self.fail('no exception raised')
def main(script_name): try: from pkgcore.util import commandline script_module = '.'.join( os.path.realpath(__file__).split('/')[-3:-1] + [script_name.replace('-', '_')]) script = import_module(script_module) except ImportError as e: sys.stderr.write('Failed importing: %s!\n' % str(e)) sys.stderr.write( 'Verify that snakeoil and pkgcore are properly installed ' 'and/or PYTHONPATH is set correctly for python %s.\n' % ('.'.join(map(str, sys.version_info[:3])),)) if '--debug' in sys.argv: raise sys.stderr.write('Add --debug to the commandline for a traceback.\n') sys.exit(1) argparser = getattr(script, 'argparser', None) commandline.main(argparser)
def main(script_name): try: from pkgcore.util import commandline script_module = '.'.join( os.path.realpath(__file__).split('/')[-3:-1] + [script_name.replace('-', '_')]) script = import_module(script_module) except ImportError as e: sys.stderr.write('Failed importing: %s!\n' % str(e)) sys.stderr.write( 'Verify that snakeoil and pkgcore are properly installed ' 'and/or PYTHONPATH is set correctly for python %s.\n' % ('.'.join(map(str, sys.version_info[:3])), )) if '--debug' in sys.argv: raise sys.stderr.write('Add --debug to the commandline for a traceback.\n') sys.exit(1) argparser = getattr(script, 'argparser', None) commandline.main(argparser)
def main(script_name=None): if script_name is None: script_name = os.path.basename(sys.argv[0]) try: from pkgcore.util import commandline script = import_module( 'pkgcore.scripts.%s' % script_name.replace("-", "_")) except ImportError as e: sys.stderr.write(str(e) + '!\n') sys.stderr.write( 'Verify that snakeoil and pkgcore are properly installed ' 'and/or PYTHONPATH is set correctly for python %s.\n' % (".".join(map(str, sys.version_info[:3])),)) if '--debug' in sys.argv: raise sys.stderr.write('Add --debug to the commandline for a traceback.\n') sys.exit(1) subcommands = getattr(script, 'argparser', None) commandline.main(subcommands)
def test_tty_detection(self): 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'), (['--nocolor'], 'PlainTextFormatter', 'PlainTextFormatter'), ): master, out = self._get_pty_pair() err, err_getvalue = _stream_and_getvalue() try: commandline.main({None: (commandline.OptionParser, main)}, args, out, err) except commandline.MySystemExit 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 = os.read(master.fileno(), 128).decode() 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')
def test_tty_detection(self): 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'), (['--nocolor'], 'PlainTextFormatter', 'PlainTextFormatter'), ): master, out = self._get_pty_pair() err, err_getvalue = _stream_and_getvalue() try: commandline.main( {None: (commandline.OptionParser, main)}, args, out, err) except commandline.MySystemExit 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 = os.read(master.fileno(), 128).decode() 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')
*commandline.convert_to_restrict(namespace.target)) namespace.outputter = attrgetter(namespace.print_type) @argparser.bind_main_func def main(options, out, err): repo = options.repo for built in options.vdb.itermatch(options.restrict): current = repo.match(built.versioned_atom) if current: current = current[0] oldflags = built.iuse & built.use newflags = current.iuse & built.use if (newflags != oldflags) or (current.iuse ^ built.iuse): changed_flags = (oldflags ^ newflags) | (current.iuse ^ built.iuse) if options.quiet: out.write(options.outputter(current)) else: out.write("for package %s, %d flags have changed:\n\t%s" % (current.cpvstr, len(changed_flags), ' '.join(changed_flags))) else: if options.verbose: out.write("%s is the same as it was before" % current.cpvstr) if __name__ == '__main__': commandline.main(argparser)
def check_values(self, values, args): values, args = commandline.OptionParser.check_values( self, values, args) if args: self.error("This script takes no arguments") # Get repo(s) to operate on. if values.repo: repos = (values.repo,) else: repos = values.config.get_default('domain').repos values.repos = get_virtual_repos(get_raw_repos(repos), False) return values, () def main(options, out, err): for repo in options.repos: out.write("Repo ID: %s" % repo.repo_id) location = getattr(repo, "location", None) if location: out.write("Repo location: %s" % location) else: out.write("Repo has no on-disk location") out.write("%d packages" % len(repo.versions)) out.write("%d categories" % len(repo.packages)) out.write() if __name__ == '__main__': commandline.main({None: (OptionParser, main)})
namespace.repo = namespace.domain.ebuild_repos namespace.restrict = OrRestriction( *commandline.convert_to_restrict(namespace.target)) def getter(pkg): return (pkg.key, getattr(pkg, "maintainers", None)) @argparser.bind_main_func def main(options, out, err): for t, pkgs in itertools.groupby( options.repo.itermatch(options.restrict, sorter=sorted), getter): out.write(t[0]) out.first_prefix = " " for pkg in pkgs: out.write('%s::%s' % (pkg.cpvstr, pkg.repo.repo_id)) out.first_prefix = "" out.write() item = 'maintainer' values = t[1] if values: out.write( "%s%s: %s" % (item.title(), 's'[len(values) == 1:], ', '.join((unicode(x) for x in values)))) out.write() out.write() if __name__ == '__main__': commandline.main(argparser)
self, values, args) values.repo = values.config.get_default('domain').repos[1] values.restrict = OrRestriction(*commandline.convert_to_restrict(args)) return values, () def getter(pkg): return (pkg.key, getattr(pkg, "maintainers", None), getattr(pkg, "herds", None)) def main(options, out, err): for t, pkgs in itertools.groupby( options.repo.itermatch(options.restrict, sorter=sorted), getter): out.write(t[0]) out.first_prefix = " " for pkg in pkgs: out.write(pkg.cpvstr) out.first_prefix = "" out.write() for item, values in zip(("maintainer", "herd"), t[1:]): if values: out.write("%s(s): %s" % (item.title(), ', '.join( (unicode(x) for x in values)))) out.write() out.write() if __name__ == '__main__': commandline.main({None: (OptionParser, main)})