Skip to content

Commit

Permalink
bundle.bbclass: simplify varflag usage
Browse files Browse the repository at this point in the history
By assigning a dictionary as a default value, the code
can be greatly simplified.
This makes the code clearer and more "Pythonic".

Signed-off-by: Jean-Pierre Geslin <jarsop@outlook.com>
(cherry picked from commit ffba61f)
[slightly adapted to apply on kirkstone]
Signed-off-by: Enrico Jörns <ejo@pengutronix.de>
  • Loading branch information
Jarsop authored and ejoerns committed Sep 9, 2024
1 parent 4e0a422 commit 87c1cc2
Showing 1 changed file with 28 additions and 46 deletions.
74 changes: 28 additions & 46 deletions classes/bundle.bbclass
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ python __anonymous() {
d.appendVarFlag('do_unpack', 'vardeps', ' RAUC_BUNDLE_HOOKS')
for slot in (d.getVar('RAUC_BUNDLE_SLOTS') or "").split():
slot_varflags = d.getVar('RAUC_VARFLAGS_SLOTS').split()
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags)
imgtype = slotflags.get('type') if slotflags else None
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags) or {}

imgtype = slotflags.get('type')
if not imgtype:
bb.debug(1, "No [type] given for slot '%s', defaulting to 'image'" % slot)
imgtype = 'image'
Expand All @@ -154,7 +155,7 @@ python __anonymous() {
return

d.appendVarFlag('do_unpack', 'vardeps', ' RAUC_SLOT_%s' % slot)
depends = slotflags.get('depends') if slotflags else None
depends = slotflags.get('depends')
if depends:
d.appendVarFlag('do_unpack', 'depends', ' ' + depends)
continue
Expand Down Expand Up @@ -217,7 +218,7 @@ def write_manifest(d):
manifest.write('\n')

hooks_varflags = d.getVar('RAUC_VARFLAGS_HOOKS').split()
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags)
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags) or {}
have_hookfile = False
if 'file' in hooksflags:
have_hookfile = True
Expand All @@ -231,55 +232,37 @@ def write_manifest(d):

for slot in (d.getVar('RAUC_BUNDLE_SLOTS') or "").split():
slot_varflags = d.getVar('RAUC_VARFLAGS_SLOTS').split()
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags)
if slotflags and 'name' in slotflags:
slotname = slotflags.get('name')
else:
slotname = slot
slotflags = d.getVarFlags('RAUC_SLOT_%s' % slot, expand=slot_varflags) or {}

slotname = slotflags.get('name', slot)
manifest.write('[image.%s]\n' % slotname)
if slotflags and 'type' in slotflags:
imgtype = slotflags.get('type')
else:
imgtype = 'image'

if slotflags and 'fstype' in slotflags:
img_fstype = slotflags.get('fstype')
else:
img_fstype = d.getVar('RAUC_IMAGE_FSTYPE')
imgtype = slotflags.get('type', 'image')

img_fstype = slotflags.get('fstype', d.getVar('RAUC_IMAGE_FSTYPE'))

if imgtype == 'image':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s-%s.%s" % (d.getVar('RAUC_SLOT_%s' % slot), machine, img_fstype)
imgname = imgsource
fallback = "%s-%s.%s" % (d.getVar('RAUC_SLOT_%s' % slot), machine, img_fstype)
imgname = imgsource = slotflags.get('file', fallback)
elif imgtype == 'kernel':
# TODO: Add image type support
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s-%s.bin" % ("zImage", machine)
fallback = "%s-%s.bin" % ("zImage", machine)
imgsource = slotflags.get('file', fallback)
imgname = "%s.%s" % (imgsource, "img")
elif imgtype == 'boot':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = "%s" % ("barebox.img")
imgname = imgsource
imgname = imgsource = slotflags.get('file', 'barebox.img')
elif imgtype == 'file':
if slotflags and 'file' in slotflags:
imgsource = d.getVarFlag('RAUC_SLOT_%s' % slot, 'file')
else:
imgsource = slotflags.get('file')
if not imgsource:
bb.fatal('Unknown file for slot: %s' % slot)
imgname = "%s.%s" % (imgsource, "img")
else:
bb.fatal('Unknown image type: %s' % imgtype)

if slotflags and 'rename' in slotflags:
imgname = d.getVarFlag('RAUC_SLOT_%s' % slot, 'rename')
if slotflags and 'offset' in slotflags:
imgname = slotflags.get('rename', imgname)
if 'offset' in slotflags:
padding = 'seek'
imgoffset = d.getVarFlag('RAUC_SLOT_%s' % slot, 'offset')
imgoffset = slotflags.get('offset')
if imgoffset:
sign, magnitude = imgoffset[:1], imgoffset[1:]
if sign == '+':
Expand All @@ -294,12 +277,12 @@ def write_manifest(d):
# Keep only the image name in case the image is in a $DEPLOY_DIR_IMAGE subdirectory
imgname = PurePath(imgname).name
manifest.write("filename=%s\n" % imgname)
if slotflags and 'hooks' in slotflags:
if 'hooks' in slotflags:
if not have_hookfile:
bb.warn("A hook is defined for slot %s, but RAUC_BUNDLE_HOOKS[file] is not defined" % slot)
manifest.write("hooks=%s\n" % d.getVarFlag('RAUC_SLOT_%s' % slot, 'hooks'))
if slotflags and 'adaptive' in slotflags:
manifest.write("adaptive=%s\n" % d.getVarFlag('RAUC_SLOT_%s' % slot, 'adaptive'))
manifest.write("hooks=%s\n" % slotflags.get('hooks'))
if 'adaptive' in slotflags:
manifest.write("adaptive=%s\n" % slotflags.get('adaptive'))
manifest.write("\n")

bundle_imgpath = "%s/%s" % (bundle_path, imgname)
Expand All @@ -324,8 +307,7 @@ def write_manifest(d):

for meta_section in (d.getVar('RAUC_META_SECTIONS') or "").split():
manifest.write("[meta.%s]\n" % meta_section)
for meta_key in d.getVarFlags('RAUC_META_%s' % meta_section):
meta_value = d.getVarFlag('RAUC_META_%s' % meta_section, meta_key)
for meta_key, meta_value in d.getVarFlags('RAUC_META_%s' % meta_section).items():
manifest.write("%s=%s\n" % (meta_key, meta_value))
manifest.write("\n");

Expand Down Expand Up @@ -361,8 +343,8 @@ python do_configure() {
write_manifest(d)

hooks_varflags = d.getVar('RAUC_VARFLAGS_HOOKS').split()
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags)
if hooksflags and 'file' in hooksflags:
hooksflags = d.getVarFlags('RAUC_BUNDLE_HOOKS', expand=hooks_varflags) or {}
if 'file' in hooksflags:
hf = hooksflags.get('file')
if not os.path.exists(d.expand("${WORKDIR}/%s" % hf)):
bb.error("hook file '%s' does not exist in WORKDIR" % hf)
Expand Down

0 comments on commit 87c1cc2

Please sign in to comment.