Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / tap / ftp.py
1 # -*- test-case-name: twisted.test.test_ftp_options -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5
6 """
7 I am the support module for making a ftp server with twistd.
8 """
9
10 from twisted.application import internet
11 from twisted.cred import portal, checkers, strcred
12 from twisted.protocols import ftp
13
14 from twisted.python import usage, deprecate, versions
15
16 import warnings
17
18
19
20 class Options(usage.Options, strcred.AuthOptionMixin):
21     synopsis = """[options].
22     WARNING: This FTP server is probably INSECURE do not use it.
23     """
24     optParameters = [
25         ["port", "p", "2121",              "set the port number"],
26         ["root", "r", "/usr/local/ftp",    "define the root of the ftp-site."],
27         ["userAnonymous", "", "anonymous", "Name of the anonymous user."]
28     ]
29
30     compData = usage.Completions(
31         optActions={"root": usage.CompleteDirs(descr="root of the ftp site")}
32         )
33
34     longdesc = ''
35
36     def __init__(self, *a, **kw):
37         usage.Options.__init__(self, *a, **kw)
38         self.addChecker(checkers.AllowAnonymousAccess())
39
40
41     def opt_password_file(self, filename):
42         """
43         Specify a file containing username:password login info for
44         authenticated connections. (DEPRECATED; see --help-auth instead)
45         """
46         self['password-file'] = filename
47         msg = deprecate.getDeprecationWarningString(
48             self.opt_password_file, versions.Version('Twisted', 11, 1, 0))
49         warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
50         self.addChecker(checkers.FilePasswordDB(filename, cache=True))
51
52
53
54 def makeService(config):
55     f = ftp.FTPFactory()
56
57     r = ftp.FTPRealm(config['root'])
58     p = portal.Portal(r, config.get('credCheckers', []))
59
60     f.tld = config['root']
61     f.userAnonymous = config['userAnonymous']
62     f.portal = p
63     f.protocol = ftp.FTP
64
65     try:
66         portno = int(config['port'])
67     except KeyError:
68         portno = 2121
69     return internet.TCPServer(portno, f)