0009a6b81102b02992fb0c3dbe087cd9d75ebea0
[enigma2.git] / lib / python / Components / Harddisk.py
1 import os
2
3 def tryOpen(filename):
4         try:
5                 procFile = open(filename)
6         except IOError:
7                 return ""
8         return procFile
9
10 def num2prochdx(num):
11         return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
12
13 class Harddisk:
14         def __init__(self, index):
15                 self.index = index
16
17                 host = self.index / 4
18                 bus = (self.index & 2)
19                 target = (self.index & 1)
20
21                 self.prochdx = num2prochdx(index)
22                 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
23
24         def hdindex(self):
25                 return self.index
26         def capacity(self):
27                 procfile = tryOpen(self.prochdx + "capacity")
28                 
29                 if procfile == "":
30                         return -1
31
32                 line = procfile.readline()
33                 procfile.close()
34                 
35                 try:
36                         cap = int(line)
37                 except:
38                         return -1
39                 
40                 return cap      
41                                                 
42         def model(self):
43                 procfile = tryOpen(self.prochdx + "model")
44                 
45                 if procfile == "":
46                         return ""
47
48                 line = procfile.readline()
49                 procfile.close()
50
51                 return line.strip()
52
53         def free(self):
54                 procfile = tryOpen("/proc/mounts")
55                 
56                 if procfile == "":
57                         return -1
58
59                 free = -1
60                 while 1:
61                         line = procfile.readline()
62                         if line == "":
63                                 break
64                         if line.startswith(self.devidex):
65                                 parts = line.strip().split(" ")
66                                 try:
67                                         stat = os.statvfs(parts[1])
68                                 except OSError:
69                                         continue
70                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
71                                 break
72                 procfile.close()
73                 return free             
74
75         def numPartitions(self):
76                 try:
77                         idedir = os.listdir(self.devidex)
78                 except OSError:
79                         return -1
80                 numPart = -1
81                 for filename in idedir:
82                         if filename.startswith("disc"):
83                                 numPart += 1
84                         if filename.startswith("part"):
85                                 numPart += 1
86                 return numPart
87
88
89 def existHDD(num):
90         mediafile = tryOpen(num2prochdx(num) + "media")
91
92         if mediafile == "":
93                 return -1
94
95         line = mediafile.readline()
96         mediafile.close()
97         
98         if line.startswith("disk"):
99                 return 1
100         
101         return -1
102
103 class HarddiskManager:
104         def __init__(self):
105                 hddNum = 0
106                 self.hdd = [ ]
107                 while 1:
108                         if existHDD(hddNum) == 1:
109                                 self.hdd.append(Harddisk(hddNum))
110                         hddNum += 1
111                         
112                         if hddNum > 8:
113                                 break
114                 
115         def HDDList(self):
116                 list = [ ]
117                 for hd in self.hdd:
118                         cap = hd.capacity() / 1000 * 512 / 1000
119                         hdd = hd.model() + " (" 
120                         if hd.index & 1:
121                                 hdd += "slave"
122                         else:   
123                                 hdd += "master"
124                         if cap > 0:
125                                 hdd += ", %d,%d GB" % (cap/1024, cap%1024)
126                         hdd += ")"
127
128                         list.append((hdd, hd))
129                 return list