Best Python code snippet using avocado_python
partition.py
Source:partition.py  
...81                        % (device, self.loop))82    def __repr__(self):83        return '<Partition: %s>' % self.device84    @staticmethod85    def list_mount_devices():86        """87        Lists mounted file systems and swap on devices.88        """89        # list mounted file systems90        devices = [line.split()[0]91                   for line in process.system_output('mount').splitlines()]92        # list mounted swap devices93        swaps = process.system_output('swapon -s').splitlines()94        devices.extend([line.split()[0] for line in swaps95                        if line.startswith('/')])96        return devices97    @staticmethod98    def list_mount_points():99        """100        Lists the mount points.101        """102        return [line.split()[2]103                for line in process.system_output('mount').splitlines()]104    def get_mountpoint(self, filename=None):105        """106        Find the mount point of this partition object.107        :param filename: where to look for the mounted partitions information108                (default None which means it will search /proc/mounts and/or109                /etc/mtab)110        :return: a string with the mount point of the partition or None if not111                mounted112        """113        # Try to match this device/mountpoint114        if filename:115            for line in open(filename):116                parts = line.split()117                if parts[0] == self.device or parts[1] == self.mountpoint:118                    return parts[1]    # The mountpoint where it's mounted119            return None120        # no specific file given, look in /proc/mounts121        res = self.get_mountpoint(filename='/proc/mounts')122        if not res:123            # sometimes the root partition is reported as /dev/root in124            # /proc/mounts in this case, try /etc/mtab125            res = self.get_mountpoint(filename='/etc/mtab')126            if res != '/':127                res = None128        return res129    def mkfs(self, fstype=None, args=''):130        """131        Format a partition to filesystem type132        :param fstype: the filesystem type, such as "ext3", "ext2". Defaults133                       to previously set type or "ext2" if none has set.134        :param args: arguments to be passed to mkfs command.135        """136        if self.device in self.list_mount_devices():137            raise PartitionError(self, 'Unable to format mounted device')138        if not fstype:139            if self.fstype:140                fstype = self.fstype141            else:142                fstype = 'ext2'143        if self.mkfs_flags:144            args += ' ' + self.mkfs_flags145        if fstype == 'xfs':146            args += ' -f'147        if self.loop:148            if fstype.startswith('ext'):149                args += ' -F'150            elif fstype == 'reiserfs':151                args += ' -f'152        # If there isn't already a '-t <type>' argument, add one.153        if "-t" not in args:154            args = "-t %s %s" % (fstype, args)155        args = args.strip()156        mkfs_cmd = "mkfs %s %s" % (args, self.device)157        try:158            process.system_output("yes | %s" % mkfs_cmd, shell=True)159        except process.CmdError as error:160            raise PartitionError(self, "Failed to mkfs", error)161        else:162            self.fstype = fstype163    def mount(self, mountpoint=None, fstype=None, args=''):164        """165        Mount this partition to a mount point166        :param mountpoint: If you have not provided a mountpoint to partition167                object or want to use a different one, you may specify it here.168        :param fstype: Filesystem type. If not provided partition object value169                will be used.170        :param args: Arguments to be passed to "mount" command.171        """172        if not mountpoint:173            mountpoint = self.mountpoint174        if not mountpoint:175            raise PartitionError(self, "No mountpoint specified and no "176                                 "default provided to this partition object")177        if fstype is None:178            fstype = self.fstype179        else:180            self.fstype = fstype181        if self.mount_options:182            args += ' -o ' + self.mount_options183        if fstype:184            args += ' -t ' + fstype185        if self.loop:186            args += ' -o loop'187        args = args.lstrip()188        with MtabLock():189            if self.device in self.list_mount_devices():190                raise PartitionError(self, "Attempted to mount mounted device")191            if mountpoint in self.list_mount_points():192                raise PartitionError(self, "Attempted to mount busy mountpoint")193            if not os.path.isdir(mountpoint):194                os.makedirs(mountpoint)195            try:196                process.system("mount %s %s %s"197                               % (args, self.device, mountpoint), sudo=True)198            except process.CmdError as details:199                raise PartitionError(self, "Mount failed", details)200        # Update the fstype as the mount command passed201        self.fstype = fstype202    def _unmount_force(self, mountpoint):203        """...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
