summaryrefslogtreecommitdiffstats
path: root/pyLoadCore.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyLoadCore.py')
-rwxr-xr-xpyLoadCore.py149
1 files changed, 72 insertions, 77 deletions
diff --git a/pyLoadCore.py b/pyLoadCore.py
index 35cac4682..e79da3fc3 100755
--- a/pyLoadCore.py
+++ b/pyLoadCore.py
@@ -20,31 +20,34 @@
@author: mkaay
@version: v0.4.9
"""
-CURRENT_VERSION = '0.4.9'
+CURRENT_VERSION = '0.4.9.9-dev'
import __builtin__
from getopt import getopt, GetoptError
-import module.common.pylgettext as gettext
from imp import find_module
import logging
import logging.handlers
import os
-from os import _exit, execl, getcwd, makedirs, remove, sep, walk, chdir, close
-from os.path import exists, join
+from os import _exit, execl, getcwd, remove, walk, chdir, close
import signal
-import subprocess
import sys
from sys import argv, executable, exit
from time import time, sleep
from traceback import print_exc
+import locale
+locale.locale_alias = locale.windows_locale = {} #save ~100kb ram, no known sideeffects for now
+
+import subprocess
+subprocess.__doc__ = None # the module with the largest doc we are using
+
from module import InitHomeDir
from module.plugins.AccountManager import AccountManager
-from module.CaptchaManager import CaptchaManager
-from module.ConfigParser import ConfigParser
+from module.interaction.CaptchaManager import CaptchaManager
+from module.config.ConfigParser import ConfigParser
from module.plugins.PluginManager import PluginManager
-from module.PullEvents import PullManager
+from module.interaction.EventManager import EventManager
from module.network.RequestFactory import RequestFactory
from module.web.ServerThread import WebServer
from module.Scheduler import Scheduler
@@ -53,11 +56,17 @@ from module import remote
from module.remote.RemoteManager import RemoteManager
from module.database import DatabaseBackend, FileHandler
-from module.utils import freeSpace, formatSize, get_console_encoding
+import module.common.pylgettext as gettext
+from module.utils import formatSize, get_console_encoding
+from module.utils.fs import free_space, exists, makedirs, join
from codecs import getwriter
-enc = get_console_encoding(sys.stdout.encoding)
+# test runner overwrites sys.stdout
+if hasattr(sys.stdout, "encoding"): enc = get_console_encoding(sys.stdout.encoding)
+else: enc = "utf8"
+
+sys._stdout = sys.stdout
sys.stdout = getwriter(enc)(sys.stdout, errors="replace")
# TODO List
@@ -73,6 +82,7 @@ class Core(object):
self.running = False
self.daemon = False
self.remote = True
+ self.pdb = None
self.arg_links = []
self.pidfile = "pyload.pid"
self.deleteLinks = False # will delete links on startup
@@ -129,7 +139,7 @@ class Core(object):
print pid
exit(0)
else:
- print "false"
+ print "false"
exit(1)
elif option == "--clean":
self.cleanTree()
@@ -144,7 +154,7 @@ class Core(object):
def print_help(self):
print ""
- print "pyLoad v%s 2008-2011 the pyLoad Team" % CURRENT_VERSION
+ print "pyLoad v%s 2008-2012 the pyLoad Team" % CURRENT_VERSION
print ""
if sys.argv[0].endswith(".py"):
print "Usage: python pyLoadCore.py [options]"
@@ -258,12 +268,12 @@ class Core(object):
print join(path, f)
remove(join(path, f))
- def start(self, rpc=True, web=True):
+ def start(self, rpc=True, web=True, tests=False):
""" starts the fun :D """
self.version = CURRENT_VERSION
- if not exists("pyload.conf"):
+ if not exists("pyload.conf") and not tests:
from module.setup import Setup
print "This is your first start, running configuration assistent now."
@@ -295,11 +305,15 @@ class Core(object):
languages=[self.config['general']['language'],"en"],fallback=True)
translation.install(True)
+ # load again so translations are propagated
+ self.config.loadDefault()
+
self.debug = self.doDebug or self.config['general']['debug_mode']
self.remote &= self.config['remote']['activated']
pid = self.isAlreadyRunning()
- if pid:
+ # dont exit when in test runner
+ if pid and not tests:
print _("pyLoad already running with pid %s") % pid
exit()
@@ -326,8 +340,6 @@ class Core(object):
except Exception, e:
print _("Failed changing user: %s") % e
- self.check_file(self.config['log']['log_folder'], _("folder for logs"), True)
-
if self.debug:
self.init_logger(logging.DEBUG) # logging level
else:
@@ -339,8 +351,9 @@ class Core(object):
self.log.info(_("Starting") + " pyLoad %s" % CURRENT_VERSION)
self.log.info(_("Using home directory: %s") % getcwd())
-
- self.writePidFile()
+
+ if not tests:
+ self.writePidFile()
#@TODO refractor
@@ -348,24 +361,15 @@ class Core(object):
self.log.debug("Remote activated: %s" % self.remote)
self.check_install("Crypto", _("pycrypto to decode container files"))
- #img = self.check_install("Image", _("Python Image Libary (PIL) for captcha reading"))
- #self.check_install("pycurl", _("pycurl to download any files"), True, True)
- self.check_file("tmp", _("folder for temporary files"), True)
- #tesser = self.check_install("tesseract", _("tesseract for captcha reading"), False) if os.name != "nt" else True
self.captcha = True # checks seems to fail, althoug tesseract is available
- self.check_file(self.config['general']['download_folder'], _("folder for downloads"), True)
-
if self.config['ssl']['activated']:
self.check_install("OpenSSL", _("OpenSSL for secure connection"))
- self.setupDB()
- if self.config.oldRemoteData:
- self.log.info(_("Moving old user config to DB"))
- self.db.addUser(self.config.oldRemoteData["username"], self.config.oldRemoteData["password"])
- self.log.info(_("Please check your logindata with ./pyLoadCore.py -u"))
+ self.eventManager = EventManager(self)
+ self.setupDB()
if self.deleteLinks:
self.log.info(_("All links removed"))
@@ -379,7 +383,7 @@ class Core(object):
# later imported because they would trigger api import, and remote value not set correctly
from module import Api
from module.HookManager import HookManager
- from module.ThreadManager import ThreadManager
+ from module.threads.ThreadManager import ThreadManager
if Api.activated != self.remote:
self.log.warning("Import error: API remote status not correct.")
@@ -390,7 +394,7 @@ class Core(object):
#hell yeah, so many important managers :D
self.pluginManager = PluginManager(self)
- self.pullManager = PullManager(self)
+ self.interActionManager = None #stub
self.accountManager = AccountManager(self)
self.threadManager = ThreadManager(self)
self.captchaManager = CaptchaManager(self)
@@ -399,6 +403,9 @@ class Core(object):
self.js = JsEngine()
+ # enough initialization for test cases
+ if tests: return
+
self.log.info(_("Downloadtime: %s") % self.api.isTimeDownload())
if rpc:
@@ -407,7 +414,12 @@ class Core(object):
if web:
self.init_webserver()
- spaceLeft = freeSpace(self.config["general"]["download_folder"])
+ dl_folder = self.config["general"]["download_folder"]
+
+ if not exists(dl_folder):
+ makedirs(dl_folder)
+
+ spaceLeft = free_space(dl_folder)
self.log.info(_("Free space: %s") % formatSize(spaceLeft))
@@ -430,15 +442,14 @@ class Core(object):
#self.scheduler.addJob(0, self.accountManager.getAccountInfos)
self.log.info(_("Activating Accounts..."))
- self.accountManager.getAccountInfos()
-
+ self.accountManager.refreshAllAccounts()
self.threadManager.pause = False
self.running = True
- self.log.info(_("Activating Plugins..."))
- self.hookManager.coreReady()
+ self.hookManager.activateHooks()
self.log.info(_("pyLoad is up and running"))
+ self.eventManager.dispatchEvent("coreReady")
#test api
# from module.common.APIExerciser import startApiExerciser
@@ -447,10 +458,13 @@ class Core(object):
#some memory stats
# from guppy import hpy
# hp=hpy()
+# print hp.heap()
# import objgraph
-# objgraph.show_most_common_types(limit=20)
+# objgraph.show_most_common_types(limit=30)
# import memdebug
# memdebug.start(8002)
+# from meliae import scanner
+# scanner.dump_all_objects(self.path('objs.json'))
locals().clear()
@@ -486,6 +500,9 @@ class Core(object):
console.setFormatter(frm)
self.log = logging.getLogger("log") # settable in config
+ if not exists(self.config['log']['log_folder']):
+ makedirs(self.config['log']['log_folder'], 0600)
+
if self.config['log']['file_log']:
if self.config['log']['log_rotate']:
file_handler = logging.handlers.RotatingFileHandler(join(self.config['log']['log_folder'], 'log.txt'),
@@ -523,43 +540,6 @@ class Core(object):
return False
- def check_file(self, check_names, description="", folder=False, empty=True, essential=False, quiet=False):
- """check wether needed files exists"""
- tmp_names = []
- if not type(check_names) == list:
- tmp_names.append(check_names)
- else:
- tmp_names.extend(check_names)
- file_created = True
- file_exists = True
- for tmp_name in tmp_names:
- if not exists(tmp_name):
- file_exists = False
- if empty:
- try:
- if folder:
- tmp_name = tmp_name.replace("/", sep)
- makedirs(tmp_name)
- else:
- open(tmp_name, "w")
- except:
- file_created = False
- else:
- file_created = False
-
- if not file_exists and not quiet:
- if file_created:
- #self.log.info( _("%s created") % description )
- pass
- else:
- if not empty:
- self.log.warning(
- _("could not find %(desc)s: %(name)s") % {"desc": description, "name": tmp_name})
- else:
- print _("could not create %(desc)s: %(name)s") % {"desc": description, "name": tmp_name}
- if essential:
- exit()
-
def isClientConnected(self):
return (self.lastClientConnected + 30) > time()
@@ -578,10 +558,13 @@ class Core(object):
def shutdown(self):
self.log.info(_("shutting down..."))
+ self.eventManager.dispatchEvent("coreShutdown")
try:
if self.config['webinterface']['activated'] and hasattr(self, "webserver"):
self.webserver.quit()
+
+
for thread in self.threadManager.threads:
thread.put("quit")
pyfiles = self.files.cache.values()
@@ -589,7 +572,7 @@ class Core(object):
for pyfile in pyfiles:
pyfile.abortDownload()
- self.hookManager.coreExiting()
+ self.hookManager.deactivateHooks()
except:
if self.debug:
@@ -602,11 +585,23 @@ class Core(object):
self.deletePidFile()
+ def shell(self):
+ """ stop and open a ipython shell inplace"""
+ if self.debug:
+ from IPython import embed
+ sys.stdout = sys._stdout
+ embed()
+
+ def breakpoint(self):
+ if self.debug:
+ from IPython.core.debugger import Pdb
+ sys.stdout = sys._stdout
+ if not self.pdb: self.pdb = Pdb()
+ self.pdb.set_trace()
def path(self, *args):
return join(pypath, *args)
-
def deamon():
try:
pid = os.fork()