diff options
| author | Joerg Hofrichter <joerg.hofrichter@ni.com> | 2020-07-08 12:46:25 +0200 | 
|---|---|---|
| committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2021-02-11 07:18:45 -0600 | 
| commit | 3f3006e0b4940825444f19e190b8dccf690e59ca (patch) | |
| tree | b92deb6a83aaed8f95aab010ad7b8aa33587a9d6 /mpm/python | |
| parent | d2c0a464084535af4393e7e2f444668e5e7b9e8a (diff) | |
| download | uhd-3f3006e0b4940825444f19e190b8dccf690e59ca.tar.gz uhd-3f3006e0b4940825444f19e190b8dccf690e59ca.tar.bz2 uhd-3f3006e0b4940825444f19e190b8dccf690e59ca.zip | |
mpm: added Mount class
Class for creating a mount point
Diffstat (limited to 'mpm/python')
| -rw-r--r-- | mpm/python/usrp_mpm/sys_utils/mount.py | 96 | 
1 files changed, 96 insertions, 0 deletions
| diff --git a/mpm/python/usrp_mpm/sys_utils/mount.py b/mpm/python/usrp_mpm/sys_utils/mount.py new file mode 100644 index 000000000..2e32fa014 --- /dev/null +++ b/mpm/python/usrp_mpm/sys_utils/mount.py @@ -0,0 +1,96 @@ +# +# Copyright 2020 Ettus Research, a National Instruments Brand +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Utilities for creating a mount point +""" + +import subprocess +import os +from usrp_mpm.mpmlog import get_logger + +class Mount(): +    """ +    Class for creating a mount point +    """ + +    def __init__(self, devicepath, mountpoint, options=None, log=None): +        assert isinstance(devicepath, str) +        assert isinstance(mountpoint, str) +        assert isinstance(options, list) +        self.devicepath = devicepath +        self.mountpoint = mountpoint +        self.options = options +        if log is None: +            self.log = get_logger("Mount") +        else: +            self.log = log.getChild("Mount") +        self.log.trace("Early initialization: devicepath={}, mountpoint={}, options={}".format( +            devicepath, mountpoint, options)) + +    def ismounted(self): +        """ +        Returns true if the mount point is mounted +        """ +        assert self.devicepath is not None +        collection = [line.split()[0] for line in open("/etc/mtab") +                      if line.split()[0] == self.devicepath] +        mounted = len(collection) != 0 +        return mounted + +    def get_mount_point(self): +        """ +        returns the mount point (None when not mounted) +        """ +        if not self.ismounted(): +            return None +        return self.mountpoint + +    def prepare_mountpoint(self): +        """ +        Creates the mount point directory (if not already existing) +        """ +        if not os.path.exists(self.mountpoint): +            os.makedirs(self.mountpoint) +        return True + +    def delete_mountpoint(self): +        """ +        Deletes the mount point directory +        """ +        os.removedirs(self.mountpoint) +        return True + +    def mount(self): +        """ +        Mounts the mount point +        """ +        if self.ismounted(): +            self.log.warning("{} was already mounted".format(self.mountpoint)) +            return True +        self.prepare_mountpoint() +        self.log.debug("Mounting {}".format(self.mountpoint)) +        cmd = ['mount'] +        if self.options: +            cmd.extend(self.options) +        cmd.append(self.devicepath) +        cmd.append(self.mountpoint) +        proc = subprocess.run(cmd, check=True) +        self.log.trace(proc) +        return True + +    def unmount(self): +        """ +        Unmounts the mount point +        """ +        if not self.ismounted(): +            self.log.warning("{} was not mounted".format(self.mountpoint)) +            return True +        self.log.debug("Unmounting {}".format(self.mountpoint)) +        cmd = ['umount', self.mountpoint] +        proc = subprocess.run(cmd, check=True) +        self.log.trace(proc) +        self.delete_mountpoint() +        return True | 
