Source code for vinstall.backend.media
#!/bin/env python
"""Media handling. Implements a Partition class providing methods
formounting,umounting, formating and querying partitions.
"""
__author__ = "rbistolfi"
import re
import os
import subprocess
from utils import mount, umount, is_mounted, format_partition
# I think we should have a config.py and import settings from there
DEV_DIR = '/dev'
CD_INFO = '/proc/sys/dev/cdrom/info'
PARTITIONS = '/proc/partitions'
MOUNTPOINT = '/mnt/loop'
[docs]def list_cdroms(proc_info=CD_INFO):
"""Find CDROM devices based in the information stored in the proc tree.
"""
file_handler = open(proc_info)
data = file_handler.read()
file_handler.close()
return re.findall(r'^drive name:\s*([hs]d[a-z]|s[rg]\d*)', data, re.M)
[docs]def list_partitions(proc_partitions_info=PARTITIONS):
"""List the partitions present in the system by parsing /proc/partitions.
"""
file_handler = open(proc_partitions_info)
data = file_handler.read()
file_handler.close()
return [ os.path.join(DEV_DIR, i) for i in \
re.findall(r'[sh]d[a-z]\d{1,2}', data) ]
[docs]def list_disks():
"""List the disks present in the system by parsing fdisk -l. Returns a
tuple with path in the first position, and the string representation of the
size in the second.
#TODO: we shouldnt rely in the fdisk formatting. Use pyparted or similar.
"""
popen = subprocess.Popen('/sbin/fdisk -l'.split(), env={"LANG": "en_US"},
stdout=subprocess.PIPE)
data = popen.stdout.read()
popen.stdout.close()
print data
return re.findall(r'Disk (/dev/[sh]d[a-z]):\s*(\d+\.\d+ GB)', data)
[docs]def list_swap():
"""Returns a list of swap devices."""
swaps = []
for i in list_partitions():
process = subprocess.Popen(['blkid', i],
stdout=subprocess.PIPE)
data = process.stdout.read()
process.stdout.close()
if "swap" in data:
swaps.append(i)
return swaps
[docs]class Partition(object):
"""A class representing a partition in a hard disk.
device_path: the path to the device node.
"""
def __str__(self):
if self.query_filesystem():
filesystem = self.query_filesystem()
else:
filesystem = "Not formated"
return '%s (%s)' % (self.device_path, filesystem)
def __repr__(self):
return '<Partition object: %s>' % self.device_path
def __init__(self, device_path):
"""Initializes a Media object fro the device path."""
self.mountpoint = None
self.device_path = device_path
[docs] def mount(self, mountpoint, mount_opt="auto"):
"""Mount the media in the specified mountpoint."""
new_mountpoint = mount(self.device_path, mountpoint, extra=mount_opt)
self.mountpoint = new_mountpoint
[docs] def umount(self):
"""Umount the media."""
return umount(self.mountpoint)
[docs] def is_mounted(self):
"""Returns True if the media is mounted, False otherwise."""
return is_mounted(self.device_path)
[docs] def query_filesystem(self):
"""Retrieve the current filesystem type from the system."""
process = subprocess.Popen(['blkid', self.device_path],
stdout=subprocess.PIPE)
data = process.stdout.read()
process.stdout.close()
try:
return re.search(r'TYPE="(\w+)"', data).group(1)
except AttributeError:
#blkid returned nothing
return None
if __name__ == "__main__":
import doctest
doctest.testmod(verbose = True)