#446 Custom handler for koji watch_tasks
Merged 4 years ago by onosek. Opened 4 years ago by onosek.
onosek/rpkg custom_watch_tasks  into  master

file modified
+5 -1
@@ -1667,7 +1667,11 @@ 

          if self.args.dry_run:

              self.log.info('DRY-RUN: Watch tasks: %s', task_ids)

          else:

-             return koji_cli.lib.watch_tasks(self.cmd.kojisession, task_ids)

+             return koji_cli.lib.watch_tasks(

+                 self.cmd.kojisession,

+                 task_ids,

+                 ki_handler=utils.make_koji_watch_tasks_handler(self.cmd.build_client)

+             )

  

      def extract_greenwave_url(self):

          greenwave_url = None

file modified
+21
@@ -128,3 +128,24 @@ 

              'The "{0}" optional argument is reserved to built-in arguments'.format(key))

  

      return (key, value)

+ 

+ 

+ def make_koji_watch_tasks_handler(progname):

+     def koji_watch_tasks_handler(_, tasks, quiet):

+         """

+         Displays information about running tasks and says how to watch them.

+         Unlike the default version at koji library it overrides progname

+         to show brew, koji or other build client.

+         """

+         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)))

+ 

+     # Save reference of the handler during first time use.

+     # It guarantees that the same object is always returned (it allows unittest to pass).

+     global handler_reference

+     if 'handler_reference' not in globals():

+         handler_reference = koji_watch_tasks_handler

+     return handler_reference

file modified
+22 -17
@@ -3235,23 +3235,28 @@ 

          mock_build_api = None

  

          with patch('koji_cli.lib.watch_tasks') as watch_tasks:

-             with patch('sys.argv', new=cli_cmd):

-                 cli = self.new_cli(cfg=config_file)

-                 if sub_command == 'build':

-                     mock_build_api = session.build

-                     cli.build()

-                 elif sub_command == 'scratch-build':

-                     mock_build_api = session.build

-                     cli.scratch_build()

-                 elif sub_command == 'chain-build':

-                     mock_build_api = session.chainBuild

-                     cli.chainbuild()

- 

-             if '--nowait' in cli_cmd:

-                 watch_tasks.assert_not_called()

-             else:

-                 watch_tasks.assert_called_once_with(

-                     session, [mock_build_api.return_value])

+             with patch('pyrpkg.utils.make_koji_watch_tasks_handler') as mock_ki:

+                 with patch('sys.argv', new=cli_cmd):

+                     cli = self.new_cli(cfg=config_file)

+                     if sub_command == 'build':

+                         mock_build_api = session.build

+                         cli.build()

+                     elif sub_command == 'scratch-build':

+                         mock_build_api = session.build

+                         cli.scratch_build()

+                     elif sub_command == 'chain-build':

+                         mock_build_api = session.chainBuild

+                         cli.chainbuild()

+ 

+                 if '--nowait' in cli_cmd:

+                     watch_tasks.assert_not_called()

+                 else:

+                     watch_tasks.assert_called_once_with(

+                         session,

+                         [mock_build_api.return_value],

+                         ki_handler=mock_ki.return_value

+                     )

+                     self.assertEqual(mock_ki.call_args, (("koji",),))

  

          mock_build_api.assert_called_once()

  

Output text during rhpkg/fedpkg build process states that there is a 'watch_task' subcommand. When 'koji_cli' library is imported in rhpkg/fedpkg tool, it shows that command is named 'rhpkg/fedpkg watch_task' instead of 'brew/koji watch_task'. Custom handler replaces the internal one inside koji_cli library.
Additional fix in rhpkg is needed after this change is released.

Relates: rhbz#1570921
Relates: COMPOSE-2809

Signed-off-by: Ondrej Nosek onosek@redhat.com

I don't like hardcoding downstream name here, since it's not going to work with stage variants. How about something like:

def make_ki_handler(progname):
    def ki_handler(_, tasks, quiet):
       if not quiet:
            ...
    return ki_handler

and then use make_ki_handler(self.build_client) in individual downstreams?

rebased onto f77a568cb1dc5653262276d26b386415b5e779e9

4 years ago

Thanks for the suggestion.

rebased onto 910ed50

4 years ago

Pull-Request has been merged by onosek

4 years ago