Move GetVolumeID to common lib

This commit is contained in:
Ian Gulliver
2016-05-11 05:02:58 +00:00
parent 8fdd0370d3
commit 8b08a5cec6
3 changed files with 19 additions and 26 deletions

View File

@@ -3,8 +3,8 @@
import argparse import argparse
import fetcher import fetcher
import json import json
import lib
import os import os
import re
import socket import socket
import subprocess import subprocess
import time import time
@@ -58,8 +58,6 @@ FLAGS = parser.parse_args()
class Client(threadedclient.WebSocketClient): class Client(threadedclient.WebSocketClient):
_VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P<volume_id>.+)$', re.MULTILINE)
def __init__(self, config_path, *args, **kwargs): def __init__(self, config_path, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
with open(config_path, 'r') as fh: with open(config_path, 'r') as fh:
@@ -74,7 +72,7 @@ class Client(threadedclient.WebSocketClient):
'hostname': socket.gethostname(), 'hostname': socket.gethostname(),
'uptime_seconds': self._Uptime(), 'uptime_seconds': self._Uptime(),
'next_timestamp': self._NextTimestamp(), 'next_timestamp': self._NextTimestamp(),
'next_volume_id': self._GetVolumeID('/isodevice/iconograph/current'), 'next_volume_id': lib.GetVolumeID('/isodevice/iconograph/current'),
} }
report.update(self._config) report.update(self._config)
self.send(json.dumps({ self.send(json.dumps({
@@ -91,15 +89,6 @@ class Client(threadedclient.WebSocketClient):
next_image = os.path.basename(os.readlink('/isodevice/iconograph/current')) next_image = os.path.basename(os.readlink('/isodevice/iconograph/current'))
return int(next_image.split('.', 1)[0]) 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): def _OnImageTypes(self, data):
assert self._config['image_type'] in data['image_types'] assert self._config['image_type'] in data['image_types']

15
client/lib.py Normal file
View File

@@ -0,0 +1,15 @@
import re
import subprocess
_VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P<volume_id>.+)$', 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')

View File

@@ -1,15 +1,13 @@
#!/usr/bin/python3 #!/usr/bin/python3
import lib
import os import os
import re
import string import string
import subprocess
import tempfile import tempfile
class GrubUpdater(object): class GrubUpdater(object):
_VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P<volume_id>.+)$', re.MULTILINE)
_HOTKEYS = string.digits + string.ascii_letters _HOTKEYS = string.digits + string.ascii_letters
def __init__(self, image_dir, boot_dir): 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) 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): def Update(self):
grub_dir = os.path.join(self._boot_dir, 'grub') 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_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': lib.GetVolumeID(os.path.join(self._image_dir, filename)),
}) })
fh.flush() fh.flush()