From 30a0e82a5a89354c4fce735f75daa46c5e239a9f Mon Sep 17 00:00:00 2001 From: MrLYC Date: Wed, 24 Mar 2021 21:07:02 +0800 Subject: [PATCH] fix commands --- paas2/esb/apps/sdk_management/utils.py | 23 +++++++++++-------- paas2/esb/common/forms.py | 16 ++++++++----- .../commands/add_compperm_for_app.py | 15 ++++-------- .../esb/management/commands/check_service.py | 7 ++---- .../esb/management/commands/sync_api_docs.py | 7 ++---- .../commands/sync_system_and_channel_data.py | 9 +++----- .../esb/management/commands/update_locale.py | 22 +++++++----------- paas2/esb/esb/outgoing.py | 6 ++--- paas2/esb/esb/utils/func_ctrl.py | 7 +++++- 9 files changed, 52 insertions(+), 60 deletions(-) diff --git a/paas2/esb/apps/sdk_management/utils.py b/paas2/esb/apps/sdk_management/utils.py index de20912b2..1afd9dbc7 100644 --- a/paas2/esb/apps/sdk_management/utils.py +++ b/paas2/esb/apps/sdk_management/utils.py @@ -12,13 +12,11 @@ import os import shutil -import string +from apps.sdk_management.constants import API_COMPONENT_TMPL, API_PY_TMPL, COLLECTIONS_PY_TMPL from django.conf import settings from jinja2 import Template -from apps.sdk_management.constants import COLLECTIONS_PY_TMPL, API_PY_TMPL, API_COMPONENT_TMPL - class SDKGenerator(object): def __init__(self, channels, target_dir): @@ -33,7 +31,7 @@ def generate_sdk_files(self): def get_available_channels(self, channels): new_channels = {} - for system_name, sub_channels in channels.iteritems(): + for system_name, sub_channels in channels.items(): new_sub_channels = [ channel for channel in sub_channels if channel["suggest_method"] and not channel["no_sdk"] ] @@ -101,15 +99,17 @@ def get_api_file_content(self, system_name, channels): description=channel["component_label"].encode("utf-8"), ) ) + + apis_str = "".join(apis) return Template(API_PY_TMPL).render( system_name_smart=self.smart_system_name(system_name), system_name=system_name, - apis="".join(apis).decode("utf-8"), + apis=apis_str.decode("utf-8") if hasattr(apis_str, "decode") else apis_str, ) def smart_system_name(self, system_name): if "_" in system_name: - system_name = "".join(string.capitalize(word) for word in system_name.split("_")) + system_name = "".join(word.capitalize() for word in system_name.split("_")) return system_name def write_content_to_file(self, content, file_path): @@ -134,10 +134,13 @@ def group_channels_by_api_ver(self, channels): for path, channel in channels_v2.items(): if path in channels_v1: if channels_v1[path]["suggest_method"] != channel["suggest_method"]: - print "channel method different: v1=%s, v2=%s, path=%s" % ( - channels_v1[path]["suggest_method"], - channel["suggest_method"], - path, + print( + "channel method different: v1=%s, v2=%s, path=%s" + % ( + channels_v1[path]["suggest_method"], + channel["suggest_method"], + path, + ) ) channels_v1_v2.append(channel) channels_v1.pop(path) diff --git a/paas2/esb/common/forms.py b/paas2/esb/common/forms.py index fc3ab44eb..92c83bcc8 100644 --- a/paas2/esb/common/forms.py +++ b/paas2/esb/common/forms.py @@ -10,15 +10,15 @@ specific language governing permissions and limitations under the License. """ -import re import json +import re from django import forms from django.core import validators from django.core.exceptions import ValidationError -from django.utils.encoding import force_unicode, smart_unicode from django.forms import Field from django.forms.utils import ErrorDict +from django.utils.encoding import force_text, smart_text from common.base_utils import FancyDict, str_bool from common.errors import CommonAPIError @@ -31,7 +31,7 @@ def get_error_prompt(form): content = [] fields = list(form.fields.keys()) for k, v in sorted(list(form.errors.items()), key=lambda x: fields.index(x[0]) if x[0] in fields else -1): - _msg = force_unicode(v[0]) + _msg = force_text(v[0]) b_field = form._safe_get_field(k) # Get the default error messages messages = {} @@ -43,7 +43,7 @@ def get_error_prompt(form): content.append(u"%s [%s] %s" % (b_field.label, b_field.name, _msg)) else: content.append(u"%s" % _msg) - return force_unicode(content[0]) + return force_text(content[0]) class BaseComponentForm(forms.Form): @@ -121,7 +121,11 @@ def get_cleaned_data_when_exist(self, keys=[]): keys = keys or list(self.fields.keys()) if isinstance(keys, dict): return dict( - [(key_dst, self.cleaned_data[key_src]) for key_src, key_dst in list(keys.items()) if key_src in self.data] + [ + (key_dst, self.cleaned_data[key_src]) + for key_src, key_dst in list(keys.items()) + if key_src in self.data + ] ) else: return dict([(key, self.cleaned_data[key]) for key in keys if key in self.data]) @@ -149,7 +153,7 @@ def to_python_unicode(self, value): "Returns a Unicode object." if value in validators.EMPTY_VALUES: return "" - return smart_unicode(value) + return smart_text(value) def to_python(self, value): # 如果传入的数据类型本身就是list( 比如用json loads过来的数据结构来校验),直接返回 diff --git a/paas2/esb/esb/management/commands/add_compperm_for_app.py b/paas2/esb/esb/management/commands/add_compperm_for_app.py index fb099d098..119cee8e2 100644 --- a/paas2/esb/esb/management/commands/add_compperm_for_app.py +++ b/paas2/esb/esb/management/commands/add_compperm_for_app.py @@ -11,20 +11,15 @@ """ from __future__ import print_function -from optparse import make_option - from django.core.management.base import BaseCommand, CommandError - -from esb.bkcore.models import ESBChannel, AppComponentPerm +from esb.bkcore.models import AppComponentPerm, ESBChannel class Command(BaseCommand): - - option_list = BaseCommand.option_list + ( - make_option("--app_code", action="store", dest="app_code"), - make_option("--system_name", action="store", dest="system_name"), - make_option("--component_name", action="store", dest="component_name"), - ) + def add_arguments(self, parser): + parser.add_argument("--app_code", action="store", dest="app_code") + parser.add_argument("--system_name", action="store", dest="system_name") + parser.add_argument("--component_name", action="store", dest="component_name") def handle(self, *args, **options): app_code = options["app_code"] diff --git a/paas2/esb/esb/management/commands/check_service.py b/paas2/esb/esb/management/commands/check_service.py index 2da5e8dc9..9fff1014a 100644 --- a/paas2/esb/esb/management/commands/check_service.py +++ b/paas2/esb/esb/management/commands/check_service.py @@ -12,7 +12,6 @@ from __future__ import print_function import json -from optparse import make_option from django.core.management.base import BaseCommand @@ -22,10 +21,8 @@ class Command(BaseCommand): - - option_list = BaseCommand.option_list + ( - make_option("--service", action="store", dest="service", help="Service name"), - ) + def add_arguments(self, parser): + parser.add_argument("--service", action="store", dest="service", help="Service name") def handle(self, *args, **options): self.check_job_ssl() diff --git a/paas2/esb/esb/management/commands/sync_api_docs.py b/paas2/esb/esb/management/commands/sync_api_docs.py index 03ec16612..3b968374a 100644 --- a/paas2/esb/esb/management/commands/sync_api_docs.py +++ b/paas2/esb/esb/management/commands/sync_api_docs.py @@ -12,7 +12,6 @@ import json import logging -from optparse import make_option from django.core.management.base import BaseCommand from esb.bkcore.models import ComponentAPIDoc, ESBChannel @@ -26,10 +25,8 @@ class Command(BaseCommand): - - option_list = BaseCommand.option_list + ( - make_option("--all", action="store_true", dest="all", default=False, help="update all api docs"), - ) + def add_arguments(self, parser): + parser.add_argument("--all", action="store_true", dest="all", default=False, help="update all api docs") def handle(self, *args, **options): self.update_api_docs(is_update_all_api_doc=options["all"]) diff --git a/paas2/esb/esb/management/commands/sync_system_and_channel_data.py b/paas2/esb/esb/management/commands/sync_system_and_channel_data.py index 1fc9f36f0..64ec1907e 100644 --- a/paas2/esb/esb/management/commands/sync_system_and_channel_data.py +++ b/paas2/esb/esb/management/commands/sync_system_and_channel_data.py @@ -10,15 +10,14 @@ specific language governing permissions and limitations under the License. """ -from past.builtins import basestring import json import logging -from optparse import make_option from common.constants import API_TYPE_Q from django.core.management.base import BaseCommand from esb.bkcore.models import ComponentSystem, ESBBuffetComponent, ESBChannel, SystemDocCategory from esb.management.utils import conf_tools +from past.builtins import basestring logger = logging.getLogger(__name__) @@ -28,10 +27,8 @@ class Command(BaseCommand): - - option_list = BaseCommand.option_list + ( - make_option("--force", action="store_true", dest="force", help="Force data update to db"), - ) + def add_arguments(self, parser): + parser.add_argument("--force", action="store_true", dest="force", help="Force data update to db") def handle(self, *args, **options): self.force = options["force"] diff --git a/paas2/esb/esb/management/commands/update_locale.py b/paas2/esb/esb/management/commands/update_locale.py index 13e328c02..2aed11921 100644 --- a/paas2/esb/esb/management/commands/update_locale.py +++ b/paas2/esb/esb/management/commands/update_locale.py @@ -10,32 +10,26 @@ specific language governing permissions and limitations under the License. """ -from builtins import map -from builtins import object -import re -import os import copy import json -from optparse import make_option +import logging +import os +import re +from builtins import map, object +from common.base_utils import read_file from django.conf import settings from django.core.management.base import BaseCommand -from common.base_utils import read_file - -import logging - logger = logging.getLogger(__name__) BASE_DIR = settings.BASE_DIR class Command(BaseCommand): - - option_list = BaseCommand.option_list + ( - make_option("--force", action="store_true", dest="force", help="Force update locale file"), - make_option("--parse", action="store_true", dest="parse", help="only parse and print translation info"), - ) + def add_arguments(self, parser): + parser.add_argument("--force", action="store_true", dest="force", help="Force update locale file") + parser.add_argument("--parse", action="store_true", dest="parse", help="only parse and print translation info") def handle(self, *args, **options): self.force = options["force"] diff --git a/paas2/esb/esb/outgoing.py b/paas2/esb/esb/outgoing.py index f7e5d6492..d4778f282 100644 --- a/paas2/esb/esb/outgoing.py +++ b/paas2/esb/esb/outgoing.py @@ -32,7 +32,7 @@ from common.log import logger, logger_api from django.conf import settings from django.utils import timezone -from django.utils.encoding import smart_str +from django.utils.encoding import smart_bytes from past.builtins import basestring from requests.exceptions import ReadTimeout, SSLError @@ -409,7 +409,7 @@ def request( params = json.dumps(params) datetime_end = timezone.now() msecs_cost = (datetime_end - datetime_start).total_seconds() * 1000 - exception_name = smart_str(r.request_exception) if r.request_exception else None + exception_name = smart_bytes(r.request_exception) if r.request_exception else None try: api_log = { @@ -553,7 +553,7 @@ def request( request_params = str(request_params) datetime_end = timezone.now() msecs_cost = (datetime_end - datetime_start).total_seconds() * 1000 - exception_name = smart_str(request_exception) if request_exception else None + exception_name = smart_bytes(request_exception) if request_exception else None # Log to logstash, Use type="pyls-comp-api" try: diff --git a/paas2/esb/esb/utils/func_ctrl.py b/paas2/esb/esb/utils/func_ctrl.py index aa8900508..25d6e74b5 100644 --- a/paas2/esb/esb/utils/func_ctrl.py +++ b/paas2/esb/esb/utils/func_ctrl.py @@ -12,7 +12,7 @@ import json import re -from builtins import object +from builtins import bytes, object from cachetools import TTLCache, cached from common.constants import CACHE_MAXSIZE, CacheTimeLevel, FunctionControllerCodeEnum @@ -64,6 +64,11 @@ def get_jwt_key(cls): @classmethod def save_jwt_key(cls, private_key, public_key): + if isinstance(private_key, bytes): + private_key = private_key.decode("utf-8") + + if isinstance(public_key, bytes): + public_key = public_key.decode("utf-8") FunctionController.objects.get_or_create( func_code=FunctionControllerCodeEnum.JWT_KEY.value,