Also remove changed cache files during directory scan.
[quix0rs-apt-p2p.git] / apt-dht.py
index a532527f0f457941fd8c0741fdf0bc5705a3dd6f..1d9933b47d39b280dbd205e347c0479d06fb8add 100644 (file)
@@ -16,19 +16,20 @@ from twisted.internet import reactor
 from twisted.python import usage, log
 from twisted.web2 import channel
 
-from apt_dht.apt_dht_conf import config, version
+from apt_dht.apt_dht_conf import config, version, DEFAULT_CONFIG_FILES
 from apt_dht.interfaces import IDHT
 
-config_file = []
+config_file = ''
 
 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', [], "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):
@@ -43,8 +44,16 @@ 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)
+log.msg("Loading config files: '%s'" % "', '".join(DEFAULT_CONFIG_FILES + [config_file]))
+config_read = config.read(DEFAULT_CONFIG_FILES + [config_file])
+log.msg("Successfully loaded config files: '%s'" % "', '".join(config_read))
 if config.has_option('DEFAULT', 'username') and config.get('DEFAULT', 'username'):
     uid,gid = pwd.getpwnam(config.get('DEFAULT', 'username'))[2:4]
 else:
@@ -57,22 +66,22 @@ application = service.Application("apt-dht", uid, gid)
 
 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.")
+assert IDHT.implementedBy(DHT.DHT), "You must provide a DHT implementation that implements the IDHT interface."
 myDHT = DHT.DHT()
-myDHT.loadConfig(config, config.get('DEFAULT', 'DHT'))
-myDHT.join()
 
 if not config.getboolean('DEFAULT', 'DHT-only'):
     log.msg('Starting main application server')
     from apt_dht.apt_dht import AptDHT
     myapp = AptDHT(myDHT)
-    site = myapp.getSite()
-    s = strports.service('tcp:'+config.get('DEFAULT', 'port'), channel.HTTPFactory(site))
+    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()