Esempio n. 1
0
    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
Esempio n. 2
0
    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()

        self.api = ThriftClient(user=user, password=pw) if thrift else core.api

        self.id = idPool

        idPool += 1
Esempio n. 3
0
    def connectProxy(self):
        """
            initialize thrift rpc client,
            check for ssl, check auth,
            setup dispatcher,
            connect error signals,
            check server version
        """
        if self.internal: return True

        err = None
        try:
            client = ThriftClient(self.host, self.port, self.user,
                                  self.password)
        except WrongLogin:
            err = _("bad login credentials")
        except NoSSL:
            err = _("no ssl support")
        except NoConnection:
            err = _("can't connect to host")
        if err:
            if not Connector.firstAttempt:
                self.emit(SIGNAL("errorBox"), err)
            Connector.firstAttempt = False
            return False

        self.proxy = DispatchRPC(self.mutex, client)
        self.connect(self.proxy, SIGNAL("connectionLost"), self,
                     SIGNAL("connectionLost"))

        server_version = self.proxy.getServerVersion()
        self.connectionID = uuid().hex

        if not server_version == SERVER_VERSION:
            self.emit(
                SIGNAL("errorBox"),
                _("server is version %(new)s client accepts version %(current)s"
                  ) % {
                      "new": server_version,
                      "current": SERVER_VERSION
                  })
            return False

        return True
Esempio n. 4
0
def main():
    config = {"addr": "127.0.0.1", "port": "7227", "language": "en"}
    try:
        config["language"] = os.environ["LANG"][0:2]
    except:
        pass

    if (not exists(join(pypath, "locale",
                        config["language"]))) or config["language"] == "":
        config["language"] = "en"

    configFile = ConfigParser.ConfigParser()
    configFile.read(join(homedir, ".pyload-cli"))

    if configFile.has_section("cli"):
        for opt in configFile.items("cli"):
            config[opt[0]] = opt[1]

    gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
    translation = gettext.translation("Cli",
                                      join(pypath, "locale"),
                                      languages=[config["language"], "en"],
                                      fallback=True)
    translation.install(unicode=True)

    interactive = False
    command = None
    username = ""
    password = ""

    shortOptions = 'iu:p:a:hcl:'
    longOptions = [
        'interactive', "username="******"pw=", "address=", "port=", "help",
        "commands", "language="
    ]

    try:
        opts, extraparams = getopt(sys.argv[1:], shortOptions, longOptions)
        for option, params in opts:
            if option in ("-i", "--interactive"):
                interactive = True
            elif option in ("-u", "--username"):
                username = params
            elif option in ("-a", "--address"):
                config["addr"] = params
            elif option in ("-p", "--port"):
                config["port"] = params
            elif option in ("-l", "--language"):
                config["language"] = params
                gettext.setpaths(
                    [join(os.sep, "usr", "share", "pyload", "locale"), None])
                translation = gettext.translation(
                    "Cli",
                    join(pypath, "locale"),
                    languages=[config["language"], "en"],
                    fallback=True)
                translation.install(unicode=True)
            elif option in ("-h", "--help"):
                print_help(config)
                exit()
            elif option in ("--pw"):
                password = params
            elif option in ("-c", "--comands"):
                print_commands()
                exit()

    except GetoptError:
        print 'Unknown Argument(s) "%s"' % " ".join(sys.argv[1:])
        print_help(config)
        exit()

    if len(extraparams) >= 1:
        command = extraparams

    client = False

    if interactive:
        try:
            client = ThriftClient(config["addr"], int(config["port"]),
                                  username, password)
        except WrongLogin:
            pass
        except NoSSL:
            print _("You need py-openssl to connect to this pyLoad Core.")
            exit()
        except NoConnection:
            config["addr"] = False
            config["port"] = False

        if not client:
            if not config["addr"]: config["addr"] = raw_input(_("Address: "))
            if not config["port"]: config["port"] = raw_input(_("Port: "))
            if not username: username = raw_input(_("Username: "******"Password: "******"addr"], int(config["port"]),
                                      username, password)
            except WrongLogin:
                print _("Login data is wrong.")
            except NoConnection:
                print _(
                    "Could not establish connection to %(addr)s:%(port)s." % {
                        "addr": config["addr"],
                        "port": config["port"]
                    })

    else:
        try:
            client = ThriftClient(config["addr"], int(config["port"]),
                                  username, password)
        except WrongLogin:
            print _("Login data is wrong.")
        except NoConnection:
            print _("Could not establish connection to %(addr)s:%(port)s." % {
                "addr": config["addr"],
                "port": config["port"]
            })
        except NoSSL:
            print _("You need py-openssl to connect to this pyLoad core.")

    if interactive and command:
        print _("Interactive mode ignored since you passed some commands.")

    if client:
        writeConfig(config)
        cli = Cli(client, command)
Esempio n. 5
0
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)
Esempio n. 6
0
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)