diff --git a/client/client.py b/client/client.py index 1d886aa..49b97d5 100755 --- a/client/client.py +++ b/client/client.py @@ -3,8 +3,8 @@ import argparse import fetcher import json +import lib import os -import re import socket import subprocess import time @@ -58,8 +58,6 @@ FLAGS = parser.parse_args() class Client(threadedclient.WebSocketClient): - _VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P.+)$', re.MULTILINE) - def __init__(self, config_path, *args, **kwargs): super().__init__(*args, **kwargs) with open(config_path, 'r') as fh: @@ -74,7 +72,7 @@ class Client(threadedclient.WebSocketClient): 'hostname': socket.gethostname(), 'uptime_seconds': self._Uptime(), 'next_timestamp': self._NextTimestamp(), - 'next_volume_id': self._GetVolumeID('/isodevice/iconograph/current'), + 'next_volume_id': lib.GetVolumeID('/isodevice/iconograph/current'), } report.update(self._config) self.send(json.dumps({ @@ -91,15 +89,6 @@ class Client(threadedclient.WebSocketClient): next_image = os.path.basename(os.readlink('/isodevice/iconograph/current')) return int(next_image.split('.', 1)[0]) - def _GetVolumeID(self, path): - isoinfo = subprocess.check_output([ - 'isoinfo', - '-d', - '-i', path, - ]) - match = self._VOLUME_ID_REGEX.search(isoinfo) - return match.group('volume_id').decode('ascii') - def _OnImageTypes(self, data): assert self._config['image_type'] in data['image_types'] diff --git a/client/lib.py b/client/lib.py new file mode 100644 index 0000000..abffe26 --- /dev/null +++ b/client/lib.py @@ -0,0 +1,15 @@ +import re +import subprocess + + +_VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P.+)$', re.MULTILINE) + + +def GetVolumeID(path): + isoinfo = subprocess.check_output([ + 'isoinfo', + '-d', + '-i', path, + ]) + match = _VOLUME_ID_REGEX.search(isoinfo) + return match.group('volume_id').decode('ascii') diff --git a/client/update_grub.py b/client/update_grub.py index 94cfbf3..7afcc2d 100755 --- a/client/update_grub.py +++ b/client/update_grub.py @@ -1,15 +1,13 @@ #!/usr/bin/python3 +import lib import os -import re import string -import subprocess import tempfile class GrubUpdater(object): - _VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P.+)$', re.MULTILINE) _HOTKEYS = string.digits + string.ascii_letters def __init__(self, image_dir, boot_dir): @@ -20,15 +18,6 @@ class GrubUpdater(object): self._image_path = '/' + os.path.relpath(self._image_dir, self._boot_dir) - def _GetVolumeID(self, path): - isoinfo = subprocess.check_output([ - 'isoinfo', - '-d', - '-i', path, - ]) - match = self._VOLUME_ID_REGEX.search(isoinfo) - return match.group('volume_id').decode('ascii') - def Update(self): grub_dir = os.path.join(self._boot_dir, 'grub') @@ -63,7 +52,7 @@ menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s { 'image_filename': filename, 'image_path': self._image_path, 'hotkey': self._HOTKEYS[i], - 'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)), + 'volume_id': lib.GetVolumeID(os.path.join(self._image_dir, filename)), }) fh.flush()