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