From cbcebe90e1cde91bf8af97377667113c392e41b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Wed, 30 May 2018 16:09:31 +0200 Subject: [PATCH] Allow extracting koji event from another compose MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When multiple composes are chained, they should reuse the same event. However it is tricky as the value would have to be passed by hand. This patch makes it possible to read the value from another compose (the first one in the chain). JIRA: COMPOSE-2571 Signed-off-by: Lubomír Sedlář --- bin/pungi-koji | 7 ++++--- pungi/util.py | 21 ++++++++++++++++++++- tests/test_util.py | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/bin/pungi-koji b/bin/pungi-koji index 68dfc44b..42dcb424 100755 --- a/bin/pungi-koji +++ b/bin/pungi-koji @@ -23,7 +23,7 @@ if here != '/usr/bin': sys.path[0] = os.path.dirname(here) from pungi.phases import PHASES_NAMES -from pungi import get_full_version +from pungi import get_full_version, util # force C locales @@ -130,8 +130,9 @@ def main(): parser.add_argument( "--koji-event", metavar="ID", - type=int, - help="specify a koji event for populating package set", + type=util.parse_koji_event, + help="specify a koji event for populating package set, either as event ID " + "or a path to a compose from which to reuse the event", ) parser.add_argument( "--version", diff --git a/pungi/util.py b/pungi/util.py index ce6d7ae7..0bcd6d60 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -13,8 +13,9 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see . - +import argparse import fnmatch +import json import subprocess import os import shutil @@ -836,3 +837,21 @@ def get_tz_offset(): hours = offset / 3600 minutes = (offset / 60) % 60 return "%+03d:%02d" % (hours, minutes) + + +def parse_koji_event(event): + """Process event specification. If event looks like a number, it will be + used as is. If a string is given, it will be interpreted as a path to the + topdir of another compose, from which an even it will be extracted. + """ + try: + return int(event) + except ValueError: + pass + try: + with open(os.path.join(event, "work/global/koji-event")) as f: + return json.load(f)["id"] + except (IOError, OSError, KeyError): + raise argparse.ArgumentTypeError( + "%s is not a number or path to compose with valid Koji event" % event + ) diff --git a/tests/test_util.py b/tests/test_util.py index c8a72074..3fa34804 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,6 +1,7 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +import argparse import mock import os import sys @@ -685,5 +686,23 @@ class TestTZOffset(unittest.TestCase): self.assertEqual(util.get_tz_offset(), "+00:00") +class TestParseKojiEvent(PungiTestCase): + + def test_number(self): + self.assertEqual(util.parse_koji_event("1234"), 1234) + + def test_correct_path(self): + touch( + os.path.join(self.topdir, 'work/global/koji-event'), + '{"id": 19769058, "ts": 1527641311.22855}' + ) + + self.assertEqual(util.parse_koji_event(self.topdir), 19769058) + + def test_bad_path(self): + with self.assertRaises(argparse.ArgumentTypeError): + util.parse_koji_event(self.topdir) + + if __name__ == "__main__": unittest.main()