Skip to content

Commit

Permalink
Merge pull request #149 from hautof/dev-3.0.0
Browse files Browse the repository at this point in the history
add plugin manager to support third party plugin
  • Loading branch information
tsbxmw authored Apr 4, 2019
2 parents 793e6da + 362bfa9 commit 65cfa58
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 110 deletions.
4 changes: 4 additions & 0 deletions haf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pluggy


hookimpl = pluggy.HookimplMarker("haf")
9 changes: 5 additions & 4 deletions haf/common/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def connect_execute(self, sqlconfig:SQLConfig, sqlscript:list, **kwargs):
key = kwargs.get("key", "database$%common$%")
commit = kwargs.get("commit", False)
run_background = kwargs.get("run_background", False)

logger.debug(sqlconfig)
logger.debug(sqlconfig.deserialize() if sqlconfig else None)
sqlconfig = sqlconfig
self.connect_msql = None

Expand Down Expand Up @@ -106,10 +107,10 @@ def connect_execute(self, sqlconfig:SQLConfig, sqlscript:list, **kwargs):
if not run_background:
logger.info(f"{key} result {str(data)}", __name__)

elif isinstance(ss, tuple):
elif isinstance(sqlscript, tuple):
if not run_background:
logger.info(f"{key} start {sqlconfig.host} execute {ss}", __name__)
cursor_m.execute(*ss)
logger.info(f"{key} start {sqlconfig.host} execute {sqlscript}", __name__)
cursor_m.execute(*sqlscript)
data.append(cursor_m.fetchall())
if not run_background:
logger.info(f"{key} result {str(data)}", __name__)
Expand Down
2 changes: 1 addition & 1 deletion haf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

MAIN_VERSION = 2
SUB_VERSION = 7
FIX_VERSION = 6
FIX_VERSION = 7
VERSION_TYPE = "haf"
PLATFORM_VERSION = f"{VERSION_TYPE}-{MAIN_VERSION}.{SUB_VERSION}.{FIX_VERSION}"

Expand Down
1 change: 0 additions & 1 deletion haf/hooks.py

This file was deleted.

40 changes: 40 additions & 0 deletions haf/hookspecs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pluggy


hookspec = pluggy.HookspecMarker("haf")


@hookspec
def add_option(parse):
"""add option to parse
:param run args
:return: None
"""


@hookspec
def load_from_file(file_name):
"""add option to parse
:param run args
:return: file_name
"""


@hookspec
def publish_to_sql(args, results):
"""publish result to sql
:param publish : or not
:param result : publish result
:return : None
"""

@hookspec
def start_web_server(args, bus_client):
"""start web server
:param args:
:return:
"""
34 changes: 34 additions & 0 deletions haf/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import haf
from haf.utils import LoadFromConfig
from haf.common.log import Log

logger = Log.getLogger(__name__)


@haf.hookimpl
def add_option():
args = []
return args


@haf.hookimpl
def load_from_file(file_name):
if file_name.endswith(".xlsx"):
output = LoadFromConfig.load_from_xlsx(file_name)
elif file_name.endswith(".json"):
output = LoadFromConfig.load_from_json(file_name)
elif file_name.endswith(".yml"):
output = LoadFromConfig.load_from_yml(file_name)
elif file_name.endswith(".py"):
output = LoadFromConfig.load_from_py(file_name)
return output


@haf.hookimpl
def publish_to_sql(args, results):
pass


@haf.hookimpl
def start_web_server(args, bus_client):
return False
46 changes: 5 additions & 41 deletions haf/loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding='utf-8'
import time
import haf
from multiprocessing import Process, Lock as m_lock
from haf.bench import HttpApiBench
from haf.busclient import BusClient
Expand All @@ -10,6 +11,7 @@
from haf.config import *
from haf.utils import Utils
from haf.mark import locker, new_locker
from haf.pluginmanager import plugin_manager

logger = Log.getLogger(__name__)

Expand Down Expand Up @@ -63,7 +65,9 @@ def run(self):
continue

file_name = temp.get("file_name")
inputs = LoadFromConfig.load_from_file(file_name)
# loader = LoadFromConfig()
# inputs = loader.load_from_file(file_name)
inputs = plugin_manager.load_from_file(file_name)

logger.debug(f"{self.key} -- {inputs}", __name__)
input = inputs.get("config")[0]
Expand All @@ -79,7 +83,6 @@ def run(self):
bench.add_db(db)

for input in inputs.get("testcases"):

if input.get("id") is None or input.get("subid") is None:
continue
if input.get("host_port") is None:
Expand Down Expand Up @@ -159,42 +162,3 @@ def end_handler(self, error=None):
self.case_queue.put(SIGNAL_CASE_END)
except Exception as e:
logger.error(f"{self.key} {e}", __name__)


class LoadFromConfig(object):

@staticmethod
def load_from_file(file_name):
if file_name.endswith(".xlsx"):
output = LoadFromConfig.load_from_xlsx(file_name)
elif file_name.endswith(".json"):
output = LoadFromConfig.load_from_json(file_name)
elif file_name.endswith(".yml"):
output = LoadFromConfig.load_from_yml(file_name)
elif file_name.endswith(".py"):
output = LoadFromConfig.load_from_py(file_name)
return output

@staticmethod
def load_from_xlsx(file_name):
if isinstance(file_name, str):
inputs = Utils.get_rows_from_xlsx(file_name)
return inputs

@staticmethod
def load_from_json(file_name):
if isinstance(file_name, str):
inputs = Utils.load_from_json(file_name)
return inputs

@staticmethod
def load_from_yml(file_name):
if isinstance(file_name, str):
inputs = Utils.load_from_yml(file_name)
return inputs

@staticmethod
def load_from_py(file_name):
if isinstance(file_name, str):
inputs = Utils.load_from_py(file_name)
return inputs
34 changes: 9 additions & 25 deletions haf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys

from haf.pluginmanager import PluginManager, plugin_manager
from haf.program import Program
from haf.helper import Helper
from haf.config import BANNER_STRS
Expand Down Expand Up @@ -45,10 +46,6 @@ def main_args():
help="""default is 9000, using another port to start bus server""")
sub_run_arg_program.add_argument("--only-bus", "-ob", type=bool, default=False, dest="only_bus",
help="""if true, only start bus""")
# web server
sub_run_arg_program.add_argument("--web-server", "-ws", type=bool,
help="""default is not run;
if is True, would create web server to offer the api and html service;""")
# report
sub_run_arg_program.add_argument("--report-html", "-rh", type=bool, default=True,
help="""default is True,to generate html report""")
Expand Down Expand Up @@ -76,11 +73,9 @@ def main_args():
# debug
sub_run_arg_program.add_argument("--debug", "-debug", dest="debug", default=False, type=bool,
help="open debug or not")
# sql publish
sub_run_arg_program.add_argument("--sql-publish", "-sp", dest="sql_publish", default=False, type=bool,
help="sql publish or not")
sub_run_arg_program.add_argument("--sql-publish-db", "-sp_db", dest="sql_publish_db", type=str, default="",
help="sql publish db config, format like : host:port@username:password@database)")
# console
sub_run_arg_program.add_argument("--console", "-cmd", dest="console", default=False, type=bool,
help="open console or not")
# init
sub_init_arg_program = sub_all_arg_program.add_parser("init",
help="init workspace, using 'python -m haf init -t=all' to init workspace of haf")
Expand All @@ -93,6 +88,8 @@ def main_args():
sub_help_arg_program.add_argument("--all", dest="help-all", type=str, default=None,
help="show all help informations")

plugin_manager.add_options(sub_run_arg_program)

args = arg_program.parse_args()

if args.all == "run":
Expand All @@ -115,7 +112,8 @@ def main_args():
args.only_bus = bus_config.get("only")
args.bus_server = None if bus_config.get("host") is None or bus_config.get("host")=="" else f"{bus_config.get('auth_key')}@{bus_config.get('host')}:{bus_config.get('host')}"

args.debug = config_run.get("debug", False)
args.debug = True if args.debug else config_run.get("debug", False)
args.console = True if args.console else config_run.get("console", False)

config_run_report = config_run.get("report")
args.report_output_dir = config_run_report.get("report_path")
Expand Down Expand Up @@ -147,27 +145,12 @@ def main_args():
if args.runner_count:
pass

if args.sql_publish:
if isinstance(args.sql_publish_db, str):
from haf.common.database import SQLConfig
sql_config = SQLConfig()
hp, up, db = args.sql_publish_db.split('@')
host, port = hp.split(':')
username, password = up.split(':')
sc_dict = {
"host": host, "port": port, "username": username, "password": password, "id":0, "sql_name": "haf-publish", "protocol": "mysql"
}
sql_config.constructor(sc_dict)
args.sql_publish_db = sql_config

# here : bus server <- password@host:port
if args.bus_server:
if "@" in args.bus_server and ":" in args.bus_server:
password, temp = args.bus_server.split("@")
host, port = temp.split(":")
args.bus_server = [bytes(password, encoding='utf-8'), host, int(port)]
if args.web_server:
pass

# here : case <- dir/file
if args.case:
Expand All @@ -188,6 +171,7 @@ def main_args():
else:
args.case.append(path)
print(args)

main_program = Program()
main_program.run(args)
elif args.all == "init":
Expand Down
46 changes: 46 additions & 0 deletions haf/pluginmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import itertools
import random

import pluggy
import json
import os
import sys
import argparse
from haf import hookspecs, lib


class PluginManager(object):
def __init__(self):
self.get_plugin_manager()

def add_options(self, sub_run_arg_program):
pm = self.pm
return pm.hook.add_option(parse = sub_run_arg_program)

def load_from_file(self, file_name):
pm = self.pm
inputs = pm.hook.load_from_file(file_name = file_name)
if isinstance(inputs, list):
return inputs[0]
elif isinstance(inputs, dict):
return inputs

def publish_to_sql(self, args, results):
pm = self.pm
publish_result = pm.hook.publish_to_sql(args=args, results=results)
return publish_result

def start_web_server(self, args, bus_client):
pm = self.pm
result = pm.hook.start_web_server(args=args, bus_client=bus_client)
return result

def get_plugin_manager(self):
self.pm = pluggy.PluginManager("haf")
self.pm.add_hookspecs(hookspecs)
self.pm.load_setuptools_entrypoints("haf")
self.pm.register(lib)
return self.pm


plugin_manager = PluginManager()
Loading

0 comments on commit 65cfa58

Please sign in to comment.