Skip to content

Commit 1f9ea06

Browse files
Improve exception handling
add _run_cmd method
1 parent 7230c35 commit 1f9ea06

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

lib/inputstreamhelper.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ def _parse_chromeos_offset(self, bin_path):
137137
self._log('Failed to calculate losetup offset.')
138138
return False
139139

140+
def _run_cmd(self, cmd):
141+
try:
142+
output = subprocess.check_output(cmd)
143+
self._log('{0} cmd executed successfully.'.format(cmd))
144+
return True
145+
except subprocess.CalledProcessError, error:
146+
self._log('cmd failed with output: {0}'.format(error.output))
147+
return False
148+
140149
def _set_loop_dev(self):
141150
"""Set an unused loop device that's available for use."""
142151
cmd = ['losetup', '-f']
@@ -147,9 +156,12 @@ def _set_loop_dev(self):
147156
def _losetup(self, bin_path):
148157
"""Setup Chrome OS loop device."""
149158
cmd = ['losetup', self._loop_dev, bin_path, '-o', self._parse_chromeos_offset(bin_path)]
150-
subprocess.check_output(cmd)
151-
self._attached_loop_dev = True
152-
return True
159+
success = self._run_cmd(cmd)
160+
if success:
161+
self._attached_loop_dev = True
162+
return True
163+
else:
164+
return False
153165

154166
def _mnt_loop_dev(self):
155167
"""Mount loop device to self._mnt_path()"""
@@ -163,9 +175,12 @@ def _mnt_loop_dev(self):
163175
else:
164176
self._log('User do not have root permissions and/or sudo installed.')
165177

166-
subprocess.check_output(cmd)
167-
self._mounted = True
168-
return True
178+
success = self._run_cmd(cmd)
179+
if success:
180+
self._mounted = True
181+
return True
182+
else:
183+
return False
169184

170185
def _has_widevine_cdm(self):
171186
if xbmc.getCondVisibility('system.platform.android'): # widevine is built in on android
@@ -463,12 +478,14 @@ def _cleanup(self):
463478
cmd = ['umount', self._mnt_path()]
464479
if not os.getuid() == 0 and self._cmd_exists('sudo'): # no need to ask for permission again
465480
cmd.insert(0, 'sudo')
466-
subprocess.check_call(cmd)
467-
self._mounted = False
481+
unmount_success = self._run_cmd(cmd)
482+
if unmount_success:
483+
self._mounted = False
468484
if self._attached_loop_dev:
469485
cmd = ['losetup', '-d', self._loop_dev]
470-
subprocess.check_call(cmd)
471-
self._loop_dev = False
486+
unattach_success = self._run_cmd(cmd)
487+
if unattach_success:
488+
self._loop_dev = False
472489

473490
shutil.rmtree(self._temp_path())
474491
return True

0 commit comments

Comments
 (0)