fix error page on unknown cmd
[enigma2-plugins.git] / webinterface / src / WebChilds / IPKG.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web import server, resource, http
4
5 class IPKGResource(resource.Resource):
6         IPKG_PATH = "/usr/bin/ipkg"
7
8         SIMPLECMDS = ( "list", "list_installed", "update", "upgrade" )
9         PACKAGECMDS = ( "info", "status", "install", "remove" )
10         FILECMDS = ( "search", )
11
12         def render(self, request):
13                 self.args = request.args
14                 self.command = self.getArg("command")
15
16                 if self.command is not None:
17                         if self.command in IPKGResource.SIMPLECMDS:
18                                 return self.execSimpleCmd(request)
19                         elif self.command in IPKGResource.PACKAGECMDS:
20                                 return self.execPackageCmd(request)
21                         elif self.command in IPKGResource.FILECMDS:
22                                 return self.execFileCmd(request)
23                         else:
24                                 return self.doErrorPage(request, "Unknown command: "+ self.command)
25                 else:
26                         return self.doIndexPage(request)
27
28         def buildCmd(self, parms=[]):
29                 cmd = [IPKGResource.IPKG_PATH, "ipkg", self.command] + parms
30                 print "[IPKG.py] cmd: %s" % cmd
31                 return cmd
32
33         def execCmd(self, request, parms=[]):
34                 cmd = self.buildCmd(parms)
35
36                 request.setResponseCode(http.OK)
37                 IPKGConsoleStream(request, cmd)
38
39                 return server.NOT_DONE_YET
40
41         def execSimpleCmd(self, request):
42                  return self.execCmd(request)
43
44         def execPackageCmd(self, request):
45                 package = self.getArg("package")
46                 if package is not None:
47                         return self.execCmd(request, [package])
48                 else:
49                         return self.doErrorPage(request, "Missing parameter: package")
50
51         def execFileCmd(self, request):
52                 file = self.getArg("file")
53                 if file is not None:
54                         return self.execCmd(request, [file])
55
56                 else:
57                         return self.doErrorPage("Missing parameter: file")
58
59         def doIndexPage(self, request):
60                 html = "<html><body>"
61                 html += "<h1>Interface to IPKG</h1>"
62                 html += "update, ?command=update<br>"
63                 html += "upgrade, ?command=upgrade<br>"
64                 html += "list_installed, ?command=list_installed<br>"
65                 html += "list, ?command=list<br>"
66                 html += "search, ?command=search&file=&lt;filename&gt;<br>"
67                 html += "info, ?command=info&package=&lt;packagename&gt;<br>"
68                 html += "status, ?command=status&package=&lt;packagename&gt;<br>"
69                 html += "install, ?command=install&package=&lt;packagename&gt;<br>"
70                 html += "remove, ?command=remove&package=&lt;packagename&gt;<br>"
71                 html += "</body></html>"
72
73                 request.setResponseCode(http.OK)
74                 request.write(html)
75                 request.finish()
76
77                 return server.NOT_DONE_YET
78
79         def doErrorPage(self, request, errormsg):
80                 request.setResponseCode(http.OK)
81                 request.write(errormsg)
82                 request.finish()
83
84                 return server.NOT_DONE_YET
85
86         def getArg(self, key):
87                 if key in self.args:
88                         return self.args[key][0]
89                 else:
90                         return None
91
92 class IPKGConsoleStream:
93         def __init__(self, request, cmd):
94                 self.request = request
95
96                 self.container = eConsoleAppContainer()
97
98                 self.container.dataAvail.append(self.dataAvail)
99                 self.container.appClosed.append(self.cmdFinished)
100
101                 self.container.execute(*cmd)
102
103         def cmdFinished(self, data):
104                 self.request.finish()
105
106         def dataAvail(self, data):
107                 self.request.write(data)
108