Ejemplo n.º 1
0
def get_last_task(app, username, project):
    icons = {
        "running": url_for('static', filename='img/running.gif'),
        "success": url_for('static', filename='img/success.png'),
        "fail": url_for('static', filename='img/fail.png'),
        "exception": url_for('static', filename='img/exception.png')
    }
    job_path = app.config["AUTO_HOME"] + "/jobs/%s/%s" % (username, project)
    status = icons["running"]
    if exists_path(job_path):
        next_build = get_next_build_number(job_path)
        last_job = next_build - 1
        if exists_path(job_path + "/%s" % last_job):
            try:
                suite = ExecutionResult(job_path +
                                        "/%s/output.xml" % last_job).suite
                stat = suite.statistics.critical
                if stat.failed != 0:
                    status = icons["fail"]
                else:
                    status = icons['success']
            except:
                status = icons["running"]
        else:
            status = icons["exception"]
    else:
        status = icons['success']

    return status
Ejemplo n.º 2
0
def robot_run(app, case_key, args='', user='', driver='USER'):

    username = user if user != '' else session['username']

    project = get_projectnamefromkey(case_key)
    output = app.config["AUTO_HOME"] + "/jobs/%s/%s" % (username, project)

    if not exists_path(output):
        mk_dirs(output)

    (out, index) = reset_next_build_numb(output)

    mk_dirs(out) if not exists_path(out) else None

    cmd = 'robot ' + args + ' --outputdir=' + out + ' ' + case_key

    log.info("Robot_run CMD:{}".format(cmd))
    with open(out + "/cmd.txt", 'w') as f:
        f.write("{}|robot|{}|--outputdir={}|{}\n".format(
            driver, args, out, case_key))

    cp = subRun(cmd,
                shell=True,
                stdout=PIPE,
                stderr=STDOUT,
                text=True,
                timeout=7200)  # timeout: sec 2hrs

    with open(out + "/debug.txt", 'w') as f:
        f.write(cp.stdout)

    app.config['DB'].insert_loginfo(username, 'task', 'run', case_key, 'OK')

    # Report and xUnit files can be generated based on the result object.
    # ResultWriter(result).write_results(report=out + '/report.html', log=out + '/log.html')
    try:

        detail_result = ExecutionResult(out + "/output.xml")

    except Exception as e:
        log.error(
            "Open output.xml Exception:{},\n May robot run fail, console:{}".
            format(e, cp.stdout))
        return

    # detail_result.save(out + "/output_new.xml")
    reset_last_status(detail_result, output, index)

    # Report and xUnit files can be generated based on the result object.
    ResultWriter(detail_result).write_results(report=out + '/report.html',
                                              log=out + '/log.html')

    s = detail_result.suite
    dealwith_source(app, username, s)
Ejemplo n.º 3
0
    def run(self):
        #lock = threading.Lock()

        # self.lock.acquire()
        if not exists_path(self.output):
            mk_dirs(self.output)

        self.suite = TestSuiteBuilder().build(self.project)

        (output, index) = self.reset_next_build_numb()

        self.setName(output)

        self.result = self.suite.run(output_directory=output,
                                     output=output + "/output.xml",
                                     debugfile=output + "/debug.txt",
                                     loglevel="TRACE")

        # self.reset_last_status(index)

        # Report and xUnit files can be generated based on the result object.
        # ResultWriter(self.result).write_results(report=output + '/report.html', log=output + '/log.html')

        # self.lock.release()

        # Generating log files requires processing the earlier generated output XML.
        # ResultWriter(self.output + '/output.xml').write_results()

        self.result = ExecutionResult(out + "/output.xml")

        self.reset_last_status(self.result, output, index)

        # Report and xUnit files can be generated based on the result object.
        ResultWriter(self.result).write_results(report=out + '/report.html',
                                                log=out + '/log.html')
Ejemplo n.º 4
0
def robot_run(username, name, project, output):
    if not exists_path(output):
        mk_dirs(output)

    suite = TestSuiteBuilder().build(project)

    (out, index) = reset_next_build_numb(output)

    result = suite.run(output_directory=out,
                       output=out + "/output.xml",
                       debugfile=out + "/debug.txt",
                       loglevel="TRACE")

    # reset_last_status(result, output, index)

    # Report and xUnit files can be generated based on the result object.
    # ResultWriter(result).write_results(report=out + '/report.html', log=out + '/log.html')
    detail_result = ExecutionResult(out + "/output.xml")

    # detail_result.save(out + "/output_new.xml")
    reset_last_status(detail_result, output, index)

    # Report and xUnit files can be generated based on the result object.
    ResultWriter(detail_result).write_results(report=out + '/report.html',
                                              log=out + '/log.html')

    send_robot_report(username, name, index, detail_result, out)
Ejemplo n.º 5
0
def reset_next_build_numb(output):
    next_build_number = output + "/nextBuildNumber"
    index = 1
    data = "%d" % (index + 1)
    if not exists_path(next_build_number):
        make_nod(next_build_number)
    else:
        index = int(read_file(next_build_number)["data"])
        data = "%d" % (index + 1)
    write_file(next_build_number, data)

    out = output + "/%d" % index
    if not exists_path(output):
        mk_dirs(output)

    return (out, index)
Ejemplo n.º 6
0
def reset_last_status(result, output, index):
    stats = result.statistics
    fail = stats.total.critical.failed

    last_fail = output + "/lastFail"
    last_passed = output + "/lastPassed"
    data = "%d" % index

    if fail != 0:
        if not exists_path(last_fail):
            make_nod(last_fail)

        write_file(last_fail, data)
    else:
        if not exists_path(last_passed):
            make_nod(last_passed)
        write_file(last_passed, data)
Ejemplo n.º 7
0
def view_img():
    args = request.args.to_dict()
    app = current_app._get_current_object()
    img_path = app.config[
        "AUTO_HOME"] + "/workspace/%s" % session['username'] + args["path"]
    img_path.replace("\\", "/")
    if exists_path(img_path):
        return send_file(img_path)

    return False
Ejemplo n.º 8
0
    def __create(self, args):
        result = {"status": "success", "msg": "创建目录成功"}
        user_path = self.app.config["AUTO_HOME"] + "/workspace/%s/%s/%s" % (
            session["username"], args["project_name"], args["name"])
        if not exists_path(user_path):
            mk_dirs(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "目录名称重复,创建失败"

        return result
Ejemplo n.º 9
0
    def __upload(self, file, path):
        result = {"status": "success", "msg": "上传成功"}

        user_path = self.app.config["AUTO_HOME"] + "/workspace/%s" % session['username'] + path + file.filename
        if not exists_path(user_path):
            file.save(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "上传失败"

        return result
Ejemplo n.º 10
0
def get_last_pass(job_path):
    passed = "无"
    passed_path = job_path + "lastPassed"
    if exists_path(passed_path):
        f = codecs.open(passed_path, "r", "utf-8")

        passed = f.read()

        f.close()

    return passed
Ejemplo n.º 11
0
def get_last_fail(job_path):
    fail = "无"
    fail_path = job_path + "lastFail"
    if exists_path(fail_path):
        f = codecs.open(fail_path, "r", "utf-8")

        fail = f.read()

        f.close()

    return fail
Ejemplo n.º 12
0
def get_next_build_number(job_path):
    next_build_number = 1
    next_path = job_path + "/nextBuildNumber"
    if exists_path(next_path):
        f = codecs.open(next_path, "r", "utf-8")

        next_build_number = int(f.read())

        f.close()

    return next_build_number
Ejemplo n.º 13
0
    def __delete(self, args):
        result = {"status": "success", "msg": "项目删除成功"}
        user_path = self.app.config["AUTO_HOME"] + "/workspace/%s/%s" % (session["username"], args["name"])
        if exists_path(user_path):
            remove_dir(user_path)

            remove_project(self.app, session['username'], args['name'])
        else:
            result["status"] = "fail"
            result["msg"] = "删除失败,不存在的项目"

        return result
Ejemplo n.º 14
0
    def reset_last_status(self, index):
        stats = self.result.statistics
        fail = stats.total.critical.failed

        lock = threading.Lock()

        lock.acquire()
        last_fail = self.output + "/lastFail"
        last_passed = self.output + "/lastPassed"
        data = "%d" % index

        if fail != 0:
            if not exists_path(last_fail):
                make_nod(last_fail)

            write_file(last_fail, data)
        else:
            if not exists_path(last_passed):
                make_nod(last_passed)
            write_file(last_passed, data)

        lock.release()
Ejemplo n.º 15
0
    def __delete(self, args):
        result = {"status": "success", "msg": "目录删除成功"}
        user_path = self.app.config[
            "AUTO_HOME"] + "/workspace/%s/%s/%s/%s%s" % (
                session["username"], args["project_name"], args["suite_name"],
                args["name"], args["category"])
        if exists_path(user_path):
            remove_file(user_path)

        else:
            result["status"] = "fail"
            result["msg"] = "删除失败,不存在的文件"

        return result
Ejemplo n.º 16
0
def get_all_task(app):
    user_path = app.config["AUTO_HOME"] + "/users/" + session["username"]
    if exists_path(user_path):
        config = json.load(
            codecs.open(user_path + '/config.json', 'r', 'utf-8'))
        projects = config['data']
        task_list = {"total": len(projects), "rows": []}
        for p in projects:
            # job_path = app.config["AUTO_HOME"] + "/jobs/%s/%s" % (session["username"], p["name"])
            # running = False
            # lock = threading.Lock()
            # lock.acquire()
            # remove_robot(app)
            # next_build = get_next_build_number(job_path)
            # if next_build != 0:
            #     for pp in app.config["AUTO_ROBOT"]:
            #         if pp["name"] == p["name"]:
            #             running = True
            #             status = icons["running"]
            #             break
            #     if running is False:
            #         if exists_path(job_path + "/%s" % (next_build-1)):
            #             try:
            #                 suite = ExecutionResult(job_path + "/%s/output.xml" % (next_build-1)).suite
            #                 stat = suite.statistics.critical
            #                 if stat.failed != 0:
            #                     status = icons["fail"]
            #                 else:
            #                     status = icons['success']
            #             except:
            #                 status = icons["running"]
            # else:
            #     status = icons['success']

            # lock.release()
            task = {
                #"status": status,
                "name": p["name"],
                #"last_success": get_last_pass(job_path + "/lastPassed"),
                #"last_fail": get_last_fail(job_path + "/lastFail"),
                "enable": p["enable"],
                "next_time": get_next_time(app, p["name"]),
                "cron": p["cron"],
                "status": get_last_task(app, session["username"], p["name"])
            }

            task_list["rows"].append(task)

    return task_list
Ejemplo n.º 17
0
    def __create(self, args):
        result = {"status": "success", "msg": "成功:创建目录."}
        user_path = args['key'] + '/' + args['name']
        if not exists_path(user_path):
            mk_dirs(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "失败:目录已存在!"

        try:
            res = self.app.config['DB'].insert_loginfo(session['username'], 'dir', 'create', user_path, result['status'])
        except Exception as e:
            self.log.error("创建目录 {} 异常: {}".format(user_path,e))

        return result
Ejemplo n.º 18
0
    def __delete(self, args):
        result = {"status": "success", "msg": "成功:删除目录."}
        user_path = args['key']
        if exists_path(user_path):
            remove_dir(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "失败:目录不存在!"
            return result

        self.app.config['DB'].delete_suite(user_path)

        self.app.config['DB'].insert_loginfo(session['username'], 'dir', 'delete', user_path, result['status'])

        return result
Ejemplo n.º 19
0
    def __upload(self, file, path):
        result = {"status": "success", "msg": "成功:上传文件."}

        #charis added  TODO: 如果统一菜单的话,可以这里判断path是否为目录或文件
        user_path = path + '/' + file.filename
        #user_path = self.app.config["AUTO_HOME"] + "/workspace/%s" % session['username'] + path + file.filename
        if not exists_path(user_path):
            file.save(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "失败:上传文件."

        self.app.config['DB'].insert_loginfo(session['username'], 'file',
                                             'upload', user_path)

        return result
Ejemplo n.º 20
0
    def __create(self, args):

        self.log.info("***create***name:{} **cat:{}".format(
            args['name'], args['category']))
        if args['name'].endswith(args['category']):
            args['name'] = args['name'].split('.')[0]
        if args['category'] == '.oth':
            user_path = args["key"] + '/' + args['name']
        else:
            user_path = args["key"] + '/' + args['name'] + args['category']

        result = {
            "status": "success",
            "msg":
            "创建测试模型成功" + ":" + os.path.basename(user_path) + ":" + user_path
        }
        if not exists_path(user_path):
            make_nod(user_path)
            mod = '''
{ "class": "GraphLinksModel",
  "nodeKeyProperty": "id",
  "linkKeyProperty": "key",
  "nodeDataArray": [ 
    {"id":-1, "loc":"-256 -210", "category":"Start", "text":"开始节点", "description":"这里也可以定义变量", "outputvariable":"产品列表", "disabled":false, "properties":""},
    {"id":0, "loc":"-113 -126", "text":"Shopping页", "description":"", "outputvariable":"", "disabled":false, "properties":"产品列表=产品列表"},
    {"id":2, "loc":"82 -127", "text":"购买", "description":"", "outputvariable":"", "disabled":false, "properties":"购物列表=手表"},
    {"text":"页面关闭", "id":-5, "loc":"71.0 -74.0"}
  ],
  "linkDataArray": [ 
    {"key":-1, "from":-1, "to":0, "text":"打开网站", "points":[-187,-166,-161,-159,-140,-146,-124,-126], "description":"", "parameters":"", "mainpath":true},
    {"key":0, "from":0, "to":2, "progress":"true", "text":"买手表", "points":[-70,-122,-30,-131,13,-131,60,-118], "description":"", "parameters":"手表", "weight":"2.0", "mainpath":true, "action":"buy"},
    {"key":1, "from":0, "to":-5, "points":[-70,-111,-27,-111,13,-98,49,-74], "text":"关闭", "description":"", "action":"close", "parameters":"window", "mainpath":false}
  ],
  "modelData": {"version":1, "nodes":4, "links":3, "actions":3, "variables":5, "variable":[], "init_actions":"模块的初始化条件"}
}
            '''
            with open(user_path, 'w') as f:
                f.write(mod)
        else:
            result["status"] = "fail"
            result["msg"] = "失败: 文件已存在 !"

        self.app.config['DB'].insert_loginfo(session['username'], 'model',
                                             'create', user_path,
                                             result['status'])

        return result
Ejemplo n.º 21
0
def robot_run(name, output):
    if not exists_path(output):
        mk_dirs(output)

    suite = TestSuiteBuilder().build(name)

    (out, index) = reset_next_build_numb(output)

    result = suite.run(output_directory=out,
                       output=out + "/output.xml",
                       log_level="TRACE")

    reset_last_status(result, output, index)

    # Report and xUnit files can be generated based on the result object.
    ResultWriter(result).write_results(report=out + '/report.html',
                                       log=out + '/log.html')
Ejemplo n.º 22
0
    def __delete(self, args):
        result = {"status": "success", "msg": "用户删除成功"}
        user_path = self.app.config["AUTO_HOME"] + "/users/" + args["username"]

        if exists_path(user_path):
            config = json.load(
                codecs.open(user_path + '/config.json', 'r', 'utf-8'))
            if len(config["data"]) > 0:
                result["status"] = "fail"
                result["msg"] = "请先删除该用户拥有的项目"
            else:
                remove_dir(user_path)

        else:
            result["status"] = "fail"
            result["msg"] = "用户不存在,删除失败"

        return result
Ejemplo n.º 23
0
def robot_debugrun(app, cases):

    out = app.config['AUTO_TEMP']
    if not exists_path(out):
        mk_dirs(out)

    cmd = 'robot --outputdir=' + out + ' ' + cases
    cp = subRun(cmd,
                shell=True,
                stdout=PIPE,
                stderr=STDOUT,
                text=True,
                timeout=60)  # timeout: sec

    app.config['DB'].insert_loginfo(session['username'], 'case', 'debug',
                                    cases, 'OK')

    return cp.stdout
Ejemplo n.º 24
0
    def __delete(self, args):
        result = {"status": "success", "msg": "删除项目成功."}

        self.log.debug("删除项目 args:{}".format(args))

        project = args["name"]
        owner = self.app.config['DB'].get_projectowner(project)
        #owner = get_ownerfromkey(args['key'])

        if not session["username"] == "Admin":
            result["status"] = "fail"
            result["msg"] = "FAIL:只有Admin可以进行此操作!"
            return result

        user_path = self.app.config["AUTO_HOME"] + \
            "/workspace/%s/%s" % (owner, args["name"])
        #user_path = args['key']
        self.log.info("删除项目:开始删除项目目录 {}".format(user_path))
        if exists_path(user_path):
            remove_dir(user_path)

        if not self.app.config['DB'].del_project(args["name"], owner):
            result["status"] = "fail"
            result["msg"] = "删除失败, 项目不存在."

        # TODO 还需要删除项目目录 workspace 和 jobs 下的内容
        self.log.info("删除项目的owner:{} 和以 {} 为主项目的成员".format(owner, project))
        self.app.config['DB'].del_user(owner)
        work_path = os.path.join(self.app.config['AUTO_HOME'], 'workspace',
                                 owner)
        remove_dir(work_path) if os.path.exists(work_path) else None

        self.app.config['DB'].runsql(
            "Delete from user where main_project='{}' ;".format(project))

        self.app.config['DB'].insert_loginfo(session['username'], 'project',
                                             'delete', user_path,
                                             result['status'])

        # Delete resource is not dangerous, all of that can be auto generated.
        clear_projectres('usepath', user_path)

        return result
Ejemplo n.º 25
0
    def __create(self, args):

        name = args["name"]
        manager = args["manager"]
        passwd = generate_password_hash("123")

        result = {"status": "success", "msg": "成功:创建项目{}.".format(name)}

        if args["name"] in self.reserved_names:
            result = {"status": "fail", "msg": "请换一个用户名."}
            return result

        if not self.app.config['DB'].add_user(
                manager, manager, passwd, manager + "@qq.com", 'User', name):
            result = {
                "status": "fail",
                "msg": "失败:创建项目管理员{}失败.".format(manager)
            }
            return result

        project_path = self.app.config["AUTO_HOME"] + \
            "/workspace/%s/%s" % (manager, name)
        if not exists_path(project_path):
            mk_dirs(project_path)
            mk_dirs(os.path.join(project_path, 'platforminterface'))

        if not self.app.config['DB'].add_project(name, manager, ''):
            result["status"] = "fail"
            result["msg"] = "失败: 创建项目失败(?名称存在)"
            self.app.config['DB'].del_user(manager)
            return result

        self.save_project(project_path)
        self.save_user(project_path)
        self.save_settings(project_path)

        self.app.config['DB'].insert_loginfo(session['username'], 'project',
                                             'create', project_path,
                                             result['status'])

        return result
Ejemplo n.º 26
0
    def __create(self, args):
        result = {"status": "success", "msg": "创建项目成功"}

        user_path = self.app.config["AUTO_HOME"] + "/workspace/%s/%s" % (
            session["username"], args["name"])
        if not exists_path(user_path):
            mk_dirs(user_path)

            create_project(
                self.app, session["username"], {
                    "name": args["name"],
                    "description": args["description"],
                    "boolean": args["boolean"],
                    "enable": args["enable"],
                    "cron": args["cron"]
                })
        else:
            result["status"] = "fail"
            result["msg"] = "项目名称重复,创建失败"

        return result
Ejemplo n.º 27
0
def robot_runOLD(app, username, project, case_key, output):
    if not exists_path(output):
        mk_dirs(output)

    suite = TestSuiteBuilder().build(case_key)

    (out, index) = reset_next_build_numb(output)

    result = suite.run(output_directory=out,
                       output=out + "/output.xml",
                       debugfile=out + "/debug.txt",
                       loglevel="TRACE")

    try:
        totalcases = result.statistics.total.all.total
        passed = result.statistics.total.all.passed
        failed = result.statistics.total.all.failed
        elapsedtime = result.suite.elapsedtime
        logres = "total:{},pass:{},fail:{},elapsedtime:{}".format(
            totalcases, passed, failed, elapsedtime)
        app.config['DB'].insert_loginfo(username, 'task', 'run', case_key,
                                        logres)
    except Exception as e:
        log.error("robot_run Exception: {}".format(e))

    # Report and xUnit files can be generated based on the result object.
    # ResultWriter(result).write_results(report=out + '/report.html', log=out + '/log.html')
    detail_result = ExecutionResult(out + "/output.xml")

    # detail_result.save(out + "/output_new.xml")
    reset_last_status(detail_result, output, index)

    # Report and xUnit files can be generated based on the result object.
    ResultWriter(detail_result).write_results(report=out + '/report.html',
                                              log=out + '/log.html')

    s = detail_result.suite
    dealwith_source(app, username, s)

    send_robot_report(username, project, index, detail_result, out)
Ejemplo n.º 28
0
    def __create(self, args):
        result = {"status": "success", "msg": "创建用户成功"}
        user_path = self.app.config["AUTO_HOME"] + "/users/%s" % (
            args["username"])
        if not exists_path(user_path):
            mk_dirs(user_path)

            make_nod(user_path + "/config.json")

            user = {
                "fullname": args["fullname"],
                "email": args["email"],
                "passwordHash": generate_password_hash(args["password"]),
                "data": []
            }
            json.dump(user,
                      codecs.open(user_path + '/config.json', 'w', 'utf-8'))
        else:
            result["status"] = "fail"
            result["msg"] = "用户名称重复,创建失败"

        return result
Ejemplo n.º 29
0
    def run(self):
        #lock = threading.Lock()

        # self.lock.acquire()
        if not exists_path(self.output):
            mk_dirs(self.output)

        self.suite = TestSuiteBuilder().build(self.project)

        (output, index) = self.reset_next_build_numb()

        self.setName(output)

        self.result = self.suite.run(output_directory=output,
                                     output=output + "/output.xml",
                                     log_level="DEBUG")

        self.reset_last_status(index)

        # Report and xUnit files can be generated based on the result object.
        ResultWriter(self.result).write_results(report=output + '/report.html',
                                                log=output + '/log.html')
Ejemplo n.º 30
0
    def __delete(self, args):
        result = {"status": "success", "msg": "删除成功:" + args['key']}

        user_path = args["key"]
        if exists_path(user_path):
            remove_file(user_path)
        else:
            result["status"] = "fail"
            result["msg"] = "删除失败,文件不存在!"
            return result

        if user_path.endswith('.robot'):
            self.app.config['DB'].delete_suite(user_path)
        if user_path.endswith(
                '.resource'):  # delete keywords or update highlight
            update_resource(user_path)

        self.app.config['DB'].insert_loginfo(session['username'], 'suite',
                                             'delete', user_path,
                                             result['status'])

        return result