Skip to content

Commit a9cf38a

Browse files
authored
[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 apache/skywalking#5406
1 parent a92eda3 commit a9cf38a

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist/
1010
/apache_skywalking.egg-info/
1111
**/venv/
1212
tests/**/requirements.txt
13+
skywalking/protocol

MANIFEST.in

Lines changed: 0 additions & 6 deletions
This file was deleted.

Makefile

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ setup-test: setup
2727

2828
gen:
2929
python3 -m grpc_tools.protoc --version || python3 -m pip install grpcio-tools
30-
python3 -m grpc_tools.protoc -I protocol --python_out=. --grpc_python_out=. protocol/**/*.proto
31-
touch browser/__init__.py
32-
touch common/__init__.py
33-
touch language_agent/__init__.py
34-
touch management/__init__.py
35-
touch profile/__init__.py
36-
touch service_mesh_probe/__init__.py
30+
python3 tools/codegen.py
3731

3832
lint: clean
3933
flake8 --version || python3 -m pip install flake8
@@ -59,10 +53,11 @@ upload: package
5953
twine upload dist/*
6054

6155
clean:
62-
rm -rf browser common language_agent management profile service_mesh_probe
63-
rm -rf skywalking_python.egg-info dist build
56+
rm -rf skywalking/protocol
57+
rm -rf apache_skywalking.egg-info dist build
6458
rm -rf skywalking-python*.tgz*
6559
find . -name "__pycache__" -exec rm -r {} +
60+
find . -name ".pytest_cache" -exec rm -r {} +
6661
find . -name "*.pyc" -exec rm -r {} +
6762

6863
release: clean lint license

skywalking/agent/protocol/grpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
import grpc
2222

23-
from common.Common_pb2 import KeyStringValuePair
24-
from language_agent.Tracing_pb2 import SegmentObject, SpanObject, Log, SegmentReference
2523
from skywalking import config
2624
from skywalking.agent import Protocol
2725
from skywalking.agent.protocol.interceptors import header_adder_interceptor
2826
from skywalking.client.grpc import GrpcServiceManagementClient, GrpcTraceSegmentReportService
27+
from skywalking.protocol.common.Common_pb2 import KeyStringValuePair
28+
from skywalking.protocol.language_agent.Tracing_pb2 import SegmentObject, SpanObject, Log, SegmentReference
2929
from skywalking.trace.segment import Segment
3030

3131
logger = logging.getLogger(__name__)

skywalking/client/grpc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import logging
1919

2020
import grpc
21-
from common.Common_pb2 import KeyStringValuePair
22-
from language_agent.Tracing_pb2_grpc import TraceSegmentReportServiceStub
23-
from management.Management_pb2 import InstancePingPkg, InstanceProperties
24-
from management.Management_pb2_grpc import ManagementServiceStub
2521

2622
from skywalking import config
2723
from skywalking.client import ServiceManagementClient, TraceSegmentReportService
24+
from skywalking.protocol.common.Common_pb2 import KeyStringValuePair
25+
from skywalking.protocol.language_agent.Tracing_pb2_grpc import TraceSegmentReportServiceStub
26+
from skywalking.protocol.management.Management_pb2 import InstancePingPkg, InstanceProperties
27+
from skywalking.protocol.management.Management_pb2_grpc import ManagementServiceStub
2828

2929
logger = logging.getLogger(__name__)
3030

tools/codegen.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
import glob
18+
import os
19+
import re
20+
21+
from grpc_tools import protoc
22+
23+
dest_dir = 'skywalking/protocol'
24+
src_dir = 'protocol'
25+
26+
27+
def touch(filename):
28+
open(filename, 'a').close()
29+
30+
31+
def codegen():
32+
if not os.path.exists(dest_dir):
33+
os.mkdir(dest_dir)
34+
35+
touch(os.path.join(dest_dir, '__init__.py'))
36+
37+
protoc.main(['grpc_tools.protoc',
38+
'--proto_path=' + src_dir,
39+
'--python_out=' + dest_dir,
40+
'--grpc_python_out=' + dest_dir
41+
] + [proto for proto in glob.iglob(src_dir + '/**/*.proto')])
42+
43+
for py_file in glob.iglob(os.path.join(dest_dir, '**/*.py')):
44+
touch(os.path.join(os.path.dirname(py_file), '__init__.py'))
45+
with open(py_file, 'r+') as file:
46+
code = file.read()
47+
file.seek(0)
48+
file.write(re.sub(r'from (.+) (import .+_pb2.*)', 'from ..\\1 \\2', code))
49+
file.truncate()
50+
51+
52+
if __name__ == '__main__':
53+
codegen()

0 commit comments

Comments
 (0)