summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPedro Alvarez <pedro.alvarez@codethink.co.uk>2014-12-01 15:25:21 +0000
committerPedro Alvarez <pedro.alvarez@codethink.co.uk>2014-12-01 16:39:42 +0000
commitb03e6f1c4342d48c3fb9e530a49807e08a0162e7 (patch)
tree7797cd044bad22bc8769936ae16727e534cfea56
parent5aa74fd32200d84e81e1473eed9e155ebd5a379b (diff)
downloaddefinitions-b03e6f1c4342d48c3fb9e530a49807e08a0162e7.tar.gz
writeexts.py: convert 'mount' to context manager
-rw-r--r--writeexts.py65
1 files changed, 27 insertions, 38 deletions
diff --git a/writeexts.py b/writeexts.py
index 7030c0b5..91936f64 100644
--- a/writeexts.py
+++ b/writeexts.py
@@ -173,17 +173,14 @@ class WriteExtension(cliapp.Application):
raise
def create_system(self, temp_root, raw_disk):
- try:
- mp = self.mount(raw_disk)
- self.create_btrfs_system_layout(
- temp_root, mp, version_label='factory',
- disk_uuid=self.get_uuid(raw_disk))
- except BaseException, e:
- sys.stderr.write('Error creating Btrfs system layout')
- self.unmount(mp)
- raise
- else:
- self.unmount(mp)
+ with self.mount(raw_disk) as mp:
+ try:
+ self.create_btrfs_system_layout(
+ temp_root, mp, version_label='factory',
+ disk_uuid=self.get_uuid(raw_disk))
+ except BaseException, e:
+ sys.stderr.write('Error creating Btrfs system layout')
+ raise
def _parse_size(self, size):
'''Parse a size from a string.
@@ -249,34 +246,26 @@ class WriteExtension(cliapp.Application):
# lies by exiting successfully.
return cliapp.runcmd(['blkid', '-s', 'UUID', '-o', 'value',
location]).strip()
-
- def mount(self, location):
- '''Mount the filesystem so it can be tweaked.
-
- Return path to the mount point.
- The mount point is a newly created temporary directory.
- The caller must call self.unmount to unmount on the return value.
-
- '''
- self.status(msg='Mounting filesystem')
- tempdir = tempfile.mkdtemp()
- if self.is_device(location):
- cliapp.runcmd(['mount', location, tempdir])
- else:
- cliapp.runcmd(['mount', '-o', 'loop', location, tempdir])
- return tempdir
-
- def unmount(self, mount_point):
- '''Unmount the filesystem mounted by self.mount.
-
- Also, remove the temporary directory.
-
- '''
-
- self.status(msg='Unmounting filesystem')
- cliapp.runcmd(['umount', mount_point])
- os.rmdir(mount_point)
+ @contextlib.contextmanager
+ def mount(self, location):
+ self.status(msg='Mounting filesystem')
+ try:
+ mount_point = tempfile.mkdtemp()
+ if self.is_device(location):
+ cliapp.runcmd(['mount', location, mount_point])
+ else:
+ cliapp.runcmd(['mount', '-o', 'loop', location, mount_point])
+ except BaseException, e:
+ sys.stderr.write('Error mounting filesystem')
+ os.rmdir(mount_point)
+ raise
+ try:
+ yield mount_point
+ finally:
+ self.status(msg='Unmounting filesystem')
+ cliapp.runcmd(['umount', mount_point])
+ os.rmdir(mount_point)
def create_btrfs_system_layout(self, temp_root, mountpoint, version_label,
disk_uuid):