#3406 Add a utility function to watch builds
Merged 6 days ago by tkopecek. Opened 2 months ago by tkopecek.
tkopecek/koji issue2981  into  master

file modified
+8 -34
@@ -45,6 +45,7 @@ 

      print_task_recurse,

      unique_path,

      warn,

+     wait_repo,

      watch_logs,

      watch_tasks,

      truncate_string
@@ -7044,8 +7045,6 @@ 

                             "value only")

      (suboptions, args) = parser.parse_args(args)

  

-     start = time.time()

- 

      builds = [koji.parse_NVR(build) for build in suboptions.builds]

      if len(args) < 1:

          parser.error("Please specify a tag name")
@@ -7087,38 +7086,13 @@ 

                  warn("nvr %s is not current in tag %s\n  latest build in %s is %s" %

                       (expected_nvr, tag, tag, present_nvr))

  

-     last_repo = None

-     repo = session.getRepo(tag_id)

- 

-     while True:

-         if builds and repo and repo != last_repo:

-             if koji.util.checkForBuilds(session, tag_id, builds, repo['create_event'],

-                                         latest=True):

-                 if not suboptions.quiet:

-                     print("Successfully waited %s for %s to appear in the %s repo" %

-                           (koji.util.duration(start), koji.util.printList(suboptions.builds), tag))

-                 return

- 

-         if (time.time() - start) >= (suboptions.timeout * 60.0):

-             if not suboptions.quiet:

-                 if builds:

-                     error("Unsuccessfully waited %s for %s to appear in the %s repo" %

-                           (koji.util.duration(start), koji.util.printList(suboptions.builds), tag))

-                 else:

-                     error("Unsuccessfully waited %s for a new %s repo" %

-                           (koji.util.duration(start), tag))

-             error()

- 

-         time.sleep(options.poll_interval)

-         last_repo = repo

-         repo = session.getRepo(tag_id)

- 

-         if not builds:

-             if repo != last_repo:

-                 if not suboptions.quiet:

-                     print("Successfully waited %s for a new %s repo" %

-                           (koji.util.duration(start), tag))

-                 return

+     success, msg = wait_repo(session, tag_id, builds,

+                              poll_interval=options.poll_interval, timeout=suboptions.timeout)

+     if success:

+         if not suboptions.quiet:

+             print(msg)

+     else:

+         error('' if suboptions.quiet else msg)

  

  

  def handle_regen_repo(options, session, args):

file modified
+45
@@ -831,6 +831,51 @@ 

      return tasklist

  

  

+ def wait_repo(session, tag_id, builds, poll_interval=5, timeout=120):

+     """Watch for given builds to appear in given tag. If no builds are given,

+     watch for new repo for given tag.

+ 

+     :param session: Koji session object

+     :param int tag_id: Tag id

+     :param [dict] builds: List of builds as NVR dicts

+     :param int poll_interval: Poll interval in seconds

+     :param int timeout: Watch timeout in minutes

+     :returns bool, msg: False if timeouted

+     """

+     last_repo = None

+     repo = session.getRepo(tag_id)

+ 

+     # String representations for logs and exceptions

+     builds_str = koji.util.printList([koji.buildLabel(build) for build in builds])

+     tag_info = session.getTag(tag_id, strict=True)

+     tag_name = tag_info['name']

+ 

+     start = time.time()

+     while True:

+         if builds and repo and repo != last_repo:

+             if koji.util.checkForBuilds(session, tag_id, builds,

+                                         repo['create_event'], latest=True):

+                 return (True, "Successfully waited %s for %s to appear in the %s repo" %

+                         (koji.util.duration(start), builds_str, tag_name))

+ 

+         if (time.time() - start >= timeout * 60.0):

+             if builds:

+                 return (False, "Unsuccessfully waited %s for %s to appear in the %s repo" %

+                         (koji.util.duration(start), builds_str, tag_name))

+             else:

+                 return (False, "Unsuccessfully waited %s for a new %s repo" %

+                         (koji.util.duration(start), tag_name))

+ 

+         time.sleep(poll_interval)

+         last_repo = repo

+         repo = session.getRepo(tag_id)

+ 

+         if not builds:

+             if repo != last_repo:

+                 return (True, "Successfully waited %s for a new %s repo" %

+                         (koji.util.duration(start), tag_name))

+ 

+ 

  def format_inheritance_flags(parent):

      """Return a human readable string of inheritance flags"""

      flags = ''

@@ -36,7 +36,7 @@ 

  

          self.options = mock.MagicMock()

          self.options.quiet = True

-         self.options.poll_interval = 1  # second

+         self.options.poll_interval = 0.0001  # keep it fast

          self.options.weburl = 'https://localhost.local'

  

          self.session = mock.MagicMock()

@@ -0,0 +1,72 @@ 

+ from __future__ import absolute_import

+ import mock

+ import os

+ import rpm

+ import unittest

+ 

+ import koji

+ 

+ class TestBuildLabel(unittest.TestCase):

+     def test_buildLabel(self):

+         """Test the buildLabel method"""

+ 

+         self.assertRaises(AttributeError, koji.buildLabel, None)

+         self.assertRaises(AttributeError, koji.buildLabel, 1)

+         self.assertRaises(AttributeError, koji.buildLabel, [])

+ 

+         input = {}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "None-None-None")

+ 

+         input = {"name": "foo"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "foo-None-None")

+ 

+         input = {"version": "1.0.2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "None-1.0.2-None")

+ 

+         input = {"release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "None-None-2")

+ 

+         input = {"name": "foo", "version": "1.0.2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "foo-1.0.2-None")

+ 

+         input = {"name": "foo", "release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "foo-None-2")

+ 

+         input = {"version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "None-1.0.2-2")

+ 

+         input = {"name": "foo", "version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "foo-1.0.2-2")

+ 

+         input = {"package_name": "bar", "version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "bar-1.0.2-2")

+ 

+         input = {

+             "package_name": "bar",

+             "name": "foo",

+             "version": "1.0.2",

+             "release": "2"

+         }

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "bar-1.0.2-2")

+ 

+         input = {"epoch": 7, "name": "foo", "version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input)

+         self.assertEqual(ret, "foo-1.0.2-2")

+ 

+         input = {"epoch": 7, "name": "foo", "version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input, True)

+         self.assertEqual(ret, "7:foo-1.0.2-2")

+ 

+         input = {"name": "foo", "version": "1.0.2", "release": "2"}

+         ret = koji.buildLabel(input, True)

+         self.assertEqual(ret, "foo-1.0.2-2")

3 new commits added

  • exception-free watch_builds
  • Add utility function to watch builds
  • Add unit test for buildLabel
2 months ago

3 new commits added

  • exception-free watch_builds
  • Add utility function to watch builds
  • Add unit test for buildLabel
2 months ago

Heh. That's very Go, but hey... it works!

rebased onto 2b85a578529311e7c712dff16c8c3a7f959de33e

a month ago

pretty please pagure-ci rebuild

13 days ago

This is a direct relocation of the logic that was formerly in anon_handle_wait_repo(), the only changes are those necessary for it to work as a standalone function. I don't see any obvious issues with the relocation.

I'm not sure the function name is correct. This is distinctly about watching for builds to appear in a repo. In fact, apart from a bit of argument juggling, this function essentially IS the wait-repo command.

So, I've renamed it to wait_repo, watch_repo looks a bit inconsistent with CLI, but it is also an option.

rebased onto c1e2cb4

12 days ago

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

12 days ago

Metadata Update from @jobrauer:
- Pull-request tagged with: testing-done

6 days ago

Commit fad0eed fixes this pull-request

Pull-Request has been merged by tkopecek

6 days ago