OLD | NEW |
| (Empty) |
1 # -*- test-case-name: twisted.test.test_twistd -*- | |
2 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. | |
3 # See LICENSE for details. | |
4 | |
5 import warnings | |
6 | |
7 from twisted.python import log, logfile | |
8 from twisted.application import app, service, internet | |
9 from twisted import copyright | |
10 import sys, os | |
11 | |
12 class ServerOptions(app.ServerOptions): | |
13 synopsis = "Usage: twistd [options]" | |
14 | |
15 optFlags = [['nodaemon','n', "(for backwards compatability)."], | |
16 ] | |
17 | |
18 def opt_version(self): | |
19 """Print version information and exit. | |
20 """ | |
21 print 'twistd (the Twisted Windows runner) %s' % copyright.version | |
22 print copyright.copyright | |
23 sys.exit() | |
24 | |
25 | |
26 def _getLogObserver(logfilename): | |
27 """ | |
28 Create and return a suitable log observer for the given configuration. | |
29 | |
30 The observer will go to stdout if C{logfilename} is empty or equal to | |
31 C{"-"}. Otherwise, it will go to a file with that name. | |
32 | |
33 @type logfilename: C{str} | |
34 @param logfilename: The name of the file to which to log, if other than the | |
35 default. | |
36 | |
37 @return: An object suitable to be passed to C{log.addObserver}. | |
38 """ | |
39 if logfilename == '-' or not logfilename: | |
40 logFile = sys.stdout | |
41 else: | |
42 logFile = logfile.LogFile.fromFullPath(logfilename) | |
43 return log.FileLogObserver(logFile).emit | |
44 | |
45 | |
46 def startLogging(*args, **kw): | |
47 warnings.warn( | |
48 """ | |
49 Use ApplicationRunner instead of startLogging." | |
50 """, | |
51 category=PendingDeprecationWarning, | |
52 stacklevel=2) | |
53 observer = _getLogObserver(*args, **kw) | |
54 log.startLoggingWithObserver(observer) | |
55 sys.stdout.flush() | |
56 | |
57 | |
58 class WindowsApplicationRunner(app.ApplicationRunner): | |
59 """ | |
60 An ApplicationRunner which avoids unix-specific things. No | |
61 forking, no PID files, no privileges. | |
62 """ | |
63 def preApplication(self): | |
64 """ | |
65 Do pre-application-creation setup. | |
66 """ | |
67 self.oldstdout = sys.stdout | |
68 self.oldstderr = sys.stderr | |
69 os.chdir(self.config['rundir']) | |
70 | |
71 | |
72 def getLogObserver(self): | |
73 """ | |
74 Override to supply a log observer suitable for Windows based on the | |
75 given arguments. | |
76 """ | |
77 return _getLogObserver(self.config['logfile']) | |
78 | |
79 | |
80 def postApplication(self): | |
81 """ | |
82 Start the application and run the reactor. | |
83 """ | |
84 service.IService(self.application).privilegedStartService() | |
85 app.startApplication(self.application, not self.config['no_save']) | |
86 app.startApplication(internet.TimerService(0.1, lambda:None), 0) | |
87 self.startReactor(None, self.oldstdout, self.oldstderr) | |
88 log.msg("Server Shut Down.") | |
OLD | NEW |