import cleanup
[enigma2-plugins.git] / webinterface / src / WebChilds / IPKG.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web2 import resource, stream, responsecode, http
4
5 class IPKGResource(resource.Resource):
6     def render(self, req):
7        self.args = req.args
8        if self.getArg("command") == "update":
9            return self.doUpdate()
10        elif self.getArg("command") == "upgrade":
11            return self.doUpgrade()
12        elif self.getArg("command") == "list_installed":
13            return self.doListInstalled()
14        elif self.getArg("command") == "list":
15            return self.doListAvaiable()
16        elif self.getArg("command") == "search":
17            return self.doSearch()
18        elif self.getArg("command") == "info":
19            return self.doInfo()
20        elif self.getArg("command") == "status":
21            return self.doInfo()
22        elif self.getArg("command") == "install":
23            return self.doInstall()
24        elif self.getArg("command") == "remove":
25            return self.doRemove()
26        elif self.getArg("command"):
27            return self.doErrorPage("no or unknown command")
28        else:
29            return self.doIndexPage()
30
31     def doIndexPage(self):
32         html  = "<html><body>"
33         html += "<h1>Interface to IPKG</h1>"
34         html += "update, ?command=update<br>"
35         html += "upgrade, ?command=upgrade<br>"
36         html += "list_installed, ?command=list_installed<br>"
37         html += "list, ?command=list<br>"
38         html += "search, ?command=search&file=&lt;filename&gt;<br>"
39         html += "info, ?command=search&package=&lt;packagename&gt;<br>"
40         html += "status, ?command=search&package=&lt;packagename&gt;<br>"
41         html += "install, ?command=install&package=&lt;packagename&gt;<br>"
42         html += "remove, ?command=remove&package=&lt;packagename&gt;<br>"
43         html += "</body></html>"
44         return http.Response(responsecode.OK,stream=html)
45
46     def doUpdate(self):
47         return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg update"))
48         
49     def doUpgrade(self):
50         return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg upgrade"))
51         
52     def doListInstalled(self):
53         return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg list_installed"))
54         
55     def doListAvaiable(self):
56         return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg list"))
57
58     def doSearch(self):
59         if self.getArg("file"):
60             return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg search "+self.getArg("file")))
61         else:
62             return self.doErrorPage("no file to search givven")
63
64     def doInfo(self):
65         if self.getArg("package"):
66             return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg info "+self.getArg("package")))
67         else:
68             return self.doErrorPage("no package to print info givven")
69
70     def doStatus(self):
71         if self.getArg("package"):
72             return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg status "+self.getArg("package")))
73         else:
74             return self.doErrorPage("no package to print status givven")
75
76     def doInstall(self):
77         if self.getArg("package"):
78             return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg install "+self.getArg("package")))
79         else:
80             return self.doErrorPage("no package to install givven")
81
82     def doRemove(self):
83         if self.getArg("package"):
84             return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg remove "+self.getArg("package")))
85         else:
86             return self.doErrorPage("no package to remove givven")
87
88     def doListAvaiable(self):
89         return http.Response(responsecode.OK,stream=IPKGConsoleStream("ipkg list"))
90         
91     def doErrorPage(self,errormsg):
92         return http.Response(responsecode.OK,stream=errormsg)
93     
94     def getArg(self,key):
95         if self.args.has_key(key):
96             return self.args[key][0]
97         else:
98             return False
99
100 class IPKGConsoleStream(stream.ProducerStream):
101     def __init__(self,cmd):
102         stream.ProducerStream.__init__(self)
103         self.container = eConsoleAppContainer()
104         self.container.appClosed.get().append(self.cmdFinished)
105         self.container.dataAvail.get().append(self.dataAvail)
106         self.container.execute(cmd)
107
108     def cmdFinished(self,data):
109         self.finish()    
110             
111     def dataAvail(self,data):
112         self.write(data)