Remove need for shell wrap complexity

This commit is contained in:
Ian Gulliver
2016-05-10 19:02:14 +00:00
parent a830264910
commit b1f01ea143

View File

@@ -4,7 +4,7 @@ import os
import re import re
import string import string
import subprocess import subprocess
import sys import tempfile
class GrubUpdater(object): class GrubUpdater(object):
@@ -30,23 +30,26 @@ class GrubUpdater(object):
return match.group('volume_id').decode('ascii') return match.group('volume_id').decode('ascii')
def Update(self): def Update(self):
current = os.readlink(os.path.join(self._image_dir, 'current')) grub_dir = os.path.join(self._boot_dir, 'grub')
sys.stdout.write(""" with tempfile.NamedTemporaryFile(dir=grub_dir, delete=False) as fh:
current = os.readlink(os.path.join(self._image_dir, 'current'))
fh.write("""
set timeout=5 set timeout=5
set default=%(default_image_filename)s set default=%(default_image_filename)s
""" % { """ % {
'default_image_filename': os.path.basename(current), 'default_image_filename': os.path.basename(current),
}) })
files = [] files = []
for filename in os.listdir(self._image_dir): for filename in os.listdir(self._image_dir):
if not filename.endswith('.iso'): if not filename.endswith('.iso'):
continue continue
files.append(filename) files.append(filename)
for i, filename in enumerate(sorted(files, reverse=True)): for i, filename in enumerate(sorted(files, reverse=True)):
sys.stdout.write(""" fh.write("""
menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s { menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s {
search --no-floppy --file --set=root %(image_path)s/%(image_filename)s search --no-floppy --file --set=root %(image_path)s/%(image_filename)s
iso_path="%(image_path)s/%(image_filename)s" iso_path="%(image_path)s/%(image_filename)s"
@@ -56,8 +59,11 @@ menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s {
configfile /boot/grub/loopback.cfg configfile /boot/grub/loopback.cfg
} }
""" % { """ % {
'image_filename': filename, 'image_filename': filename,
'image_path': self._image_path, 'image_path': self._image_path,
'hotkey': self._HOTKEYS[i], 'hotkey': self._HOTKEYS[i],
'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)), 'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)),
}) })
fh.flush()
os.rename(fh.name, os.path.join(grub_dir, 'grub.cfg'))