Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / tap / manhole.py
1
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5
6 """
7 I am the support module for making a manhole server with twistd.
8 """
9
10 from twisted.manhole import service
11 from twisted.spread import pb
12 from twisted.python import usage, util
13 from twisted.cred import portal, checkers
14 from twisted.application import strports
15 import os, sys
16
17 class Options(usage.Options):
18     synopsis = "[options]"
19     optParameters = [
20            ["user", "u", "admin", "Name of user to allow to log in"],
21            ["port", "p", str(pb.portno), "Port to listen on"],
22     ]
23
24     optFlags = [
25         ["tracebacks", "T", "Allow tracebacks to be sent over the network"],
26     ]
27
28     compData = usage.Completions(
29         optActions={"user": usage.CompleteUsernames()}
30         )
31
32     def opt_password(self, password):
33         """Required.  '-' will prompt or read a password from stdin.
34         """
35         # If standard input is a terminal, I prompt for a password and
36         # confirm it.  Otherwise, I use the first line from standard
37         # input, stripping off a trailing newline if there is one.
38         if password in ('', '-'):
39             self['password'] = util.getPassword(confirm=1)
40         else:
41             self['password'] = password
42     opt_w = opt_password
43
44     def postOptions(self):
45         if not self.has_key('password'):
46             self.opt_password('-')
47
48 def makeService(config):
49     port, user, password = config['port'], config['user'], config['password']
50     p = portal.Portal(
51         service.Realm(service.Service(config["tracebacks"], config.get('namespace'))),
52         [checkers.InMemoryUsernamePasswordDatabaseDontUse(**{user: password})]
53     )
54     return strports.service(port, pb.PBServerFactory(p, config["tracebacks"]))