You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/08/28 11:55:54 UTC

[skywalking-python] branch bugfix/codegen created (now 20f8051)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a change to branch bugfix/codegen
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git.


      at 20f8051  Move generated packages into skywalking namespace

This branch includes the following new commits:

     new 20f8051  Move generated packages into skywalking namespace

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[skywalking-python] 01/01: Move generated packages into skywalking namespace

Posted by ke...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch bugfix/codegen
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git

commit 20f80518fece66ab46fbf2c5fe8f4147483e7e72
Author: kezhenxu94 <ke...@163.com>
AuthorDate: Fri Aug 28 19:55:36 2020 +0800

    Move generated packages into skywalking namespace
    
    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                          | 10 ++------
 skywalking/agent/protocol/grpc.py |  4 +--
 skywalking/client/grpc.py         |  8 +++---
 tools/codegen.py                  | 53 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 62 insertions(+), 20 deletions(-)

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..11b079a 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,7 +53,7 @@ upload: package
 	twine upload dist/*
 
 clean:
-	rm -rf browser common language_agent management profile service_mesh_probe
+	rm -rf skywalking/protocol
 	rm -rf skywalking_python.egg-info dist build
 	rm -rf skywalking-python*.tgz*
 	find . -name "__pycache__" -exec rm -r {} +
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()