Skip to content

Commit

Permalink
feat(1.支持动态安装所需依赖包; 2.新增simple_restful模块):
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeclub committed Dec 17, 2020
1 parent 6db5c27 commit c3c19af
Show file tree
Hide file tree
Showing 56 changed files with 4,642 additions and 457 deletions.
4 changes: 3 additions & 1 deletion HiveNetLib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# -*- coding: UTF-8 -*-

__all__ = [
'generic', 'simple_i18n', 'simple_log', 'simple_stream', 'simple_cache',
'generic', 'simple_i18n', 'simple_log', 'simple_stream', 'simple_cache', 'simple_console', 'simple_grpc',
'simple_restful', 'deps_tool', 'pipeline_router', 'pipeline', 'simple_id', 'simple_server_fw',
'simple_xml',
'simple_parallel', 'simple_queue', 'prompt_plus', 'formula', 'redirect_stdout',
'base_tools', 'net_service', 'interface_tool'
]
2 changes: 1 addition & 1 deletion HiveNetLib/base_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

__all__ = [
'string_tool', 'net_tool', 'file_tool', 'import_tool', 'run_tool', 'exception_tool',
'debug_tool', 'test_tool', 'call_chain_tool', 'value_tool'
'debug_tool', 'test_tool', 'call_chain_tool', 'value_tool', 'validate_tool'
]
1 change: 0 additions & 1 deletion HiveNetLib/base_tools/call_chain_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import logging
import inspect
import copy
from queue import Full, Empty
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
Expand Down
12 changes: 8 additions & 4 deletions HiveNetLib/base_tools/file_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import platform
import subprocess
import shutil
try:
import chardet
except:
pass
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
# 动态添加包安装
import HiveNetLib.deps_tool as deps_tool
try:
import chardet
except ImportError:
deps_tool.install_package('chardet')
import chardet
# 引用自有模块
import HiveNetLib.base_tools.myzipfile as zipfile


Expand Down
30 changes: 23 additions & 7 deletions HiveNetLib/base_tools/net_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""
import os
import sys
import netifaces
import socket
import urllib
import copy
Expand All @@ -25,19 +24,36 @@
import json
import time
import datetime
import collections
import logging
import traceback
from io import BytesIO
from enum import Enum
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import selenium.webdriver.chrome.options
import selenium.webdriver.firefox.options
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
# 动态添加包安装
import HiveNetLib.deps_tool as deps_tool
process_install_selenium = False
while True:
try:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import selenium.webdriver.chrome.options
import selenium.webdriver.firefox.options
break
except ImportError:
if not process_install_selenium:
deps_tool.install_package('selenium')
process_install_selenium = True
continue
raise
try:
import netifaces
except ImportError:
deps_tool.install_package('netifaces')
import netifaces
# 引用自有模块
import HiveNetLib.base_tools.wget as wget


Expand Down
20 changes: 17 additions & 3 deletions HiveNetLib/base_tools/string_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@
"""

import copy
import os
import sys
import json
import dicttoxml
from lxml import etree
from random import Random
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
# 动态添加包安装
import HiveNetLib.deps_tool as deps_tool
try:
import dicttoxml
except ImportError:
deps_tool.install_package('dicttoxml')
import dicttoxml
try:
from lxml import etree
except ImportError:
deps_tool.install_package('lxml')
from lxml import etree


__MOUDLE__ = 'string_tool' # 模块名
Expand Down
30 changes: 14 additions & 16 deletions HiveNetLib/base_tools/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import json
import operator
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
from HiveNetLib.base_tools.string_tool import StringTool

__MOUDLE__ = 'test_tool' # 模块名
Expand Down Expand Up @@ -99,23 +100,20 @@ def cmp_dict(src_data, dst_data, print_if_diff=True):
print('cmp_dict: len difference!')
else:
src_key = list(src_data.keys())
src_key.sort()
dst_key = list(dst_data.keys())
dst_key.sort()
if operator.eq(src_key, dst_key):
src_val = list(src_data.values())
dst_val = list(dst_data.values())
if operator.eq(src_val, dst_val):
is_break = False
for key in src_data.keys():
if src_data[key] != dst_data[key]:
# print(src_data1[key])
print('cmp_dict: value difference in key "%s"!' % (key))
is_break = True
break
if not is_break:
# 如果没有中断过,则代表比较成功
return True
else:
print('cmp_dict: value list difference!')
is_break = False
for key in src_data.keys():
if src_data[key] != dst_data[key]:
# print(src_data1[key])
print('cmp_dict: value difference in key "%s"!' % (key))
is_break = True
break
if not is_break:
# 如果没有中断过,则代表比较成功
return True
else:
print('cmp_dict: key list difference!')
if print_if_diff:
Expand Down
1 change: 0 additions & 1 deletion HiveNetLib/base_tools/validate_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import datetime
import traceback
import copy
from enum import Enum
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir)))
Expand Down
2 changes: 0 additions & 2 deletions HiveNetLib/connection_pool/pool_fw.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import datetime
import time
import logging
import copy
import traceback
from queue import Empty
from enum import Enum
from abc import ABC, abstractmethod # 利用abc模块实现抽象类
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
Expand Down
160 changes: 160 additions & 0 deletions HiveNetLib/deps_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Copyright 2019 黎慧剑
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
依赖包安装工具包
@module deps_tool
@file deps_tool.py
"""

import sys
import subprocess


__MOUDLE__ = 'deps_tool' # 模块名
__DESCRIPT__ = u'依赖包安装工具包' # 模块描述
__VERSION__ = '0.1.0' # 版本
__AUTHOR__ = u'黎慧剑' # 作者
__PUBLISH__ = '2020.11.30' # 发布日期


# 依赖字典, key为包名, value为安装的包及版本要求
DEPENDENCIES_DICT = {
'gevent': {
'install': 'gevent',
},
'prompt-toolkit': {
'install': 'prompt-toolkit>=2.0.0',
},
'affinity': {
'install': 'affinity>=0.1.0',
},
'dicttoxml': {
'install': 'dicttoxml>=1.7.4',
},
'grpcio': {
'install': 'grpcio>=1.21.1',
},
'grpcio-health': {
'install': 'grpcio-health-checking>=1.21.1',
},
'lxml': {
'install': 'lxml',
},
'googleapis-common-protos': {
'install': 'googleapis-common-protos',
},
'jsonpath-rw': {
'install': 'jsonpath-rw',
},
'netifaces': {
'install': 'netifaces>=0.10.9',
},
'chardet': {
'install': 'chardet',
},
'selenium': {
'install': 'selenium',
},
'flask-cors': {
'install': 'flask-cors',
},
'flask': {
'install': 'flask',
},
'flask-restful': {
'install': 'flask-restful'
},
'pycryptodome': {
'install': 'pycryptodome' if sys.platform != 'win32' else 'pycryptodomex'
}
}


def install_package(package_name: str, force_reinstall: bool = False) -> tuple:
"""
安装指定依赖包
@param {str} package_name - 要安装的包名(DEPENDENCIES_DICT中的key)
@param {bool} force_reinstall=False - 是否强制重新安装
@returns {tuple[int, str]} - 安装结果,
第一位为运行结果,0代表成本,其他代表失败
第二位为命令安装结果输出内容
"""
_result = subprocess.getstatusoutput(
'pip install %s%s' % (
'--force-reinstall ' if force_reinstall else '',
DEPENDENCIES_DICT[package_name]['install']
)
)
if _result[0] == 0:
# 安装成功
print('安装依赖包 %s 成功' % package_name)
else:
# 安装失败
print('安装依赖包 %s 失败\n%s\n' % (package_name, _result))

return _result


def install_all(force_reinstall: bool = False) -> bool:
"""
安装所有依赖包
@param {bool} force_reinstall=False - 是否强制重新安装
@returns {bool} - 最后安装情况
"""
_fail_list = []
for _key in DEPENDENCIES_DICT.keys():
_result = install_package(_key, force_reinstall=force_reinstall)
if _result[0] != 0:
# 安装失败
_fail_list.append(_key)

# 打印最后结果
if len(_fail_list) > 0:
print('以下依赖包安装失败: %s' % ', '.join(_fail_list))
return False
else:
return True


if __name__ == '__main__':
# 当程序自己独立运行时执行的操作
if len(sys.argv) <= 1:
# 打印版本信息
print(('模块名:%s - %s\n'
'作者:%s\n'
'发布日期:%s\n'
'版本:%s\n'
'使用方法:\n%s'
% (
__MOUDLE__, __DESCRIPT__, __AUTHOR__, __PUBLISH__, __VERSION__,
'\n'.join([
' 安装全部依赖包: python deps_tool.py install',
' 安装单个依赖包: python deps_tool.py install flask',
' 强制重新安装全部依赖包: python deps_tool.py install -f'
])
)))
else:
# 按命令参数执行
_force_reinstall = False
if '-f' in sys.argv:
_force_reinstall = True

if sys.argv[1] == 'install':
# 安装依赖包
if sys.argv[-1] not in ('install', '-f'):
# 安装单个包
_package_name = sys.argv[-1]
install_package(_package_name, force_reinstall=_force_reinstall)
else:
# 安装全部
install_all(force_reinstall=_force_reinstall)
1 change: 0 additions & 1 deletion HiveNetLib/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import sys
import os
import json
import copy
import re
# 根据当前文件路径将包路径纳入,在非安装的情况下可以引用到
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
Expand Down
10 changes: 9 additions & 1 deletion HiveNetLib/hivenet_error_code/errorcode_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@
"validators failed": "validators failed",
"get info failed": "get info failed",
"control check failed": "control check failed",
"user confirmation required": "user confirmation required"
"user confirmation required": "user confirmation required",
"signature error":"signature error",
"timestamp expired":"timestamp expired",
"ip address verify failure": "ip address verify failure",
"username format error": "username format error",
"username is not existed": "username is not existed",
"username exists": "username exists",
"user is locked": "user is locked",
"user status is abnormal": "user status is abnormal"
}
10 changes: 9 additions & 1 deletion HiveNetLib/hivenet_error_code/errorcode_zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@
"validators failed": "校验失败",
"get info failed": "获取信息失败",
"control check failed": "控制检查失败",
"user confirmation required": "需要用户确认"
"user confirmation required": "需要用户确认",
"signature error":"签名检查失败",
"timestamp expired":"时间戳已过期",
"ip address verify failure": "IP地址验证失败",
"username format error": "用户名格式错误",
"username exists": "用户名已存在",
"username is not existed": "用户名不存在",
"user is locked": "用户被锁定",
"user status is abnormal": "用户状态异常"
}

0 comments on commit c3c19af

Please sign in to comment.