pvl/backup/mount.py
changeset 78 5698225aab66
parent 69 468704db09c4
equal deleted inserted replaced
77:0e79da714c19 78:5698225aab66
    59 
    59 
    60         # check
    60         # check
    61         if not os.path.isdir(self.mnt) :
    61         if not os.path.isdir(self.mnt) :
    62             raise MountError("Mountpoint is not a directory: {mnt}".format(mnt=self.mnt))
    62             raise MountError("Mountpoint is not a directory: {mnt}".format(mnt=self.mnt))
    63 
    63 
    64         if os.path.ismount(self.mnt) :
    64         if self.is_mounted() :
    65             raise MountError("Mountpoint is already mounted: {mnt}".format(mnt=self.mnt))
    65             raise MountError("Mountpoint is already mounted: {mnt}".format(mnt=self.mnt))
    66 
    66 
    67         if not os.path.exists(self.dev) :
    67         if not os.path.exists(self.dev) :
    68             raise MountError("Device does not exist: {dev}".format(dev=self.dev))
    68             raise MountError("Device does not exist: {dev}".format(dev=self.dev))
    69 
    69 
    70         # mount
    70         # mount
    71         invoke(self.MOUNT, optargs(self.dev, self.mnt, options=self.options()), sudo=self.sudo)
    71         invoke(self.MOUNT, optargs(self.dev, self.mnt, options=self.options()), sudo=self.sudo)
    72 
    72 
       
    73     def is_mounted (self) :
       
    74         """
       
    75             Test if the given mountpoint is mounted.
       
    76         """
       
    77         
       
    78         # workaround http://bugs.python.org/issue2466
       
    79         if os.path.exists(self.mnt) and not os.path.exists(os.path.join(self.mnt, '.')) :
       
    80             # this is a sign of a mountpoint that we do not have access to
       
    81             return True
       
    82 
       
    83         return os.path.ismount(self.mnt)
       
    84 
    73     def close (self) :
    85     def close (self) :
    74         """
    86         """
    75             Un-mount
    87             Un-mount
    76         """
    88         """
    77 
    89 
    78         # check
    90         # check
    79         if not os.path.ismount(self.mnt):
    91         if not self.is_mounted() :
    80             raise MountError("Mountpoint is not mounted: {mnt}".format(mnt=self.mnt))
    92             raise MountError("Mountpoint is not mounted: {mnt}".format(mnt=self.mnt))
    81 
    93 
    82         # umount
    94         # umount
    83         invoke(self.UMOUNT, optargs(self.mnt), sudo=self.sudo)
    95         invoke(self.UMOUNT, optargs(self.mnt), sudo=self.sudo)
    84 
    96 
   125             yield mount
   137             yield mount
   126 
   138 
   127         finally:
   139         finally:
   128             # cleanup
   140             # cleanup
   129             log.debug("cleanup: %s", mount)
   141             log.debug("cleanup: %s", mount)
   130             mount.close()
   142 
       
   143             try :
       
   144                 mount.close()
       
   145 
       
   146             except Exception as ex :
       
   147                 log.warning("cleanup: %s: %s", mount, ex)
   131 
   148 
   132     finally:
   149     finally:
   133         if tmpdir :
   150         if tmpdir :
   134             # cleanup
   151             # cleanup
   135             log.debug("cleanup tmp mnt: %s", tmpdir)
   152             log.debug("cleanup tmp mnt: %s", tmpdir)