diff options
Diffstat (limited to 'module/common')
| -rw-r--r-- | module/common/APIExerciser.py | 157 | ||||
| -rw-r--r-- | module/common/JsEngine.py | 35 | ||||
| -rw-r--r-- | module/common/__init__.py | 2 | ||||
| -rw-r--r-- | module/common/packagetools.py | 4 | 
4 files changed, 36 insertions, 162 deletions
diff --git a/module/common/APIExerciser.py b/module/common/APIExerciser.py deleted file mode 100644 index 96f5ce9cf..000000000 --- a/module/common/APIExerciser.py +++ /dev/null @@ -1,157 +0,0 @@ -# -*- coding: utf-8 -*- - -import string -from threading import Thread -from random import choice, random, sample, randint -from time import time, sleep -from math import floor -import gc - -from traceback import print_exc, format_exc - -from module.remote.thriftbackend.ThriftClient import ThriftClient, Destination - -def createURLs(): -    """ create some urls, some may fail """ -    urls = [] -    for x in range(0, randint(20, 100)): -        name = "DEBUG_API" -        if randint(0, 5) == 5: -            name = "" #this link will fail - -        urls.append(name + "".join(sample(string.ascii_letters, randint(10, 20)))) - -    return urls - -AVOID = (0,3,8) - -idPool = 0 -sumCalled = 0 - - -def startApiExerciser(core, n): -    for i in range(n): -        APIExerciser(core).start() - -class APIExerciser(Thread): - - -    def __init__(self, core, thrift=False, user=None, pw=None): -        global idPool - -        Thread.__init__(self) -        self.setDaemon(True) -        self.core = core -        self.count = 0 #number of methods -        self.time = time() - -        if thrift: -            self.api = ThriftClient(user=user, password=pw) -        else: -            self.api = core.api - - -        self.id = idPool - -        idPool += 1 - -        #self.start() - -    def run(self): - -        self.core.log.info("API Excerciser started %d" % self.id) - -        out = open("error.log", "ab") -        #core errors are not logged of course -        out.write("\n" + "Starting\n") -        out.flush() - -        while True: -            try: -                self.testAPI() -            except Exception: -                self.core.log.error("Excerciser %d throw an execption" % self.id) -                print_exc() -                out.write(format_exc() + 2 * "\n") -                out.flush() - -            if not self.count % 100: -                self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) -            if not self.count % 1000: -                out.flush() - -            if not sumCalled % 1000: #not thread safe -                self.core.log.info("Exercisers tested %d api calls" % sumCalled) -                persec = sumCalled / (time() - self.time) -                self.core.log.info("Approx. %.2f calls per second." % persec) -                self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) -                self.core.log.info("Collected garbage: %d" % gc.collect()) - - -                #sleep(random() / 500) - -    def testAPI(self): -        global sumCalled - -        m = ["statusDownloads", "statusServer", "addPackage", "getPackageData", "getFileData", "deleteFiles", -             "deletePackages", "getQueue", "getCollector", "getQueueData", "getCollectorData", "isCaptchaWaiting", -             "getCaptchaTask", "stopAllDownloads", "getAllInfo", "getServices" , "getAccounts", "getAllUserData"] - -        method = choice(m) -        #print "Testing:", method - -        if hasattr(self, method): -            res = getattr(self, method)() -        else: -            res = getattr(self.api, method)() - -        self.count += 1 -        sumCalled += 1 - -        #print res - -    def addPackage(self): -        name = "".join(sample(string.ascii_letters, 10)) -        urls = createURLs() - -        self.api.addPackage(name, urls, choice([Destination.Queue, Destination.Collector])) - - -    def deleteFiles(self): -        info = self.api.getQueueData() -        if not info: return - -        pack = choice(info) -        fids = pack.links - -        if len(fids): -            fids = [f.fid for f in sample(fids, randint(1, max(len(fids) / 2, 1)))] -            self.api.deleteFiles(fids) - - -    def deletePackages(self): -        info = choice([self.api.getQueue(), self.api.getCollector()]) -        if not info: return - -        pids = [p.pid for p in info] -        if len(pids): -            pids = sample(pids, randint(1,  max(floor(len(pids) / 2.5), 1))) -            self.api.deletePackages(pids) - -    def getFileData(self): -        info = self.api.getQueueData() -        if info: -            p = choice(info) -            if p.links: -                self.api.getFileData(choice(p.links).fid) - -    def getPackageData(self): -        info = self.api.getQueue() -        if info: -            self.api.getPackageData(choice(info).pid) - -    def getAccounts(self): -        self.api.getAccounts(False) - -    def getCaptchaTask(self): -        self.api.getCaptchaTask(False)
\ No newline at end of file diff --git a/module/common/JsEngine.py b/module/common/JsEngine.py index 576be2a1b..ef7494d16 100644 --- a/module/common/JsEngine.py +++ b/module/common/JsEngine.py @@ -27,8 +27,10 @@ ENGINE = ""  DEBUG = False  JS = False  PYV8 = False +NODE = False  RHINO = False +# TODO: Refactor + clean up this class  if not ENGINE:      try: @@ -54,6 +56,19 @@ if not ENGINE or DEBUG:  if not ENGINE or DEBUG:      try: +        import subprocess +        subprocess.Popen(["node", "-v"], bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() +        p = subprocess.Popen(["node", "-e", "console.log(23+19)"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) +        out, err = p.communicate() +        #integrity check +        if out.strip() == "42": +            ENGINE = "node" +        NODE = True +    except: +        pass + +if not ENGINE or DEBUG: +    try:          path = "" #path where to find rhino          if exists("/usr/share/java/js.jar"): @@ -86,6 +101,10 @@ class JsEngine():      def __nonzero__(self):          return False if not ENGINE else True +    def set_debug(self, value): +        global DEBUG +        DEBUG = value +      def eval(self, script):          if not self.init:              if ENGINE == "pyv8" or (DEBUG and PYV8): @@ -105,6 +124,8 @@ class JsEngine():                  return self.eval_pyv8(script)              elif ENGINE == "js":                  return self.eval_js(script) +            elif ENGINE == "node": +                return self.eval_node(script)              elif ENGINE == "rhino":                  return self.eval_rhino(script)          else: @@ -117,6 +138,10 @@ class JsEngine():                  res = self.eval_js(script)                  print "JS:", res                  results.append(res) +            if NODE: +                res = self.eval_node(script) +                print "NODE:", res +                results.append(res)              if RHINO:                  res = self.eval_rhino(script)                  print "Rhino:", res @@ -144,6 +169,13 @@ class JsEngine():          res = out.strip()          return res +    def eval_node(self, script): +        script = "console.log(eval(unescape('%s')))" % quote(script) +        p = subprocess.Popen(["node", "-e", script], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=-1) +        out, err = p.communicate() +        res = out.strip() +        return res +      def eval_rhino(self, script):          script = "print(eval(unescape('%s')))" % quote(script)          p = subprocess.Popen(["java", "-cp", path, "org.mozilla.javascript.tools.shell.Main", "-e", script], @@ -153,10 +185,11 @@ class JsEngine():          return res.decode("utf8").encode("ISO-8859-1")      def error(self): -        return _("No js engine detected, please install either Spidermonkey, ossp-js, pyv8 or rhino") +        return _("No js engine detected, please install either Spidermonkey, ossp-js, pyv8, nodejs or rhino")  if __name__ == "__main__":      js = JsEngine() +    js.set_debug(True)      test = u'"ü"+"ä"'      js.eval(test)
\ No newline at end of file diff --git a/module/common/__init__.py b/module/common/__init__.py index de6d13128..e69de29bb 100644 --- a/module/common/__init__.py +++ b/module/common/__init__.py @@ -1,2 +0,0 @@ -__author__ = 'christian' -  
\ No newline at end of file diff --git a/module/common/packagetools.py b/module/common/packagetools.py index 5bfbcba95..791a46d51 100644 --- a/module/common/packagetools.py +++ b/module/common/packagetools.py @@ -21,7 +21,7 @@ def parseNames(files):      """ Generates packages names from name, data lists      :param files: list of (name, data) -    :return: packagenames mapt to data lists (eg. urls) +    :return: packagenames mapped to data lists (eg. urls)      """      packs = {} @@ -64,7 +64,7 @@ def parseNames(files):          if len(split) > 1:              name = split.pop(1) -            #check if a already existing package may be ok for this file +            #check if an already existing package may be ok for this file          #        found = False          #        for pack in packs:          #            if pack in file:  | 
