Add missing formats to volumeid and image name

Also, when the format uses something unrecognized, die with more
descriptive error message.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-01-06 12:24:01 +01:00
parent 9c1418eb0a
commit 195b13d434
5 changed files with 126 additions and 36 deletions

View File

@ -187,32 +187,34 @@ aborted.
Options Options
------- -------
There a couple common format specifiers available for both the options:
* compose_id
* release_short
* version
* date
* respin
* type
* type_suffix
* label
* label_major_version
* variant
* arch
* disc_type
**image_name_format** [optional] **image_name_format** [optional]
(*str*) -- Python's format string to serve as template for image names (*str*) -- Python's format string to serve as template for image names
This format will be used for all phases generating images. Currently that This format will be used for all phases generating images. Currently that
means ``createiso``, ``live_images`` and ``buildinstall``. means ``createiso``, ``live_images`` and ``buildinstall``.
Available keys are: Available extra keys are:
* compose_id
* variant
* arch
* disc_type
* disc_num * disc_num
* suffix * suffix
* release_short
* version
**image_volid_formats** [optional] **image_volid_formats** [optional]
(*list*) -- A list of format strings for generating volume id. (*list*) -- A list of format strings for generating volume id.
The available keys are: The extra available keys are:
* compose_id
* variant
* arch
* disc_type
* release_short
* version
* base_product_short * base_product_short
* base_product_version * base_product_version

View File

@ -249,6 +249,25 @@ class Compose(kobo.log.LoggingBase):
return return
return open(path, "r").read().strip() return open(path, "r").read().strip()
def get_format_substs(self, **kwargs):
"""Return a dict of basic format substitutions.
Any kwargs will be added as well.
"""
substs = {
'compose_id': self.compose_id,
'release_short': self.ci_base.release.short,
'version': self.ci_base.release.version,
'date': self.compose_date,
'respin': self.compose_respin,
'type': self.compose_type,
'type_suffix': self.compose_type_suffix,
'label': self.compose_label,
'label_major_version': self.compose_label_major_version,
}
substs.update(kwargs)
return substs
def get_image_name(self, arch, variant, disc_type='dvd', def get_image_name(self, arch, variant, disc_type='dvd',
disc_num=1, suffix='.iso', format=None): disc_num=1, suffix='.iso', format=None):
"""Create a filename for image with given parameters. """Create a filename for image with given parameters.
@ -268,22 +287,19 @@ class Compose(kobo.log.LoggingBase):
else: else:
disc_num = "" disc_num = ""
compose_id = self.ci_base[variant.uid].compose_id
if variant.type == "layered-product": if variant.type == "layered-product":
variant_uid = variant.parent.uid variant_uid = variant.parent.uid
else: else:
variant_uid = variant.uid variant_uid = variant.uid
args = { args = self.get_format_substs(variant=variant_uid,
'compose_id': compose_id, arch=arch,
'variant': variant_uid, disc_type=disc_type,
'arch': arch, disc_num=disc_num,
'disc_type': disc_type, suffix=suffix)
'disc_num': disc_num, try:
'suffix': suffix,
'release_short': self.ci_base.release.short,
'version': self.ci_base.release.version,
}
return format % args return format % args
except KeyError as err:
raise RuntimeError('Failed to create image name: unknown format element: %s' % err.message)
def can_fail(self, variant, arch, deliverable): def can_fail(self, variant, arch, deliverable):
"""Figure out if deliverable can fail on variant.arch. """Figure out if deliverable can fail on variant.arch.

View File

@ -350,16 +350,16 @@ def get_volid(compose, arch, variant=None, escape_spaces=False, disc_type=False)
for i in all_products: for i in all_products:
if not variant_uid and "%(variant)s" in i: if not variant_uid and "%(variant)s" in i:
continue continue
volid = i % { try:
'compose_id': compose.compose_id, volid = i % compose.get_format_substs(variant=variant_uid,
'variant': variant_uid, release_short=release_short,
'arch': arch, version=release_version,
'disc_type': disc_type or '', arch=arch,
'release_short': release_short, disc_type=disc_type or '',
'version': release_version, base_product_short=base_product_short,
'base_product_short': base_product_short, base_product_version=base_product_version)
'base_product_version': base_product_version, except KeyError as err:
} raise RuntimeError('Failed to create volume id: unknown format element: %s' % err.message)
volid = _apply_substitutions(compose, volid) volid = _apply_substitutions(compose, volid)
if len(volid) <= 32: if len(volid) <= 32:
break break

View File

@ -40,6 +40,34 @@ class ComposeTestCase(unittest.TestCase):
self.assertFalse(compose.can_fail(None, 'x86_64', 'live')) self.assertFalse(compose.can_fail(None, 'x86_64', 'live'))
self.assertTrue(compose.can_fail(None, 'i386', 'live')) self.assertTrue(compose.can_fail(None, 'i386', 'live'))
@mock.patch('pungi.compose.ComposeInfo')
def test_get_image_name(self, ci):
conf = {}
variant = mock.Mock(uid='Server', type='variant')
ci.return_value.compose.respin = 2
ci.return_value.compose.id = 'compose_id'
ci.return_value.compose.date = '20160107'
ci.return_value.compose.type = 'nightly'
ci.return_value.compose.type_suffix = '.n'
ci.return_value.compose.label = 'RC-1.0'
ci.return_value.compose.label_major_version = '1'
ci.return_value.release.version = '3.0'
ci.return_value.release.short = 'rel_short'
compose = Compose(conf, self.tmp_dir)
keys = ['arch', 'compose_id', 'date', 'disc_num', 'disc_type',
'label', 'label_major_version', 'release_short', 'respin',
'suffix', 'type', 'type_suffix', 'variant', 'version']
format = '-'.join(['%(' + k + ')s' for k in keys])
name = compose.get_image_name('x86_64', variant, format=format,
disc_num=7, disc_type='live', suffix='.iso')
self.assertEqual(name, '-'.join(['x86_64', 'compose_id', '20160107', '7', 'live',
'RC-1.0', '1', 'rel_short', '2', '.iso', 'nightly',
'.n', 'Server', '3.0']))
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -5,9 +5,12 @@ import mock
import os import os
import sys import sys
import unittest import unittest
import tempfile
import shutil
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pungi import compose
from pungi import util from pungi import util
@ -80,5 +83,46 @@ class TestGetVariantData(unittest.TestCase):
self.assertItemsEqual(result, []) self.assertItemsEqual(result, [])
class TestVolumeIdGenerator(unittest.TestCase):
def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tmp_dir)
@mock.patch('pungi.compose.ComposeInfo')
def test_get_volid(self, ci):
all_keys = [
(['arch', 'compose_id', 'date', 'disc_type'], 'x86_64-compose_id-20160107-'),
(['label', 'label_major_version', 'release_short', 'respin'], 'RC-1.0-1-rel_short2-2'),
(['type', 'type_suffix', 'variant', 'version'], 'nightly-.n-Server-6.0')
]
for keys, expected in all_keys:
format = '-'.join(['%(' + k + ')s' for k in keys])
conf = {
'release_short': 'rel_short2',
'release_version': '6.0',
'release_is_layered': False,
'image_volid_formats': [format]
}
variant = mock.Mock(uid='Server', type='variant')
ci.return_value.compose.respin = 2
ci.return_value.compose.id = 'compose_id'
ci.return_value.compose.date = '20160107'
ci.return_value.compose.type = 'nightly'
ci.return_value.compose.type_suffix = '.n'
ci.return_value.compose.label = 'RC-1.0'
ci.return_value.compose.label_major_version = '1'
ci.return_value.release.version = '3.0'
ci.return_value.release.short = 'rel_short'
c = compose.Compose(conf, self.tmp_dir)
volid = util.get_volid(c, 'x86_64', variant, escape_spaces=False, disc_type=False)
self.assertEqual(volid, expected)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()