pvl/backup/mount.py
changeset 69 468704db09c4
parent 33 2de09042414f
child 78 5698225aab66
equal deleted inserted replaced
68:adc190def3b1 69:468704db09c4
     1 """
     1 """
     2     Mount filesystems.
     2     Mount filesystems.
     3 """
     3 """
     4 
     4 
     5 from pvl.backup.invoke import command
     5 from pvl.backup.invoke import invoke, optargs, command
     6 
     6 
     7 import contextlib
     7 import contextlib
     8 import os, os.path
     8 import os, os.path
     9 import logging
     9 import logging
    10 import tempfile
    10 import tempfile
    22 
    22 
    23     MOUNT   = '/bin/mount'
    23     MOUNT   = '/bin/mount'
    24     UMOUNT  = '/bin/umount'
    24     UMOUNT  = '/bin/umount'
    25 
    25 
    26 
    26 
    27     def __init__ (self, dev, mnt, readonly=False) :
    27     def __init__ (self, dev, mnt, readonly=False, sudo=None) :
    28         """
    28         """
    29             dev         - device path
    29             dev         - device path
    30             mnt         - mount path
    30             mnt         - mount path
    31             readonly    - mount readonly
    31             readonly    - mount readonly
       
    32             sudo        - invoke sudo
    32         """
    33         """
    33 
    34 
    34         self.dev = dev
    35         self.dev = dev
    35         self.mnt = mnt
    36         self.mnt = mnt
    36         self.readonly = readonly
    37         self.readonly = readonly
       
    38         self.sudo = sudo
    37 
    39 
    38     @property
    40     @property
    39     def path (self) :
    41     def path (self) :
    40         return self.mnt
    42         return self.mnt
    41 
    43 
    64 
    66 
    65         if not os.path.exists(self.dev) :
    67         if not os.path.exists(self.dev) :
    66             raise MountError("Device does not exist: {dev}".format(dev=self.dev))
    68             raise MountError("Device does not exist: {dev}".format(dev=self.dev))
    67 
    69 
    68         # mount
    70         # mount
    69         command(self.MOUNT, self.dev, self.mnt, options=self.options())
    71         invoke(self.MOUNT, optargs(self.dev, self.mnt, options=self.options()), sudo=self.sudo)
    70 
    72 
    71     def close (self) :
    73     def close (self) :
    72         """
    74         """
    73             Un-mount
    75             Un-mount
    74         """
    76         """
    76         # check
    78         # check
    77         if not os.path.ismount(self.mnt):
    79         if not os.path.ismount(self.mnt):
    78             raise MountError("Mountpoint is not mounted: {mnt}".format(mnt=self.mnt))
    80             raise MountError("Mountpoint is not mounted: {mnt}".format(mnt=self.mnt))
    79 
    81 
    80         # umount
    82         # umount
    81         command(self.UMOUNT, self.mnt)
    83         invoke(self.UMOUNT, optargs(self.mnt), sudo=self.sudo)
    82 
    84 
    83     def __repr__ (self) :
    85     def __repr__ (self) :
    84         return "Mount(dev={dev}, mnt={mnt})".format(
    86         return "Mount(dev={dev}, mnt={mnt})".format(
    85                 dev     = repr(self.dev),
    87                 dev     = repr(self.dev),
    86                 mnt     = repr(self.mnt),
    88                 mnt     = repr(self.mnt),