X-Git-Url: https://git.mxchange.org/?p=quix0rs-apt-p2p.git;a=blobdiff_plain;f=apt-dht.py;h=b096d383cd831927f39202de7fca3647aa99121e;hp=30b6a93a88d082230e90fbf38467e4dc94dd69f6;hb=004b3242cd6b9933ea049039840f1071b0284ad8;hpb=c599adb68f5e7afefb25375b063e5e7fe02f949c diff --git a/apt-dht.py b/apt-dht.py index 30b6a93..b096d38 100644 --- a/apt-dht.py +++ b/apt-dht.py @@ -16,8 +16,8 @@ from twisted.internet import reactor from twisted.python import usage, log from twisted.web2 import channel -from apt_dht import AptDHT -from apt_dht_conf import config +from apt_dht.apt_dht_conf import config, version, DEFAULT_CONFIG_FILES +from apt_dht.interfaces import IDHT config_file = [] @@ -25,14 +25,15 @@ if __name__ == '__main__': # Parse command line parameters when started on command line class AptDHTOptions(usage.Options): optFlags = [ - ['help', 'h'], + ['help', 'h', 'Print this help message'], ] optParameters = [ - ['config-file', 'c', None, "Configuration file"], + ['config-file', 'c', [], "Configuration file"], + ['log-file', 'l', '-', "File to log to, - for stdout"], ] longdesc="apt-dht is a peer-to-peer downloader for apt users" def opt_version(self): - print "apt-dht 1.9.x" + print "apt-dht %s" % version.short() sys.exit(0) opts = AptDHTOptions() @@ -43,25 +44,42 @@ if __name__ == '__main__': sys.exit(1) config_file = opts.opts['config-file'] + log_file = opts.opts['log-file'] + if log_file == '-': + f = sys.stdout + else: + f = open(log_file, 'w') + log.startLogging(f, setStdout=1) -config.read(config_file) -if config.defaults()['username']: - uid,gid = pwd.getpwnam(config.defaults()['username'])[2:4] +config.read(DEFAULT_CONFIG_FILES + [config_file]) +if config.has_option('DEFAULT', 'username') and config.get('DEFAULT', 'username'): + uid,gid = pwd.getpwnam(config.get('DEFAULT', 'username'))[2:4] else: uid,gid = None,None +log.msg('Starting application') application = service.Application("apt-dht", uid, gid) -print service.IProcess(application).processName -service.IProcess(application).processName = 'apt-dht' +#print service.IProcess(application).processName +#service.IProcess(application).processName = 'apt-dht' -myapp = AptDHT() -site = myapp.getSite() -s = strports.service('tcp:'+config.defaults()['port'], channel.HTTPFactory(site)) -s.setServiceParent(application) +log.msg('Starting DHT') +DHT = __import__(config.get('DEFAULT', 'DHT')+'.DHT', globals(), locals(), ['DHT']) +assert IDHT.implementedBy(DHT.DHT), "You must provide a DHT implementation that implements the IDHT interface." +myDHT = DHT.DHT() + +if not config.getboolean('DEFAULT', 'DHT-only'): + log.msg('Starting main application server') + from apt_dht.apt_dht import AptDHT + myapp = AptDHT(myDHT) + factory = myapp.getHTTPFactory() + s = strports.service('tcp:'+config.get('DEFAULT', 'port'), factory) + s.setServiceParent(application) +else: + myDHT.loadConfig(config, config.get('DEFAULT', 'DHT')) + myDHT.join() if __name__ == '__main__': # Run on command line - log.startLogging(sys.stdout, setStdout=0) service.IServiceCollection(application).privilegedStartService() service.IServiceCollection(application).startService() reactor.run()