1 from os import system, listdir, statvfs, popen
3 from Tools.Directories import SCOPE_HDD, resolveFilename
4 from Tools.CList import CList
8 procFile = open(filename)
14 return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
17 def __init__(self, index):
21 bus = (self.index & 2)
22 target = (self.index & 1)
24 self.prochdx = num2prochdx(index)
25 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
34 ret = "External (CF) - "
44 procfile = tryOpen(self.prochdx + "capacity")
49 line = procfile.readline()
57 cap = cap / 1000 * 512 / 1000
59 return "%d.%03d GB" % (cap/1024, cap%1024)
62 procfile = tryOpen(self.prochdx + "model")
67 line = procfile.readline()
73 procfile = tryOpen("/proc/mounts")
80 line = procfile.readline()
83 if line.startswith(self.devidex):
84 parts = line.strip().split(" ")
86 stat = statvfs(parts[1])
89 free = stat.f_bfree/1000 * stat.f_bsize/1000
94 def numPartitions(self):
96 idedir = listdir(self.devidex)
100 for filename in idedir:
101 if filename.startswith("disc"):
103 if filename.startswith("part"):
108 cmd = "/bin/umount " + self.devidex + "part*"
112 def createPartition(self):
113 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
114 sfdisk = popen(cmd, "w")
115 sfdisk.write("0,\n;\n;\n;\ny\n")
120 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
125 cmd = "/bin/mount -t ext3 " + self.devidex + "part1 /hdd"
129 def createMovieFolder(self):
130 res = system("mkdir " + resolveFilename(SCOPE_HDD))
133 errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Unmount failed")]
135 def initialize(self):
138 if self.createPartition() != 0:
144 if self.mount() != 0:
147 if self.createMovieFolder() != 0:
153 mediafile = tryOpen(num2prochdx(num) + "media")
158 line = mediafile.readline()
161 if line.startswith("disk"):
167 def __init__(self, mountpoint, description = "", force_mounted = False):
168 self.mountpoint = mountpoint
169 self.description = description
170 self.force_mounted = force_mounted
173 return statvfs(self.mountpoint)
178 return s.f_bavail * s.f_bsize
185 return s.f_blocks * s.f_bsize
190 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
191 # TODO: can os.path.ismount be used?
192 if self.force_mounted:
194 procfile = tryOpen("/proc/mounts")
195 for n in procfile.readlines():
196 if n.split(' ')[1] == self.mountpoint:
200 class HarddiskManager:
205 self.partitions = [ ]
207 self.on_partition_list_change = CList()
209 for hddNum in range(8):
211 hdd = Harddisk(hddNum)
214 # currently, this is just an enumeration of what's possible,
215 # this probably has to be changed to support automount stuff.
216 # still, if stuff is mounted into the correct mountpoints by
217 # external tools, everything is fine (until somebody inserts
218 # a second usb stick.)
220 ("/media/hdd", _("Harddisk")),
221 ("/media/card", _("Card")),
222 ("/media/cf", _("Compact Flash")),
223 ("/media/mmc1", _("MMC Card")),
224 ("/media/net", _("Network Mount")),
225 ("/media/ram", _("Ram Disk")),
226 ("/media/usb", _("USB Stick")),
227 ("/", _("Internal Flash"))
231 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
233 def getAutofsMountpoint(self, device):
234 return "/autofs/%s/" % (device)
236 def addHotplugPartition(self, device, description):
237 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
238 self.partitions.append(p)
239 self.on_partition_list_change("add", p)
241 def removeHotplugPartition(self, device):
242 mountpoint = self.getAutofsMountpoint(device)
243 for x in self.partitions[:]:
244 if x.mountpoint == mountpoint:
245 self.partitions.remove(x)
246 self.on_partition_list_change("remove", x)
254 hdd = hd.model() + " ("
260 list.append((hdd, hd))
264 def getMountedPartitions(self):
265 return [x for x in self.partitions if x.mounted()]
267 harddiskmanager = HarddiskManager()