-add Networkbrowser and Mountmanager to easy browse network for networkdevices and...
[enigma2-plugins.git] / networkbrowser / src / AutoMount.py
1 # -*- coding: utf-8 -*-\r
2 # for localized messages\r
3 from __init__ import _\r
4 from re import compile as re_compile, search as re_search\r
5 from os import system, popen, path as os_path, listdir, mkdir, rmdir, makedirs\r
6 \r
7 from enigma import eTimer, eConsoleAppContainer\r
8 from Components.Console import Console\r
9 from Components.Harddisk import harddiskmanager                 #global harddiskmanager\r
10 \r
11 from xml.etree.cElementTree import parse as cet_parse\r
12 \r
13 XML_FSTAB = "/etc/enigma2/automounts.xml"\r
14 \r
15 class AutoMount():\r
16         """Manages Mounts declared in a XML-Document."""\r
17         def __init__(self):\r
18                 self.automounts = {}\r
19                 self.restartConsole = Console()\r
20                 self.MountConsole = Console()\r
21                 self.removeConsole = Console()\r
22                 self.activeMountsCounter = 0\r
23                 # Initialize Timer\r
24                 self.callback = None\r
25                 self.timer = eTimer()\r
26                 self.timer.callback.append(self.mountTimeout)\r
27 \r
28 \r
29                 self.getAutoMountPoints()\r
30 \r
31         def getAutoMountPoints(self, callback = None):\r
32                 # Initialize mounts to empty list\r
33                 automounts = []\r
34                 self.automounts = {}\r
35                 self.activeMountsCounter = 0\r
36         \r
37                 if not os_path.exists(XML_FSTAB):\r
38                         return\r
39                 tree = cet_parse(XML_FSTAB).getroot()\r
40 \r
41                 def getValue(definitions, default):\r
42                         # Initialize Output\r
43                         ret = ""\r
44                         # How many definitions are present\r
45                         Len = len(definitions)\r
46                         return Len > 0 and definitions[Len-1].text or default\r
47                 # Config is stored in "mountmanager" element\r
48                 # Read out NFS Mounts\r
49                 for nfs in tree.findall("nfs"):\r
50                         for mount in nfs.findall("mount"):\r
51                                 data = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, 'username': False, 'password': False, 'mounttype' : False, 'options' : False }            \r
52                                 try:\r
53                                         data['mounttype'] = 'nfs'.encode("UTF-8")\r
54                                         data['active'] = getValue(mount.findall("active"), False).encode("UTF-8")\r
55                                         if data["active"] == 'True' or data["active"] == True:\r
56                                                 self.activeMountsCounter +=1\r
57                                         data['ip'] = getValue(mount.findall("ip"), "192.168.0.0").encode("UTF-8")\r
58                                         data['sharedir'] = getValue(mount.findall("sharedir"), "/exports/").encode("UTF-8")\r
59                                         data['sharename'] = getValue(mount.findall("sharename"), "MEDIA").encode("UTF-8")\r
60                                         data['options'] = getValue(mount.findall("options"), "rw,nolock").encode("UTF-8")\r
61                                         print "NFSMOUNT",data\r
62                                         self.automounts[data['sharename']] = data \r
63                                 except Exception, e:\r
64                                         print "[MountManager] Error reading Mounts:", e\r
65                         # Read out CIFS Mounts\r
66                 for nfs in tree.findall("cifs"):\r
67                         for mount in nfs.findall("mount"):\r
68                                 data = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, 'username': False, 'password': False, 'mounttype' : False, 'options' : False }            \r
69                                 try:\r
70                                         data['mounttype'] = 'cifs'.encode("UTF-8")\r
71                                         data['active'] = getValue(mount.findall("active"), False).encode("UTF-8")\r
72                                         if data["active"] == 'True' or data["active"] == True:\r
73                                                 self.activeMountsCounter +=1\r
74                                         data['ip'] = getValue(mount.findall("ip"), "192.168.0.0").encode("UTF-8")\r
75                                         data['sharedir'] = getValue(mount.findall("sharedir"), "/exports/").encode("UTF-8")\r
76                                         data['sharename'] = getValue(mount.findall("sharename"), "MEDIA").encode("UTF-8")\r
77                                         data['options'] = getValue(mount.findall("options"), "rw,nolock").encode("UTF-8")\r
78                                         data['username'] = getValue(mount.findall("username"), "guest").encode("UTF-8")\r
79                                         data['password'] = getValue(mount.findall("password"), "").encode("UTF-8")\r
80                                         print "CIFSMOUNT",data\r
81                                         self.automounts[data['sharename']] = data \r
82                                 except Exception, e:\r
83                                         print "[MountManager] Error reading Mounts:", e\r
84 \r
85                 print "[AutoMount.py] -getAutoMountPoints:self.automounts -->",self.automounts\r
86                 if len(self.automounts) == 0:\r
87                         print "[AutoMount.py]  self.automounts without mounts",self.automounts\r
88                         if callback is not None:\r
89                                 callback(True)\r
90                 else:\r
91                         for sharename, sharedata in self.automounts.items():\r
92                                 self.CheckMountPoint(sharedata, callback)                       \r
93 \r
94         def CheckMountPoint(self, data, callback):\r
95                 print "[AutoMount.py] CheckMountPoint"\r
96                 print "[AutoMount.py] activeMounts:--->",self.activeMountsCounter\r
97                 if not self.MountConsole:\r
98                         self.MountConsole = Console()\r
99                 \r
100                 self.command = None\r
101                 if self.activeMountsCounter == 0:\r
102                         print "self.automounts without active mounts",self.automounts\r
103                         if data['active'] == 'False' or data['active'] is False:\r
104                                 path = '/media/net/'+ data['sharename']\r
105                                 umountcmd = 'umount -f '+ path\r
106                                 print "[AutoMount.py] UMOUNT-CMD--->",umountcmd\r
107                                 self.MountConsole.ePopen(umountcmd, self.CheckMountPointFinished, [data, callback])\r
108                 else:\r
109                         if data['active'] == 'False' or data['active'] is False:\r
110                                 path = '/media/net/'+ data['sharename']\r
111                                 self.command = 'umount -f '+ path\r
112                                 \r
113                         if data['active'] == 'True' or data['active'] is True:\r
114                                 path = '/media/net/'+ data['sharename']\r
115                                 if os_path.exists(path) is False:\r
116                                         mkdir(path)\r
117                                 tmpsharedir = data['sharedir'].replace(" ", "\\ ")\r
118                                 if tmpsharedir[-1:] == "$":\r
119                                         tmpdir = tmpsharedir.replace("$", "\\$")\r
120                                         tmpsharedir = tmpdir\r
121                                         \r
122                                 if data['mounttype'] == 'nfs':\r
123                                         if not os_path.ismount(path):\r
124                                                 tmpcmd = 'mount -t nfs -o tcp,'+ data['options'] +',rsize=8192,wsize=8192 ' + data['ip'] + ':' + tmpsharedir + ' ' + path\r
125                                                 self.command = tmpcmd.encode("UTF-8")\r
126                                                 \r
127                                 if data['mounttype'] == 'cifs':\r
128                                         if not os_path.ismount(path):\r
129                                                 tmpusername = data['username'].replace(" ", "\\ ")\r
130                                                 tmpcmd = 'mount -t cifs -o '+ data['options'] +',iocharset=utf8,rsize=8192,wsize=8192,username='+ tmpusername + ',password='+ data['password'] + ' //' + data['ip'] + '/' + tmpsharedir + ' ' + path\r
131                                                 self.command = tmpcmd.encode("UTF-8")\r
132 \r
133                         if self.command is not None:\r
134                                 print "[AutoMount.py] U/MOUNTCMD--->",self.command\r
135                                 self.MountConsole.ePopen(self.command, self.CheckMountPointFinished, [data, callback])\r
136                         else:\r
137                                 self.CheckMountPointFinished(None,None, [data, callback])\r
138                                         \r
139         def CheckMountPointFinished(self, result, retval, extra_args):\r
140                 print "[AutoMount.py] CheckMountPointFinished"\r
141                 print "[AutoMount.py] result",result\r
142                 print "[AutoMount.py] retval",retval\r
143                 (data, callback ) = extra_args\r
144                 print "LEN",len(self.MountConsole.appContainers)\r
145                 path = '/media/net/'+ data['sharename']\r
146                 print "PATH im CheckMountPointFinished",path\r
147                 if os_path.exists(path):\r
148                         if os_path.ismount(path):\r
149                                 if self.automounts.has_key(data['sharename']):\r
150                                         self.automounts[data['sharename']]['isMounted'] = True\r
151                                         desc = data['sharename']\r
152                                         harddiskmanager.addMountedPartition(path, desc)\r
153                         else:\r
154                                 if self.automounts.has_key(data['sharename']):\r
155                                         self.automounts[data['sharename']]['isMounted'] = False\r
156                                 if os_path.exists(path):\r
157                                         if not os_path.ismount(path):\r
158                                                 rmdir(path)\r
159                                                 harddiskmanager.removeMountedPartition(path)\r
160                                 \r
161                 if self.MountConsole:\r
162                         if len(self.MountConsole.appContainers) == 0:\r
163                                 if callback is not None:\r
164                                         self.callback = callback\r
165                                         self.timer.startLongTimer(10)\r
166 \r
167         def mountTimeout(self):\r
168                 self.timer.stop()\r
169                 if self.MountConsole:\r
170                         if len(self.MountConsole.appContainers) == 0:           \r
171                                 print "self.automounts after mounting",self.automounts\r
172                                 if self.callback is not None:\r
173                                         self.callback(True)\r
174 \r
175         def getMountsList(self):\r
176                 return self.automounts\r
177 \r
178         def getMountsAttribute(self, mountpoint, attribute):\r
179                 if self.automounts.has_key(mountpoint):\r
180                         if self.automounts[mountpoint].has_key(attribute):\r
181                                 return self.automounts[mountpoint][attribute]\r
182                 return None\r
183 \r
184         def setMountsAttribute(self, mountpoint, attribute, value):\r
185                 print "setting for mountpoint", mountpoint, "attribute", attribute, " to value", value\r
186                 if self.automounts.has_key(mountpoint):\r
187                         self.automounts[mountpoint][attribute] = value\r
188 \r
189         def writeMountsConfig(self):\r
190                 # Generate List in RAM\r
191                 list = ['<?xml version="1.0" ?>\n<mountmanager>\n']\r
192 \r
193                 for sharename, sharedata in self.automounts.items():\r
194                         if sharedata['mounttype'] == 'nfs':\r
195                                 list.append('<nfs>\n')\r
196                                 list.append(' <mount>\n')\r
197                                 list.append(''.join(["  <active>", str(sharedata['active']), "</active>\n"]))\r
198                                 list.append(''.join(["  <ip>", sharedata['ip'], "</ip>\n"]))\r
199                                 list.append(''.join(["  <sharename>", sharedata['sharename'], "</sharename>\n"]))\r
200                                 list.append(''.join(["  <sharedir>", sharedata['sharedir'], "</sharedir>\n"]))\r
201                                 list.append(''.join(["  <options>", sharedata['options'], "</options>\n"]))\r
202                                 list.append(' </mount>\n')\r
203                                 list.append('</nfs>\n')\r
204 \r
205                         if sharedata['mounttype'] == 'cifs':\r
206                                 list.append('<cifs>\n')\r
207                                 list.append(' <mount>\n')\r
208                                 list.append(''.join(["  <active>", str(sharedata['active']), "</active>\n"]))\r
209                                 list.append(''.join(["  <ip>", sharedata['ip'], "</ip>\n"]))\r
210                                 list.append(''.join(["  <sharename>", sharedata['sharename'], "</sharename>\n"]))\r
211                                 list.append(''.join(["  <sharedir>", sharedata['sharedir'], "</sharedir>\n"]))\r
212                                 list.append(''.join(["  <options>", sharedata['options'], "</options>\n"]))\r
213                                 list.append(''.join(["  <username>", sharedata['username'], "</username>\n"]))\r
214                                 list.append(''.join(["  <password>", sharedata['password'], "</password>\n"]))\r
215                                 list.append(' </mount>\n')\r
216                                 list.append('</cifs>\n')\r
217 \r
218                 # Close Mountmanager Tag\r
219                 list.append('</mountmanager>\n')\r
220 \r
221                 # Try Saving to Flash\r
222                 file = None\r
223                 try:\r
224                         file = open(XML_FSTAB, "w")\r
225                         file.writelines(list)\r
226                 except Exception, e:\r
227                         print "[AutoMount.py] Error Saving Mounts List:", e\r
228                 finally:\r
229                         if file is not None:\r
230                                 file.close()\r
231 \r
232         def stopMountConsole(self):\r
233                 if self.MountConsole is not None:\r
234                         self.MountConsole = None\r
235 \r
236         def removeMount(self, mountpoint, callback = None):\r
237                 print "[AutoMount.py] removing mount: ",mountpoint\r
238                 self.newautomounts = {}\r
239                 for sharename, sharedata in self.automounts.items():\r
240                         if sharename is not mountpoint.strip():\r
241                                 self.newautomounts[sharename] = sharedata\r
242                 self.automounts.clear()\r
243                 self.automounts = self.newautomounts\r
244                 if not self.removeConsole:\r
245                         self.removeConsole = Console()\r
246                 path = '/media/net/'+ mountpoint\r
247                 umountcmd = 'umount -f '+ path\r
248                 print "[AutoMount.py] UMOUNT-CMD--->",umountcmd\r
249                 self.removeConsole.ePopen(umountcmd, self.removeMountPointFinished, [path, callback])\r
250 \r
251         def removeMountPointFinished(self, result, retval, extra_args):\r
252                 print "[AutoMount.py] removeMountPointFinished"\r
253                 print "[AutoMount.py] result",result\r
254                 print "[AutoMount.py] retval",retval\r
255                 (path, callback ) = extra_args\r
256                 if os_path.exists(path):\r
257                         if not os_path.ismount(path):\r
258                                 rmdir(path)\r
259                                 harddiskmanager.removeMountedPartition(path)\r
260 \r
261                 if self.removeConsole:\r
262                         if len(self.removeConsole.appContainers) == 0:\r
263                                 if callback is not None:\r
264                                         self.callback = callback\r
265                                         self.timer.startLongTimer(10)\r
266 \r
267 \r
268 # currently unused autofs support stuff\r
269 """\r
270 class AutoMount_Unused:\r
271         def __init__(self):\r
272                 self.automounts = {}\r
273                 self.restartConsole = Console()\r
274                 self.MountConsole = Console()\r
275                 self.activeMountsCounter = 0\r
276                 self.getAutoMountPoints()\r
277                 \r
278         # helper function\r
279         def regExpMatch(self, pattern, string):\r
280                 if string is None:\r
281                         return None\r
282                 try:\r
283                         return pattern.search(string).group()\r
284                 except AttributeError:\r
285                         None\r
286 \r
287         # helper function to convert ips from a sring to a list of ints\r
288         def convertIP(self, ip):\r
289                 strIP = ip.split('.')\r
290                 ip = []\r
291                 for x in strIP:\r
292                         ip.append(int(x))\r
293                 return ip\r
294 \r
295 \r
296         def getAutoMountPoints(self, callback = None):\r
297                 print "[AutoMount.py] getAutoMountPoints"\r
298                 automounts = []\r
299                 self.automounts = {}\r
300                 self.activeMountsCounter = 0\r
301                 fp = None\r
302                 try:\r
303                         fp = file('/etc/auto.network', 'r')\r
304                         automounts = fp.readlines()\r
305                         fp.close()\r
306                 except:\r
307                         print "[AutoMount.py] /etc/auto.network - opening failed"\r
308                         \r
309                 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'\r
310                 cifsIpLinePattern = re_compile('://' + ipRegexp + '/')\r
311                 nfsIpLinePattern = re_compile(ipRegexp + ':/')\r
312                 ipPattern = re_compile(ipRegexp)\r
313                 for line in automounts:\r
314                         print "[AutoMount.py] Line:",line\r
315                         split = line.strip().split('\t',2)\r
316                         if split[0] == '*':\r
317                                 continue\r
318                         if len(split) == 2 and split[0][0] == '*':\r
319                                 continue\r
320                         if len(split) == 3:\r
321                                 data = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, 'username': False, 'password': False, 'mounttype' : False, 'options' : False }            \r
322                                 currshare = ""\r
323                                 if split[0][0] == '#':\r
324                                         data['active'] = False\r
325                                         currshare = split[0][1:]\r
326                                         data['sharename'] = currshare\r
327                                 else:\r
328                                         data['active'] = True\r
329                                         self.activeMountsCounter +=1\r
330                                         currshare = split[0]\r
331                                         data['sharename'] = currshare        \r
332                                 if '-fstype=cifs' in split[1]:\r
333                                         data['mounttype'] = 'cifs'\r
334                                         options = split[1][split[1].index('-fstype=cifs')+13 : split[1].index(',user=')]\r
335                                         if options is not None:\r
336                                                 data['options'] = options\r
337                                         if 'user=' in split[1]:\r
338                                                 username = split[1][split[1].index(',user=')+6 : split[1].index(',pass=')]\r
339                                                 if username is not None:\r
340                                                         data['username'] = username\r
341                                         if 'pass=' in split[1]:\r
342                                                 password = split[1][split[1].index(',pass=')+6 : ]\r
343                                                 if password is not None:\r
344                                                         data['password'] = password\r
345                                         ip = self.regExpMatch(ipPattern, self.regExpMatch(cifsIpLinePattern, split[2]))\r
346                                         if ip is not None:\r
347                                                 data['ip'] = ip\r
348                                         sharedir = split[2][split[2].index(ip)+len(ip)+1 : ]\r
349                                         if sharedir is not None:\r
350                                                 tmpsharedir = sharedir.replace("\\ ", " ")\r
351                                                 if tmpsharedir[-2:] == "\$":\r
352                                                         tmpdir = tmpsharedir.replace("\$", "$")\r
353                                                         tmpsharedir = tmpdir\r
354                                                 data['sharedir'] = tmpsharedir\r
355                                                 \r
356                                 if '-fstype=nfs' in split[1]:\r
357                                         data['mounttype'] = 'nfs'\r
358                                         options = split[1][split[1].index('-fstype=nfs')+12 : ]\r
359                                         if options is not None:\r
360                                                 data['options'] = options\r
361                                         ip = self.regExpMatch(ipPattern, self.regExpMatch(nfsIpLinePattern, split[2]))\r
362                                         if ip is not None:\r
363                                                 data['ip'] = ip\r
364                                         sharedir = split[2][split[2].index(ip)+len(ip)+1 : ]\r
365                                         if sharedir is not None:\r
366                                                 tmpsharedir = sharedir.replace("\\ ", " ")\r
367                                                 if tmpsharedir[-2:] == "\$":\r
368                                                         tmpdir = tmpsharedir.replace("\$", "$")\r
369                                                         tmpsharedir = tmpdir\r
370                                                 data['sharedir'] = tmpsharedir\r
371                                                 \r
372                                 self.automounts[currshare] = data                \r
373                 if len(self.automounts) == 0:\r
374                         print "[AutoMount.py]  self.automounts without mounts",self.automounts\r
375                         if callback is not None:\r
376                                 callback(True)\r
377                 else:\r
378                         #print "automounts",self.automounts\r
379                         for sharename, sharedata in self.automounts.items():\r
380                                 self.CheckMountPoint(sharedata, callback)                       \r
381 \r
382         def CheckMountPoint(self, data, callback):\r
383                 print "[AutoMount.py] CheckMountPoint"\r
384                 if not self.MountConsole:\r
385                         self.MountConsole = Console()\r
386                 print "[AutoMount.py] activeMounts--->",self.activeMountsCounter\r
387                 if self.activeMountsCounter == 0:\r
388                 #if data['active'] is False:\r
389                         if self.MountConsole:\r
390                                 if len(self.MountConsole.appContainers) == 0:           \r
391                                         print "self.automounts without active mounts",self.automounts\r
392                                         if callback is not None:\r
393                                                 callback(True)\r
394                 else:\r
395                         if data['mounttype'] == 'nfs' and data['active'] is True:\r
396                                 path = '/tmp/'+ data['sharename']\r
397                                 if os_path.exists(path) is False:\r
398                                         mkdir(path)\r
399                                 tmpsharedir = data['sharedir'].replace(" ", "\\ ")\r
400                                 if tmpsharedir[-1:] == "$":\r
401                                         tmpdir = tmpsharedir.replace("$", "\\$")\r
402                                         tmpsharedir = tmpdir\r
403                                 nfscmd = 'mount -o nolock -t nfs ' + data['ip'] + ':' + tmpsharedir + ' ' + path\r
404                                 print "[AutoMount.py] nfsscmd--->",nfscmd\r
405                                 self.MountConsole.ePopen(nfscmd, self.CheckMountPointFinished, [data, callback])\r
406                         if data['mounttype'] == 'cifs' and data['active'] is True:\r
407                                 self.activeMountsCounter +=1\r
408                                 path = '/tmp/'+ data['sharename']\r
409                                 if os_path.exists(path) is False:\r
410                                         mkdir(path)\r
411                                 tmpsharedir = data['sharedir'].replace(" ", "\\ ")\r
412                                 if tmpsharedir[-1:] == "$":\r
413                                         tmpdir = tmpsharedir.replace("$", "\\$")\r
414                                         tmpsharedir = tmpdir\r
415                                 cifscmd = 'mount -t cifs -o rw,username='+ data['username'] + ',password='+ data['password'] + ' //' + data['ip'] + '/' + tmpsharedir + ' ' + path\r
416                                 print "[AutoMount.py] cifscmd--->",cifscmd\r
417                                 self.MountConsole.ePopen(cifscmd, self.CheckMountPointFinished, [data, callback])\r
418 \r
419 \r
420                                         \r
421         def CheckMountPointFinished(self, result, retval, extra_args):\r
422                 print "[AutoMount.py] CheckMountPointFinished"\r
423                 (data, callback ) = extra_args\r
424                 path = '/tmp/'+ data['sharename']\r
425                 if not self.MountConsole:\r
426                         self.MountConsole = Console()\r
427                 if os_path.ismount(path):\r
428                         if self.automounts.has_key(data['sharename']):\r
429                                 self.automounts[data['sharename']]['isMounted'] = True\r
430                 else:\r
431                         if self.automounts.has_key(data['sharename']):\r
432                                 self.automounts[data['sharename']]['isMounted'] = False\r
433                 umountcmd = 'umount /tmp/'+ data['sharename']\r
434                 self.MountConsole.ePopen(umountcmd, self.CleanMountPointFinished, [data, callback])                             \r
435 \r
436         def CleanMountPointFinished(self, result, retval, extra_args):\r
437                 print "[AutoMount.py] CleanMountPointFinished"\r
438                 (data, callback ) = extra_args\r
439                 path = '/tmp/'+ data['sharename']\r
440                 if os_path.exists(path):\r
441                         rmdir(path)\r
442                 if self.MountConsole:\r
443                         if len(self.MountConsole.appContainers) == 0:           \r
444                                 print "self.automounts after mountcheck",self.automounts\r
445                                 if callback is not None:\r
446                                         callback(True)\r
447 \r
448         def getMountsList(self):\r
449                 return self.automounts\r
450 \r
451         def getMountsAttribute(self, mountpoint, attribute):\r
452                 if self.automounts.has_key(mountpoint):\r
453                         if self.automounts[mountpoint].has_key(attribute):\r
454                                 return self.automounts[mountpoint][attribute]\r
455                 return None\r
456 \r
457         def setMountsAttribute(self, mountpoint, attribute, value):\r
458                 print "setting for mountpoint", mountpoint, "attribute", attribute, " to value", value\r
459                 if self.automounts.has_key(mountpoint):\r
460                         self.automounts[mountpoint][attribute] = value\r
461 \r
462         def removeMount(self, mountpoint):\r
463                 self.newautomounts = {}\r
464                 #self.automounts[currshare] = data\r
465                 print "[AutoMount.py] removing mount: ",mountpoint\r
466                 for sharename, sharedata in self.automounts.items():\r
467                         if sharename is not mountpoint.strip():\r
468                                 self.newautomounts[sharename] = sharedata\r
469                 self.automounts.clear()\r
470                 self.automounts = self.newautomounts\r
471 \r
472         def writeMountsConfig(self):\r
473                 fp = file('/etc/auto.network', 'w')\r
474                 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")\r
475                 for sharename, sharedata in self.automounts.items():\r
476                         if sharedata['mounttype'] == 'nfs':\r
477                                 if sharedata['active'] is False:\r
478                                         fp.write("#" + sharedata['sharename'] + "\t")\r
479                                 else:\r
480                                         fp.write( sharedata['sharename'] + "\t")\r
481                                 fp.write( "-fstype=nfs," + sharedata['options'] + "\t")\r
482                                 fp.write(sharedata['ip'] + ":")\r
483                                 tmpsharedir = sharedata['sharedir'].replace(" ", "\\ ")\r
484                                 if tmpsharedir[-1:] == "$":\r
485                                         tmpdir = tmpsharedir.replace("$", "\\$")\r
486                                         tmpsharedir = tmpdir\r
487                                 fp.write( tmpsharedir + "\n")\r
488                         if sharedata['mounttype'] == 'cifs':\r
489                                 if sharedata['active'] is False:\r
490                                         fp.write("#" + sharedata['sharename'] + "\t")\r
491                                 else:\r
492                                         fp.write( sharedata['sharename'] + "\t")\r
493                                 fp.write( "-fstype=cifs," + sharedata['options'] + ",")\r
494                                 fp.write( "user=" + sharedata['username'] + ",")\r
495                                 fp.write( "pass=" + sharedata['password'] + "\t://")\r
496                                 fp.write(sharedata['ip'] + "/")\r
497                                 tmpsharedir = sharedata['sharedir'].replace(" ", "\\ ")\r
498                                 if tmpsharedir[-1:] == "$":\r
499                                         tmpdir = tmpsharedir.replace("$", "\\$")\r
500                                         tmpsharedir = tmpdir\r
501                                 fp.write( tmpsharedir + "\n")\r
502                 fp.write("\n")                          \r
503                 fp.close()\r
504 \r
505         def restartAutoFS(self,callback = None):\r
506                 print "[AutoMount.py] restartAutoFS "\r
507                 self.restartConsole = Console()\r
508                 self.commands = []\r
509                 self.commands.append("/etc/init.d/autofs stop")\r
510                 self.commands.append("killall -9 automount")\r
511                 self.commands.append("rm -rf /var/run/autofs")\r
512                 self.commands.append("/etc/init.d/autofs start")\r
513                 self.restartConsole.eBatch(self.commands, self.restartAutoFSFinished, callback, debug=True)\r
514         \r
515         def restartAutoFSFinished(self,extra_args):\r
516                 print "[AutoMount.py] restartAutoFSFinished "\r
517                 ( callback ) = extra_args\r
518                 if callback is not None:\r
519                         callback(True)\r
520 \r
521         def stopMountConsole(self):\r
522                 if self.MountConsole is not None:\r
523                         self.MountConsole = None """\r
524 \r
525 iAutoMount = AutoMount()\r
526 \r