partial cleanup of main.py
This commit is contained in:
parent
47ed9da794
commit
adcee874db
1 changed files with 233 additions and 229 deletions
56
src/main.py
56
src/main.py
|
@ -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
|
||||
|
||||
# Load config
|
||||
|
@ -7,19 +21,23 @@ from Config import config
|
|||
# Create necessary files and dirs
|
||||
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.isfile("%s/sites.json" % config.data_dir): 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("{}")
|
||||
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/users.json" % config.data_dir):
|
||||
open("%s/users.json" % config.data_dir, "w").write("{}")
|
||||
|
||||
|
||||
# Setup logging
|
||||
import logging
|
||||
|
||||
if config.action == "main":
|
||||
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)
|
||||
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:
|
||||
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_log = logging.StreamHandler()
|
||||
|
@ -39,9 +57,9 @@ if config.debug:
|
|||
else:
|
||||
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
|
||||
import gevent
|
||||
import time
|
||||
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
|
||||
|
||||
|
||||
|
||||
# Log current config
|
||||
logging.debug("Config: %s" % config)
|
||||
|
@ -50,7 +68,7 @@ logging.debug("Config: %s" % config)
|
|||
# Socks Proxy monkey patch
|
||||
if config.proxy:
|
||||
from util import SocksProxy
|
||||
import urllib2
|
||||
|
||||
logging.info("Patching sockets to socks proxy: %s" % config.proxy)
|
||||
config.fileserver_ip = '127.0.0.1' # Do not accept connections anywhere but localhost
|
||||
SocksProxy.monkeyPath(*config.proxy.split(":"))
|
||||
|
@ -64,7 +82,7 @@ PluginManager.plugin_manager.loadPlugins()
|
|||
# -- Actions --
|
||||
|
||||
@PluginManager.acceptPlugins
|
||||
class Actions:
|
||||
class Actions(object):
|
||||
# Default action: Start serving UiServer and FileServer
|
||||
def main(self):
|
||||
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....")
|
||||
gevent.joinall([gevent.spawn(ui_server.start), gevent.spawn(file_server.start)])
|
||||
|
||||
|
||||
# Site commands
|
||||
|
||||
def siteCreate(self):
|
||||
|
@ -115,7 +132,6 @@ class Actions:
|
|||
|
||||
logging.info("Site created!")
|
||||
|
||||
|
||||
def siteSign(self, address, privatekey=None, inner_path="content.json", publish=False):
|
||||
from Site import Site
|
||||
logging.info("Signing site: %s..." % address)
|
||||
|
@ -128,7 +144,6 @@ class Actions:
|
|||
if succ and publish:
|
||||
self.sitePublish(address, inner_path=inner_path)
|
||||
|
||||
|
||||
def siteVerify(self, address):
|
||||
import time
|
||||
from Site import Site
|
||||
|
@ -152,7 +167,6 @@ class Actions:
|
|||
else:
|
||||
logging.error("[ERROR] Error during verifying site files!")
|
||||
|
||||
|
||||
def dbRebuild(self, address):
|
||||
from Site import Site
|
||||
logging.info("Rebuilding site sql cache: %s..." % address)
|
||||
|
@ -161,7 +175,6 @@ class Actions:
|
|||
site.storage.rebuildDb()
|
||||
logging.info("Done in %.3fs" % (time.time()-s))
|
||||
|
||||
|
||||
def dbQuery(self, address, query):
|
||||
from Site import Site
|
||||
import json
|
||||
|
@ -171,7 +184,6 @@ class Actions:
|
|||
result.append(dict(row))
|
||||
print json.dumps(result, indent=4)
|
||||
|
||||
|
||||
def siteAnnounce(self, address):
|
||||
from Site.Site import Site
|
||||
logging.info("Announcing site %s to tracker..." % address)
|
||||
|
@ -215,10 +227,7 @@ class Actions:
|
|||
else:
|
||||
logging.info("No peers found for this site, sitePublish command only works if you already have peers serving your site")
|
||||
|
||||
|
||||
|
||||
# Crypto commands
|
||||
|
||||
def cryptPrivatekeyToAddress(self, privatekey=None):
|
||||
from Crypt import CryptBitcoin
|
||||
if not privatekey: # If no privatekey in args then ask it now
|
||||
|
@ -227,14 +236,11 @@ class Actions:
|
|||
|
||||
print CryptBitcoin.privatekeyToAddress(privatekey)
|
||||
|
||||
|
||||
def cryptSign(self, message, privatekey):
|
||||
from Crypt import CryptBitcoin
|
||||
print CryptBitcoin.sign(message, privatekey)
|
||||
|
||||
|
||||
# Peer
|
||||
|
||||
def peerPing(self, peer_ip, peer_port=None):
|
||||
if not peer_port:
|
||||
peer_port = config.fileserver_port
|
||||
|
@ -252,7 +258,6 @@ class Actions:
|
|||
print "Response time: %.3fs (crypt: %s)" % (time.time()-s, peer.connection.crypt)
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def peerGetFile(self, peer_ip, peer_port, site, filename):
|
||||
logging.info("Opening a simple connection server")
|
||||
global file_server
|
||||
|
@ -266,7 +271,6 @@ class Actions:
|
|||
print peer.getFile(site, filename).read()
|
||||
print "Response time: %.3fs" % (time.time()-s)
|
||||
|
||||
|
||||
def peerCmd(self, peer_ip, peer_port, cmd, parameters):
|
||||
logging.info("Opening a simple connection server")
|
||||
global file_server
|
||||
|
|
Loading…
Reference in a new issue