From 9d3cd4ca67cdbafb21dccb887833058f37e2e26c Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Nov 28 2018 05:26:10 +0000 Subject: [PATCH 1/4] cli: add a param in watch_tasks to override KeyboardInterrupt output --- diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index 162d6a1..3d4f2a2 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -276,7 +276,7 @@ def display_task_results(tasks): print('%s has not completed' % task_label) -def watch_tasks(session, tasklist, quiet=False, poll_interval=60): +def watch_tasks(session, tasklist, quiet=False, poll_interval=60, ki_handler=None): if not tasklist: return if not quiet: @@ -319,12 +319,15 @@ def watch_tasks(session, tasklist, quiet=False, poll_interval=60): except KeyboardInterrupt: if tasks and not quiet: progname = os.path.basename(sys.argv[0]) or 'koji' - tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) - for t in tasks.values() if not t.is_done()] - print( \ + if ki_handler is None: + def ki_handler(progname, tasks): + tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) + for t in tasks.values() if not t.is_done()] + print( """Tasks still running. You can continue to watch with the '%s watch-task' command. Running Tasks: %s""" % (progname, '\n'.join(tlist))) + ki_handler(progname, tasks) raise return rv diff --git a/tests/test_cli/test_watch_tasks.py b/tests/test_cli/test_watch_tasks.py index fafcc4d..c5bb44e 100644 --- a/tests/test_cli/test_watch_tasks.py +++ b/tests/test_cli/test_watch_tasks.py @@ -121,6 +121,31 @@ Running Tasks: ''' % (os.path.basename(sys.argv[0]) or 'koji')) self.assertMultiLineEqual(stdout.getvalue(), expected) + @mock.patch('time.sleep') + @mock.patch('sys.stdout', new_callable=six.StringIO) + def test_watch_tasks_with_keyboardinterrupt_handler(self, stdout, sleep): + """Raise KeyboardInterrupt inner watch_tasks with a ki_handler""" + cfile = os.path.dirname(__file__) + '/data/calls/watchtasks2.json' + with open(cfile) as fp: + cdata = json.load(fp) + self.session.load_calls(cdata) + sleep.side_effect = [None] * 10 + [KeyboardInterrupt] + + def customized_handler(progname, tasks): + print('some output') + + with self.assertRaises(KeyboardInterrupt): + # watch_tasks catches and re-raises it to display a message + watch_tasks(self.session, [1208], quiet=False, poll_interval=5, + ki_handler=customized_handler) + expected = ('''Watching tasks (this may be safely interrupted)... +1208 build (f24, /users/mikem/fake.git:master): free +1208 build (f24, /users/mikem/fake.git:master): free -> open (builder-01) + 1209 buildSRPMFromSCM (/users/mikem/fake.git:master): free + 1209 buildSRPMFromSCM (/users/mikem/fake.git:master): free -> open (builder-01) +some output +''') + self.assertMultiLineEqual(stdout.getvalue(), expected) if __name__ == '__main__': unittest.main() From f1fd867be9ae866d5d72e30a36c68c112b344618 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Nov 28 2018 05:26:10 +0000 Subject: [PATCH 2/4] move quiet option into ki_handler --- diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index 3d4f2a2..ce04706 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -317,17 +317,18 @@ def watch_tasks(session, tasklist, quiet=False, poll_interval=60, ki_handler=Non sys.stdout.flush() time.sleep(poll_interval) except KeyboardInterrupt: - if tasks and not quiet: + if tasks: progname = os.path.basename(sys.argv[0]) or 'koji' if ki_handler is None: - def ki_handler(progname, tasks): - tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) - for t in tasks.values() if not t.is_done()] - print( + def ki_handler(progname, tasks, quiet): + if not quiet: + tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) + for t in tasks.values() if not t.is_done()] + print( """Tasks still running. You can continue to watch with the '%s watch-task' command. Running Tasks: %s""" % (progname, '\n'.join(tlist))) - ki_handler(progname, tasks) + ki_handler(progname, tasks, quiet) raise return rv diff --git a/tests/test_cli/test_watch_tasks.py b/tests/test_cli/test_watch_tasks.py index c5bb44e..ee031b3 100644 --- a/tests/test_cli/test_watch_tasks.py +++ b/tests/test_cli/test_watch_tasks.py @@ -131,7 +131,7 @@ Running Tasks: self.session.load_calls(cdata) sleep.side_effect = [None] * 10 + [KeyboardInterrupt] - def customized_handler(progname, tasks): + def customized_handler(progname, tasks, quiet): print('some output') with self.assertRaises(KeyboardInterrupt): From 0c2b0949439e7feca7c09569898dea63955e0ef7 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Nov 28 2018 05:26:10 +0000 Subject: [PATCH 3/4] move inner function out of except clause --- diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index ce04706..1f677d2 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -281,6 +281,15 @@ def watch_tasks(session, tasklist, quiet=False, poll_interval=60, ki_handler=Non return if not quiet: print("Watching tasks (this may be safely interrupted)...") + if ki_handler is None: + def ki_handler(progname, tasks, quiet): + if not quiet: + tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) + for t in tasks.values() if not t.is_done()] + print( +"""Tasks still running. You can continue to watch with the '%s watch-task' command. +Running Tasks: +%s""" % (progname, '\n'.join(tlist))) sys.stdout.flush() rv = 0 try: @@ -319,15 +328,6 @@ def watch_tasks(session, tasklist, quiet=False, poll_interval=60, ki_handler=Non except KeyboardInterrupt: if tasks: progname = os.path.basename(sys.argv[0]) or 'koji' - if ki_handler is None: - def ki_handler(progname, tasks, quiet): - if not quiet: - tlist = ['%s: %s' % (t.str(), t.display_state(t.info)) - for t in tasks.values() if not t.is_done()] - print( -"""Tasks still running. You can continue to watch with the '%s watch-task' command. -Running Tasks: -%s""" % (progname, '\n'.join(tlist))) ki_handler(progname, tasks, quiet) raise return rv From 8923181c38b61966134aad4f2774246c3081c9d7 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Nov 28 2018 05:26:10 +0000 Subject: [PATCH 4/4] whitespace --- diff --git a/tests/test_cli/test_watch_tasks.py b/tests/test_cli/test_watch_tasks.py index ee031b3..e95407b 100644 --- a/tests/test_cli/test_watch_tasks.py +++ b/tests/test_cli/test_watch_tasks.py @@ -129,8 +129,8 @@ Running Tasks: with open(cfile) as fp: cdata = json.load(fp) self.session.load_calls(cdata) - sleep.side_effect = [None] * 10 + [KeyboardInterrupt] - + sleep.side_effect = [None] * 10 + [KeyboardInterrupt] + def customized_handler(progname, tasks, quiet): print('some output') @@ -147,5 +147,6 @@ some output ''') self.assertMultiLineEqual(stdout.getvalue(), expected) + if __name__ == '__main__': unittest.main()