From a9cf38a5a867a47e7f1ba3025846fb81b9e2dbf7 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Fri, 28 Aug 2020 20:45:48 +0800 Subject: [PATCH] [Core][Bug] Move generated packages into skywalking namespace (#72) The current version generates codes from proto files into a global namespace, which causes failure when there is a package in the users' application whose name is the same as the generated one, this patch moves the generated codes into the `skywalking` module and thus avoid conflicts. Resolves https://github.com/apache/skywalking/issues/5406 --- .gitignore | 1 + MANIFEST.in | 6 ---- Makefile | 13 +++----- skywalking/agent/protocol/grpc.py | 4 +-- skywalking/client/grpc.py | 8 ++--- tools/codegen.py | 53 +++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 21 deletions(-) delete mode 100644 MANIFEST.in create mode 100644 tools/codegen.py diff --git a/.gitignore b/.gitignore index d7ffd47..b0b2fe2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ dist/ /apache_skywalking.egg-info/ **/venv/ tests/**/requirements.txt +skywalking/protocol diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index b120145..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,6 +0,0 @@ -include browser/*.py -include common/*.py -include language_agent/*.py -include management/*.py -include profile/*.py -include service_mesh_probe/*.py \ No newline at end of file diff --git a/Makefile b/Makefile index 92c4508..1a8e9fe 100644 --- a/Makefile +++ b/Makefile @@ -27,13 +27,7 @@ setup-test: setup gen: python3 -m grpc_tools.protoc --version || python3 -m pip install grpcio-tools - python3 -m grpc_tools.protoc -I protocol --python_out=. --grpc_python_out=. protocol/**/*.proto - touch browser/__init__.py - touch common/__init__.py - touch language_agent/__init__.py - touch management/__init__.py - touch profile/__init__.py - touch service_mesh_probe/__init__.py + python3 tools/codegen.py lint: clean flake8 --version || python3 -m pip install flake8 @@ -59,10 +53,11 @@ upload: package twine upload dist/* clean: - rm -rf browser common language_agent management profile service_mesh_probe - rm -rf skywalking_python.egg-info dist build + rm -rf skywalking/protocol + rm -rf apache_skywalking.egg-info dist build rm -rf skywalking-python*.tgz* find . -name "__pycache__" -exec rm -r {} + + find . -name ".pytest_cache" -exec rm -r {} + find . -name "*.pyc" -exec rm -r {} + release: clean lint license diff --git a/skywalking/agent/protocol/grpc.py b/skywalking/agent/protocol/grpc.py index ca24895..c026f99 100644 --- a/skywalking/agent/protocol/grpc.py +++ b/skywalking/agent/protocol/grpc.py @@ -20,12 +20,12 @@ from queue import Queue import grpc -from common.Common_pb2 import KeyStringValuePair -from language_agent.Tracing_pb2 import SegmentObject, SpanObject, Log, SegmentReference from skywalking import config from skywalking.agent import Protocol from skywalking.agent.protocol.interceptors import header_adder_interceptor from skywalking.client.grpc import GrpcServiceManagementClient, GrpcTraceSegmentReportService +from skywalking.protocol.common.Common_pb2 import KeyStringValuePair +from skywalking.protocol.language_agent.Tracing_pb2 import SegmentObject, SpanObject, Log, SegmentReference from skywalking.trace.segment import Segment logger = logging.getLogger(__name__) diff --git a/skywalking/client/grpc.py b/skywalking/client/grpc.py index 7849b14..7944a77 100644 --- a/skywalking/client/grpc.py +++ b/skywalking/client/grpc.py @@ -18,13 +18,13 @@ import logging import grpc -from common.Common_pb2 import KeyStringValuePair -from language_agent.Tracing_pb2_grpc import TraceSegmentReportServiceStub -from management.Management_pb2 import InstancePingPkg, InstanceProperties -from management.Management_pb2_grpc import ManagementServiceStub from skywalking import config from skywalking.client import ServiceManagementClient, TraceSegmentReportService +from skywalking.protocol.common.Common_pb2 import KeyStringValuePair +from skywalking.protocol.language_agent.Tracing_pb2_grpc import TraceSegmentReportServiceStub +from skywalking.protocol.management.Management_pb2 import InstancePingPkg, InstanceProperties +from skywalking.protocol.management.Management_pb2_grpc import ManagementServiceStub logger = logging.getLogger(__name__) diff --git a/tools/codegen.py b/tools/codegen.py new file mode 100644 index 0000000..9016423 --- /dev/null +++ b/tools/codegen.py @@ -0,0 +1,53 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import glob +import os +import re + +from grpc_tools import protoc + +dest_dir = 'skywalking/protocol' +src_dir = 'protocol' + + +def touch(filename): + open(filename, 'a').close() + + +def codegen(): + if not os.path.exists(dest_dir): + os.mkdir(dest_dir) + + touch(os.path.join(dest_dir, '__init__.py')) + + protoc.main(['grpc_tools.protoc', + '--proto_path=' + src_dir, + '--python_out=' + dest_dir, + '--grpc_python_out=' + dest_dir + ] + [proto for proto in glob.iglob(src_dir + '/**/*.proto')]) + + for py_file in glob.iglob(os.path.join(dest_dir, '**/*.py')): + touch(os.path.join(os.path.dirname(py_file), '__init__.py')) + with open(py_file, 'r+') as file: + code = file.read() + file.seek(0) + file.write(re.sub(r'from (.+) (import .+_pb2.*)', 'from ..\\1 \\2', code)) + file.truncate() + + +if __name__ == '__main__': + codegen() -- GitLab