partial cleanup of main.py

This commit is contained in:
Matthew Bell 2015-06-17 22:27:56 +01:00
parent 47ed9da794
commit adcee874db

View file

@ -1,4 +1,18 @@
import os, sys # Included modules
import os
import sys
import time
import urllib2
# Third party modules
import gevent
from gevent import monkey
# ZeroNet modules
import logging
update_after_shutdown = False # If set True then update and restart zeronet after main loop ended update_after_shutdown = False # If set True then update and restart zeronet after main loop ended
# Load config # Load config
@ -7,19 +21,23 @@ from Config import config
# Create necessary files and dirs # Create necessary files and dirs
if not os.path.isdir(config.log_dir): os.mkdir(config.log_dir) if not os.path.isdir(config.log_dir): os.mkdir(config.log_dir)
if not os.path.isdir(config.data_dir): os.mkdir(config.data_dir) if not os.path.isdir(config.data_dir): os.mkdir(config.data_dir)
if not os.path.isfile("%s/sites.json" % config.data_dir): open("%s/sites.json" % config.data_dir, "w").write("{}") if not os.path.isfile("%s/sites.json" % config.data_dir):
if not os.path.isfile("%s/users.json" % config.data_dir): open("%s/users.json" % config.data_dir, "w").write("{}") open("%s/sites.json" % config.data_dir, "w").write("{}")
if not os.path.isfile("%s/users.json" % config.data_dir):
open("%s/users.json" % config.data_dir, "w").write("{}")
# Setup logging # Setup logging
import logging
if config.action == "main": if config.action == "main":
if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate
if os.path.isfile("%s/debug-last.log" % config.log_dir): os.unlink("%s/debug-last.log" % config.log_dir) if os.path.isfile("%s/debug-last.log" % config.log_dir):
os.unlink("%s/debug-last.log" % config.log_dir)
os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir) os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir)
logging.basicConfig(format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s', level=logging.DEBUG, filename="%s/debug.log" % config.log_dir) logging.basicConfig(format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s',
level=logging.DEBUG, filename="%s/debug.log" % config.log_dir)
else: else:
logging.basicConfig(level=logging.DEBUG, stream=open(os.devnull,"w")) # No file logging if action is not main logging.basicConfig(level=logging.DEBUG, stream=open(os.devnull, "w")) # No file logging if action is not main
# Console logger # Console logger
console_log = logging.StreamHandler() console_log = logging.StreamHandler()
@ -39,9 +57,9 @@ if config.debug:
else: else:
console_log.setLevel(logging.INFO) # Display only important info to console console_log.setLevel(logging.INFO) # Display only important info to console
from gevent import monkey; monkey.patch_all(thread=False, ssl=False) # Make time, socket gevent compatible. Not thread: pyfilesystem and system tray icon not compatible, Not ssl: broken in 2.7.9 monkey.patch_all(thread=False, ssl=False) # Make time, socket gevent compatible. Not thread: pyfilesystem and system tray icon not compatible, Not ssl: broken in 2.7.9
import gevent
import time
# Log current config # Log current config
logging.debug("Config: %s" % config) logging.debug("Config: %s" % config)
@ -50,7 +68,7 @@ logging.debug("Config: %s" % config)
# Socks Proxy monkey patch # Socks Proxy monkey patch
if config.proxy: if config.proxy:
from util import SocksProxy from util import SocksProxy
import urllib2
logging.info("Patching sockets to socks proxy: %s" % config.proxy) logging.info("Patching sockets to socks proxy: %s" % config.proxy)
config.fileserver_ip = '127.0.0.1' # Do not accept connections anywhere but localhost config.fileserver_ip = '127.0.0.1' # Do not accept connections anywhere but localhost
SocksProxy.monkeyPath(*config.proxy.split(":")) SocksProxy.monkeyPath(*config.proxy.split(":"))
@ -64,7 +82,7 @@ PluginManager.plugin_manager.loadPlugins()
# -- Actions -- # -- Actions --
@PluginManager.acceptPlugins @PluginManager.acceptPlugins
class Actions: class Actions(object):
# Default action: Start serving UiServer and FileServer # Default action: Start serving UiServer and FileServer
def main(self): def main(self):
logging.info("Version: %s r%s, Python %s, Gevent: %s" % (config.version, config.rev, sys.version, gevent.__version__)) logging.info("Version: %s r%s, Python %s, Gevent: %s" % (config.version, config.rev, sys.version, gevent.__version__))
@ -84,7 +102,6 @@ class Actions:
logging.info("Starting servers....") logging.info("Starting servers....")
gevent.joinall([gevent.spawn(ui_server.start), gevent.spawn(file_server.start)]) gevent.joinall([gevent.spawn(ui_server.start), gevent.spawn(file_server.start)])
# Site commands # Site commands
def siteCreate(self): def siteCreate(self):
@ -115,7 +132,6 @@ class Actions:
logging.info("Site created!") logging.info("Site created!")
def siteSign(self, address, privatekey=None, inner_path="content.json", publish=False): def siteSign(self, address, privatekey=None, inner_path="content.json", publish=False):
from Site import Site from Site import Site
logging.info("Signing site: %s..." % address) logging.info("Signing site: %s..." % address)
@ -128,7 +144,6 @@ class Actions:
if succ and publish: if succ and publish:
self.sitePublish(address, inner_path=inner_path) self.sitePublish(address, inner_path=inner_path)
def siteVerify(self, address): def siteVerify(self, address):
import time import time
from Site import Site from Site import Site
@ -152,7 +167,6 @@ class Actions:
else: else:
logging.error("[ERROR] Error during verifying site files!") logging.error("[ERROR] Error during verifying site files!")
def dbRebuild(self, address): def dbRebuild(self, address):
from Site import Site from Site import Site
logging.info("Rebuilding site sql cache: %s..." % address) logging.info("Rebuilding site sql cache: %s..." % address)
@ -161,7 +175,6 @@ class Actions:
site.storage.rebuildDb() site.storage.rebuildDb()
logging.info("Done in %.3fs" % (time.time()-s)) logging.info("Done in %.3fs" % (time.time()-s))
def dbQuery(self, address, query): def dbQuery(self, address, query):
from Site import Site from Site import Site
import json import json
@ -171,7 +184,6 @@ class Actions:
result.append(dict(row)) result.append(dict(row))
print json.dumps(result, indent=4) print json.dumps(result, indent=4)
def siteAnnounce(self, address): def siteAnnounce(self, address):
from Site.Site import Site from Site.Site import Site
logging.info("Announcing site %s to tracker..." % address) logging.info("Announcing site %s to tracker..." % address)
@ -215,10 +227,7 @@ class Actions:
else: else:
logging.info("No peers found for this site, sitePublish command only works if you already have peers serving your site") logging.info("No peers found for this site, sitePublish command only works if you already have peers serving your site")
# Crypto commands # Crypto commands
def cryptPrivatekeyToAddress(self, privatekey=None): def cryptPrivatekeyToAddress(self, privatekey=None):
from Crypt import CryptBitcoin from Crypt import CryptBitcoin
if not privatekey: # If no privatekey in args then ask it now if not privatekey: # If no privatekey in args then ask it now
@ -227,14 +236,11 @@ class Actions:
print CryptBitcoin.privatekeyToAddress(privatekey) print CryptBitcoin.privatekeyToAddress(privatekey)
def cryptSign(self, message, privatekey): def cryptSign(self, message, privatekey):
from Crypt import CryptBitcoin from Crypt import CryptBitcoin
print CryptBitcoin.sign(message, privatekey) print CryptBitcoin.sign(message, privatekey)
# Peer # Peer
def peerPing(self, peer_ip, peer_port=None): def peerPing(self, peer_ip, peer_port=None):
if not peer_port: if not peer_port:
peer_port = config.fileserver_port peer_port = config.fileserver_port
@ -252,7 +258,6 @@ class Actions:
print "Response time: %.3fs (crypt: %s)" % (time.time()-s, peer.connection.crypt) print "Response time: %.3fs (crypt: %s)" % (time.time()-s, peer.connection.crypt)
time.sleep(1) time.sleep(1)
def peerGetFile(self, peer_ip, peer_port, site, filename): def peerGetFile(self, peer_ip, peer_port, site, filename):
logging.info("Opening a simple connection server") logging.info("Opening a simple connection server")
global file_server global file_server
@ -266,7 +271,6 @@ class Actions:
print peer.getFile(site, filename).read() print peer.getFile(site, filename).read()
print "Response time: %.3fs" % (time.time()-s) print "Response time: %.3fs" % (time.time()-s)
def peerCmd(self, peer_ip, peer_port, cmd, parameters): def peerCmd(self, peer_ip, peer_port, cmd, parameters):
logging.info("Opening a simple connection server") logging.info("Opening a simple connection server")
global file_server global file_server