3 # Load apt-dht application
5 # There are two ways apt-dht can be started:
7 # - twistd will load this file and execute the app
8 # in 'application' variable
10 # - __name__ will be '__main__'
14 from twisted.application import service, internet, app, strports
15 from twisted.internet import reactor
16 from twisted.python import usage, log
17 from twisted.web2 import channel
19 from apt_dht.apt_dht_conf import config, version
20 from apt_dht.interfaces import IDHT
24 if __name__ == '__main__':
25 # Parse command line parameters when started on command line
26 class AptDHTOptions(usage.Options):
28 ['help', 'h', 'Print this help message'],
31 ['config-file', 'c', [], "Configuration file"],
32 ['log-file', 'l', '-', "File to log to, - for stdout"],
34 longdesc="apt-dht is a peer-to-peer downloader for apt users"
35 def opt_version(self):
36 print "apt-dht %s" % version.short()
39 opts = AptDHTOptions()
42 except usage.UsageError, ue:
43 print '%s: %s' % (sys.argv[0], ue)
46 config_file = opts.opts['config-file']
47 log_file = opts.opts['log-file']
51 f = open(log_file, 'w')
52 log.startLogging(f, setStdout=1)
54 config.read(config_file)
55 if config.has_option('DEFAULT', 'username') and config.get('DEFAULT', 'username'):
56 uid,gid = pwd.getpwnam(config.get('DEFAULT', 'username'))[2:4]
60 log.msg('Starting application')
61 application = service.Application("apt-dht", uid, gid)
62 #print service.IProcess(application).processName
63 #service.IProcess(application).processName = 'apt-dht'
65 log.msg('Starting DHT')
66 DHT = __import__(config.get('DEFAULT', 'DHT')+'.DHT', globals(), locals(), ['DHT'])
67 assert IDHT.implementedBy(DHT.DHT), "You must provide a DHT implementation that implements the IDHT interface."
70 if not config.getboolean('DEFAULT', 'DHT-only'):
71 log.msg('Starting main application server')
72 from apt_dht.apt_dht import AptDHT
74 site = myapp.getSite()
75 s = strports.service('tcp:'+config.get('DEFAULT', 'port'), channel.HTTPFactory(site))
76 s.setServiceParent(application)
78 myDHT.loadConfig(config, config.get('DEFAULT', 'DHT'))
81 if __name__ == '__main__':
83 service.IServiceCollection(application).privilegedStartService()
84 service.IServiceCollection(application).startService()