Created
October 24, 2011 11:37
-
-
Save jiphex/1308829 to your computer and use it in GitHub Desktop.
Python Filesystem Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Filesystem(object): | |
# Represents a Filesystem, with convenience methods to generate the mount command | |
BLANK=0x0 | |
FAT16=0x4 | |
NTFS=0x7 | |
EXT=0x83 | |
LVM=0x8e | |
FAT32=0xb | |
SWAP=0x82 | |
VMFS=0xfb | |
fs_types = { | |
BLANK: 'Blank', | |
FAT16: 'FAT16', | |
NTFS: 'NTFS', | |
EXT: 'Ext2/3', | |
LVM: 'LVM', | |
FAT32: 'FAT32', | |
SWAP: 'Swap', | |
VMFS: 'VMware VMFS'} | |
def symbolic_type(self): | |
if(self.type in self.fs_types): | |
return self.fs_types[self.type] | |
else: | |
return 'Unknown' | |
def mount_command(self, target='/mnt', type=None, options=[], use_label=False,as_array=False): | |
mount_device = self.device | |
if(use_label): | |
mlabel = self.label() | |
if(len(mlabel) == 0): | |
pass | |
else: | |
mount_device = "LABEL=%s" % mlabel | |
mount_options = "" | |
if(len(options) > 0): | |
mount_options += "-o " | |
for o in mount_options: | |
mount_options+=s+"" | |
mount_type = "" | |
if(type != None): | |
mount_type = "-t %s" % type | |
mount_cmd=filter(lambda x: x != "", ["mount", mount_type.strip(), mount_options.strip(), mount_device.strip(), target.strip()]) | |
if(as_array): | |
return mount_cmd | |
else: | |
return " ".join(mount_cmd) | |
def label(self): | |
if(self.type == self.EXT): | |
# ext3 | |
label = Popen(['e2label',self.device],stdout=PIPE).communicate()[0] | |
if(len(label) > 0): | |
label = label.strip() | |
return label | |
else: | |
return "" | |
def __init__(self,device,type,size): | |
self.device = device | |
self.type = int(type,16) | |
self.size = size | |
def __str__(self): | |
return "<Filesystem device='%s' size='%s' type='%s' typel='%s'>" % (self.device,self.size,hex(self.type),self.symbolic_type()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment