Allow extracting koji event from another compose

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ář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2018-05-30 16:09:31 +02:00
parent ec96757707
commit cbcebe90e1
3 changed files with 43 additions and 4 deletions

View File

@ -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",

View File

@ -13,8 +13,9 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://gnu.org/licenses/>.
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
)

View File

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