fe63bd302d661505ecdc5b5d158a3f4aa446e70f
[quix0rs-apt-p2p.git] / apt-dht.py
1 #!/usr/bin/env python
2
3 # Load apt-dht application
4 #
5 # There are two ways apt-dht can be started:
6 #  1. twistd -y apt-dht
7 #     - twistd will load this file and execute the app
8 #       in 'application' variable
9 #  2. from command line
10 #     - __name__ will be '__main__'
11
12 import pwd,sys
13
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
18
19 from apt_dht import AptDHT
20 from apt_dht_conf import config, version
21
22 config_file = []
23
24 if __name__ == '__main__':
25     # Parse command line parameters when started on command line
26     class AptDHTOptions(usage.Options):
27         optFlags = [
28             ['help', 'h'],
29             ]
30         optParameters = [
31             ['config-file', 'c', [], "Configuration file"],
32             ]
33         longdesc="apt-dht is a peer-to-peer downloader for apt users"
34         def opt_version(self):
35             print "apt-dht %s" % version.short()
36             sys.exit(0)
37
38     opts = AptDHTOptions()
39     try:
40         opts.parseOptions()
41     except usage.UsageError, ue:
42         print '%s: %s' % (sys.argv[0], ue)
43         sys.exit(1)
44
45     config_file = opts.opts['config-file']
46
47 config.read(config_file)
48 if config.defaults()['username']:
49     uid,gid = pwd.getpwnam(config.defaults()['username'])[2:4]
50 else:
51     uid,gid = None,None
52
53 application = service.Application("apt-dht", uid, gid)
54 print service.IProcess(application).processName
55 service.IProcess(application).processName = 'apt-dht'
56
57 myapp = AptDHT()
58 site = myapp.getSite()
59 s = strports.service('tcp:'+config.defaults()['port'], channel.HTTPFactory(site))
60 s.setServiceParent(application)
61
62 if __name__ == '__main__':
63     # Run on command line
64     log.startLogging(sys.stdout, setStdout=0)
65     service.IServiceCollection(application).privilegedStartService()
66     service.IServiceCollection(application).startService()
67     reactor.run()