Skip to content

Commit 9ff2078

Browse files
authored
Merge pull request #66 Support protubuf 3 and protobuf 4 same time
2 parents f4e97f4 + 3981577 commit 9ff2078

File tree

202 files changed

+11302
-118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+11302
-118
lines changed

.github/workflows/tests.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: Functional tests
22

33
on:
44
push:
5+
- master
56
pull_request:
67

78
jobs:
@@ -11,7 +12,7 @@ jobs:
1112
max-parallel: 4
1213
matrix:
1314
python-version: [3.8]
14-
environment: [py38, py38-tls]
15+
environment: [py38, py38-tls, py38-proto3, py38-tls-proto3]
1516

1617
steps:
1718
- uses: actions/checkout@v1

Makefile

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
protobuf:
2-
docker build -f generate-protobuf.Dockerfile . -t ydb-python-sdk-proto-generator-env
3-
docker run --rm -it -v $${PWD}:$${PWD} -w $${PWD} ydb-python-sdk-proto-generator-env python generate_protoc.py
1+
protobuf-3:
2+
docker build -f generate-protobuf.Dockerfile . -t ydb-python-sdk-proto-generator-env-3 --build-arg GRPCIO_VER=1.39.0 --build-arg PY_PROTOBUF_VER=3.20.3
3+
docker run --rm -it -v $${PWD}:$${PWD} -w $${PWD} ydb-python-sdk-proto-generator-env-3 python generate_protoc.py --target-version=v3
4+
5+
protobuf-4:
6+
docker build -f generate-protobuf.Dockerfile . -t ydb-python-sdk-proto-generator-env-4
7+
docker run --rm -it -v $${PWD}:$${PWD} -w $${PWD} ydb-python-sdk-proto-generator-env-4 python generate_protoc.py
8+
9+
protobuf: protobuf-3 protobuf-4

generate-protobuf.Dockerfile

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
FROM python:3.9.15
2+
ENV GRPCLIB_VER=0.4.3
3+
ARG GRPCIO_VER=1.50.0
4+
ARG PY_PROTOBUF_VER=4.21.9
25
RUN \
36
python -m pip install --upgrade pip && \
4-
python -m pip install grpcio==1.39.0 && \
5-
python -m pip install grpclib && \
6-
python -m pip install protobuf==3.20.3 && \
7-
python -m pip install grpcio-tools==1.38.0
7+
python -m pip install grpcio==${GRPCIO_VER} && \
8+
python -m pip install grpclib==${GRPCLIB_VER} && \
9+
python -m pip install grpcio-tools==${GRPCIO_VER} && \
10+
python -m pip install protobuf==${PY_PROTOBUF_VER}
811

912
ENV PROTOC_VER=21.8
1013
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/protoc-${PROTOC_VER}-linux-x86_64.zip && \

generate_protoc.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import pathlib
33
import shutil
4+
45
from typing import List
6+
from argparse import ArgumentParser
57

68
from grpc_tools import command
79

@@ -35,8 +37,9 @@ def remove_protos(rootdirpath: str):
3537
os.remove(os.path.join(root, file))
3638

3739

38-
def fix_file_contents(rootdir: str):
40+
def fix_file_contents(rootdir, protobuf_version: str):
3941
flake_ignore_line = "# flake8: " + "noqa" # prevent ignore the file
42+
package_path = "ydb._grpc." + protobuf_version + ".protos"
4043

4144
for dirpath, _, fnames in os.walk(rootdir):
4245
for fname in fnames:
@@ -47,24 +50,30 @@ def fix_file_contents(rootdir: str):
4750
content = f.read()
4851

4952
# Fix imports
50-
content = content.replace("from protos", "from ydb._grpc.protos")
53+
content = content.replace("from protos", "from " + package_path)
5154

5255
# Add ignore style check
5356
content = content.replace("# -*- coding: utf-8 -*-", "# -*- coding: utf-8 -*-\n" + flake_ignore_line)
5457
f.seek(0)
5558
f.write(content)
5659

5760

58-
def generate_protobuf(src_proto_dir: str, dst_dir: str):
61+
def generate_protobuf(src_proto_dir: str, dst_dir, protobuf_version: str):
5962
shutil.rmtree(dst_dir, ignore_errors=True)
6063

6164
shutil.copytree(src_proto_dir, dst_dir, ignore=files_filter)
62-
create_init_files(dst_dir)
6365

6466
command.build_package_protos(dst_dir)
6567
remove_protos(dst_dir)
66-
fix_file_contents(dst_dir)
68+
create_init_files(dst_dir)
69+
fix_file_contents(dst_dir, protobuf_version)
6770

6871

6972
if __name__ == '__main__':
70-
generate_protobuf("ydb-api-protos", "ydb/_grpc")
73+
parser = ArgumentParser()
74+
parser.add_argument("--target-version", default="v4", help="Protobuf version")
75+
76+
args = parser.parse_args()
77+
78+
target_dir = "ydb/_grpc/" + args.target_version
79+
generate_protobuf("ydb-api-protos", target_dir, args.target_version)

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
grpcio==1.39.0
2-
protobuf>3.17.3,<4.0.0
2+
packaging
3+
protobuf>3.13.0,<5.0.0
34
pytest==6.2.4
45
aiohttp==3.7.4

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"Programming Language :: Python :: 3.6",
2525
],
2626
install_requires=(
27-
"protobuf>=3.13.0,<4.0.0",
27+
"protobuf>=3.13.0",
2828
"grpcio>=1.5.0",
2929
"enum-compat>=0.0.1",
3030
),

test-requirements.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ docker==5.0.0
1010
docker-compose==1.29.2
1111
dockerpty==0.4.1
1212
docopt==0.6.2
13-
grpcio==1.39.0
14-
grpcio-tools==1.39.0
13+
grpcio
14+
grpcio-tools
1515
idna==3.2
1616
importlib-metadata==4.6.1
1717
iniconfig==1.1.1
1818
jsonschema==3.2.0
1919
packaging==21.0
2020
paramiko==2.10.1
2121
pluggy==0.13.1
22-
protobuf>3.17.3,<4.0.0
22+
protobuf>3.13.0,<5.0.0
2323
py==1.10.0
2424
pycparser==2.20
2525
PyNaCl==1.4.0

tox.ini

+18-8
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,37 @@ setenv =
1313
deps =
1414
-r{toxinidir}/test-requirements.txt
1515

16-
[testenv:protoc]
16+
[testenv:dev]
1717
commands =
18-
python3 generate_protoc.py
1918

20-
21-
[testenv:py36]
19+
[testenv:dev-proto3]
2220
commands =
23-
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
21+
deps =
22+
-r{toxinidir}/test-requirements.txt
23+
protobuf<4.0.0
2424

25-
[testenv:py36-tls]
25+
[testenv:py38]
2626
commands =
27-
pytest -v -m tls --docker-compose-remove-volumes --docker-compose=docker-compose-tls.yml {posargs}
27+
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
2828

29-
[testenv:py38]
29+
[testenv:py38-proto3]
3030
commands =
3131
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}
32+
deps =
33+
-r{toxinidir}/test-requirements.txt
34+
protobuf<4.0.0
3235

3336
[testenv:py38-tls]
3437
commands =
3538
pytest -v -m tls --docker-compose-remove-volumes --docker-compose=docker-compose-tls.yml {posargs}
3639

40+
[testenv:py38-tls-proto3]
41+
commands =
42+
pytest -v -m tls --docker-compose-remove-volumes --docker-compose=docker-compose-tls.yml {posargs}
43+
deps =
44+
-r{toxinidir}/test-requirements.txt
45+
protobuf<4.0.0
46+
3747
[testenv:py310]
3848
commands =
3949
pytest -v -m "not tls" --docker-compose-remove-volumes --docker-compose=docker-compose.yml {posargs}

ydb/_apis.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# -*- coding: utf-8 -*-
2-
from ydb._grpc import (
2+
from ydb._grpc.common import (
33
ydb_cms_v1_pb2_grpc,
44
ydb_discovery_v1_pb2_grpc,
55
ydb_scheme_v1_pb2_grpc,
66
ydb_table_v1_pb2_grpc,
77
)
8-
from ydb._grpc.protos import (
8+
from ydb._grpc.common.protos import (
99
ydb_status_codes_pb2,
1010
ydb_discovery_pb2,
1111
ydb_scheme_pb2,
1212
ydb_table_pb2,
1313
ydb_value_pb2,
1414
)
15-
from ydb._grpc.protos import ydb_operation_pb2
16-
from ydb._grpc.protos import ydb_common_pb2
17-
from ydb._grpc import ydb_operation_v1_pb2_grpc
15+
from ydb._grpc.common.protos import ydb_operation_pb2
16+
from ydb._grpc.common.protos import ydb_common_pb2
17+
from ydb._grpc.common import ydb_operation_v1_pb2_grpc
1818

1919

2020
StatusIds = ydb_status_codes_pb2.StatusIds

ydb/_grpc/common/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
3+
import google.protobuf
4+
from packaging.version import Version
5+
6+
# generated files are incompatible between 3 and 4 protobuf versions
7+
# import right generated version for current protobuf lib
8+
# sdk code must always import from ydb._grpc.common
9+
protobuf_version = Version(google.protobuf.__version__)
10+
11+
if protobuf_version < Version("4.0"):
12+
from ydb._grpc.v3 import * # noqa
13+
from ydb._grpc.v3 import protos # noqa
14+
sys.modules["ydb._grpc.common"] = sys.modules["ydb._grpc.v3"]
15+
sys.modules["ydb._grpc.common.protos"] = sys.modules["ydb._grpc.v3.protos"]
16+
else:
17+
from ydb._grpc.v4 import * # noqa
18+
from ydb._grpc.v4 import protos # noqa
19+
sys.modules["ydb._grpc.common"] = sys.modules["ydb._grpc.v4"]
20+
sys.modules["ydb._grpc.common.protos"] = sys.modules["ydb._grpc.v4.protos"]
File renamed without changes.

ydb/_grpc/v3/protos/annotations/__init__.py

Whitespace-only changes.

ydb/_grpc/protos/ydb_auth_pb2.py renamed to ydb/_grpc/v3/protos/ydb_auth_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_cms_pb2.py renamed to ydb/_grpc/v3/protos/ydb_cms_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_coordination_pb2.py renamed to ydb/_grpc/v3/protos/ydb_coordination_pb2.py

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_discovery_pb2.py renamed to ydb/_grpc/v3/protos/ydb_discovery_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_export_pb2.py renamed to ydb/_grpc/v3/protos/ydb_export_pb2.py

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_import_pb2.py renamed to ydb/_grpc/v3/protos/ydb_import_pb2.py

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_monitoring_pb2.py renamed to ydb/_grpc/v3/protos/ydb_monitoring_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_operation_pb2.py renamed to ydb/_grpc/v3/protos/ydb_operation_pb2.py

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_persqueue_cluster_discovery_pb2.py renamed to ydb/_grpc/v3/protos/ydb_persqueue_cluster_discovery_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_persqueue_v1_pb2.py renamed to ydb/_grpc/v3/protos/ydb_persqueue_v1_pb2.py

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_rate_limiter_pb2.py renamed to ydb/_grpc/v3/protos/ydb_rate_limiter_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_scheme_pb2.py renamed to ydb/_grpc/v3/protos/ydb_scheme_pb2.py

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ydb/_grpc/protos/ydb_scripting_pb2.py renamed to ydb/_grpc/v3/protos/ydb_scripting_pb2.py

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)