WebAdmin update PkgConfList to apt system
[enigma2-plugins.git] / webadmin / src / WebComponents / Sources / PkgConfList.py
1 # -*- coding: utf-8 -*-
2 from Components.Sources.Source import Source
3 from os import popen as os_popen, statvfs as os_statvfs, path as os_path
4 from shutil import move as sh_move
5
6 class PkgConfList(Source):
7         LIST=0
8         SWITCH=1
9         MEM=2
10         
11         def __init__(self, session, func=LIST, wap=False):
12                 Source.__init__(self)
13                 self.func = func
14                 self.wap = wap
15                 self.session = session
16                 self.res = ( False, "Missing or Wrong Argument" )
17                         
18         def handleCommand(self, cmd):
19                 if cmd is not None:
20                         if self.func is self.SWITCH:
21                                 self.res = self.switch(cmd)
22                         if self.func is self.MEM:
23                                 self.res = self.getMem()
24                         elif self.func is self.LIST:
25                                 pass
26                         
27         def switch(self,cmd):
28                 if cmd:
29                         try:
30                                 file = cmd["file"]
31                                 if os_path.exists("/etc/apt/sources.list.d/" + file):
32                                         sh_move("/etc/apt/sources.list.d/" + file, "/etc/apt/sources.list.d/" + file + ".off")
33                                         return (True, file + ".off")
34                                 else:
35                                         sh_move("/etc/apt/sources.list.d/" + file + ".off", "/etc/apt/sources.list.d/" + file)
36                                         return (True, file)
37                         except Exception, e:
38                                 return (False, str(e))
39                         
40         def getMem(self):
41                 try:
42                         stat = os_statvfs("/")
43                 except OSError:
44                         return (False, "-1")
45                 freespace = stat.f_bfree / 1000 * stat.f_bsize / 1000
46                 return (True, '%d' %freespace)
47                         
48         def getList(self):
49                 list = []
50                 files = os_popen("ls /etc/apt/sources.list.d")
51                 for n in files:
52                         file = n[:-1]
53                         if file.endswith(".list") or file.endswith(".off"):
54                                 print "[PkgConfList] file ", file
55                                 text =""
56                                 with open("/etc/apt/sources.list.d/" + file) as f:
57                                         text = f.read()
58                                         print "[PkgConfList] text ",text
59                                         f.close()
60                                 list.append((file, text))
61                 return list
62
63         def getResult(self):
64                 if self.func is not self.LIST:
65                         return self.res
66                 return ( False, "illegal call" )
67
68         result = property(getResult)
69         
70         list = property(getList)
71         lut = {"Name": 0
72                         , "Text": 1
73                 }