service: add sort of servicelist including all required layers
[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 getIndex(self):
25                 return self.index
26
27         def bus(self):
28                 ret = ""
29
30                 if self.index & 2:
31                         ret = "External (CF) - "
32                 else:
33                         ret = "Internal - "
34                 
35                 if self.index & 1:
36                         return ret + "Slave"
37                 else:
38                         return ret + "Master"
39
40         def capacity(self):
41                 procfile = tryOpen(self.prochdx + "capacity")
42                 
43                 if procfile == "":
44                         return ""
45
46                 line = procfile.readline()
47                 procfile.close()
48                 
49                 try:
50                         cap = int(line)
51                 except:
52                         return ""
53                 
54                 cap = cap / 1000 * 512 / 1000
55                 
56                 return "%d.%03d GB" % (cap/1024, cap%1024)
57                                                                 
58         def model(self):
59                 procfile = tryOpen(self.prochdx + "model")
60                 
61                 if procfile == "":
62                         return ""
63
64                 line = procfile.readline()
65                 procfile.close()
66
67                 return line.strip()
68
69         def free(self):
70                 procfile = tryOpen("/proc/mounts")
71                 
72                 if procfile == "":
73                         return -1
74
75                 free = -1
76                 while 1:
77                         line = procfile.readline()
78                         if line == "":
79                                 break
80                         if line.startswith(self.devidex):
81                                 parts = line.strip().split(" ")
82                                 try:
83                                         stat = os.statvfs(parts[1])
84                                 except OSError:
85                                         continue
86                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
87                                 break
88                 procfile.close()
89                 return free             
90
91         def numPartitions(self):
92                 try:
93                         idedir = os.listdir(self.devidex)
94                 except OSError:
95                         return -1
96                 numPart = -1
97                 for filename in idedir:
98                         if filename.startswith("disc"):
99                                 numPart += 1
100                         if filename.startswith("part"):
101                                 numPart += 1
102                 return numPart
103
104         def unmount(self):
105                 cmd = "/bin/umount " + self.devidex + "part*"
106                 os.system(cmd)
107
108         def createPartition(self):
109                 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
110                 sfdisk = os.popen(cmd, "w")
111                 sfdisk.write("0,\n;\n;\n;\ny\n")
112                 sfdisk.close()
113                 return 0
114
115         def mkfs(self):
116                 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
117                 res = os.system(cmd)
118                 return (res >> 8)
119
120         def mount(self):
121                 cmd = "/bin/mount -t ext3 " + self.devidex + "part1 /hdd"
122                 res = os.system(cmd)
123                 return (res >> 8)
124
125         def createMovieFolder(self):
126                 res = os.system("mkdir /hdd/movie")
127                 return (res >> 8)
128                 
129         def initialize(self):
130                 self.unmount()
131
132                 if self.createPartition() != 0:
133                         return -1
134
135                 if self.mkfs() != 0:
136                         return -2
137
138                 if self.mount() != 0:
139                         return -3
140
141                 if self.createMovieFolder() != 0:
142                         return -4
143                 
144                 return 0
145                 
146 def existHDD(num):
147         mediafile = tryOpen(num2prochdx(num) + "media")
148
149         if mediafile == "":
150                 return -1
151
152         line = mediafile.readline()
153         mediafile.close()
154         
155         if line.startswith("disk"):
156                 return 1
157         
158         return -1
159
160 class HarddiskManager:
161         def __init__(self):
162                 hddNum = 0
163                 self.hdd = [ ]
164                 while 1:
165                         if existHDD(hddNum) == 1:
166                                 self.hdd.append(Harddisk(hddNum))
167                         hddNum += 1
168                         
169                         if hddNum > 8:
170                                 break
171
172         def HDDList(self):
173                 list = [ ]
174                 for hd in self.hdd:
175                         hdd = hd.model() + " (" 
176                         hdd += hd.bus()
177                         cap = hd.capacity()     
178                         if cap != "":
179                                 hdd += ", " + cap
180                         hdd += ")"
181                         list.append((hdd, hd))
182
183                 return list
184
185 harddiskmanager = HarddiskManager()
186
187