Skip to content

Commit 275a62d

Browse files
samsjajina-bot
andauthored
fix: compatibilty v2 new name (jina-ai#5784)
Signed-off-by: samsja <[email protected]> Co-authored-by: Jina Dev Bot <[email protected]>
1 parent 58b61d0 commit 275a62d

28 files changed

+1481
-62
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ jobs:
3131
run: |
3232
pip install flake8
3333
# stop the build if there are Python syntax errors or undefined names
34-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,jina/resources/,jina/proto/pb/jina_pb2.py,jina/proto/pb/jina_pb2_grpc.py,jina/proto/pb2/jina_pb2.py,jina/proto/pb2/jina_pb2_grpc.py
34+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,jina/resources/,jina/proto/docarray_v1,jina/proto/docarray_v2
3535
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
36-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,jina/resources/,jina/proto/pb/jina_pb2.py,jina/proto/pb/jina_pb2_grpc.py,jina/proto/pb2/jina_pb2.py,jina/proto/pb2/jina_pb2_grpc.py
36+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,jina/proto/docarray_v1,jina/proto/docarray_v2
3737
3838
check-docstring:
3939
runs-on: ubuntu-latest
@@ -395,9 +395,9 @@ jobs:
395395
- name: Prepare environment
396396
run: |
397397
docker build -f Dockerfiles/test-pip.Dockerfile -t jinaai/jina:test-pip .
398-
pip install -U "git+https://github.com/docarray/docarray@feat-rewrite-v2#egg=docarray[common]"
399398
python -m pip install --upgrade pip
400399
python -m pip install wheel
400+
pip install -U "git+https://github.com/docarray/docarray@feat-rewrite-v2#egg=docarray[common]"
401401
pip install ".[common,devel,test]" --no-cache-dir
402402
pip install -U protobuf${{ matrix.protobuf-version }}
403403
jina

.pre-commit-config.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ repos:
33
rev: 4.0.1
44
hooks:
55
- id: flake8
6-
exclude: ^(.git|__pycache__|docs/source/conf.py|old|build|dist|tests|jina/resources/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py)
6+
exclude: ^(.git|__pycache__|docs/source/conf.py|old|build|dist|tests|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
77
args:
88
- --max-complexity=10
99
- --max-line-length=127
@@ -13,7 +13,7 @@ repos:
1313
hooks:
1414
- id: darglint
1515
files: jina/
16-
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/)
16+
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
1717
args:
1818
- --message-template={path}:{line} {msg_id} {msg}
1919
- -s=sphinx
@@ -24,26 +24,28 @@ repos:
2424
hooks:
2525
- id: pydocstyle
2626
files: jina/
27-
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/)
27+
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
2828
args:
2929
- --select=D101,D102,D103
3030
- repo: https://github.com/ambv/black
3131
rev: 22.3.0
3232
hooks:
3333
- id: black
3434
types: [python]
35-
exclude: ^(jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/)
35+
exclude: ^(jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
3636
args:
3737
- -S
3838
- repo: https://github.com/asottile/blacken-docs
3939
rev: v1.12.1
4040
hooks:
4141
- id: blacken-docs
42+
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
4243
args:
4344
- -S
4445
- repo: https://github.com/pycqa/isort
4546
rev: 5.10.1
4647
hooks:
4748
- id: isort
49+
exclude: ^(jina/helloworld/|jina/proto/pb/jina_pb2.py|jina/proto/pb/jina_pb2_grpc.py|jina/proto/pb2/jina_pb2.py|jina/proto/pb2/jina_pb2_grpc.py|docs/|jina/resources/|jina/proto/docarray_v1|jina/proto/docarray_v2)
4850
args: ["--profile", "black"]
4951

jina/_docarray.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
try:
22
from docarray import BaseDoc as Document
3-
from docarray import DocArray as DocumentArray
3+
from docarray import DocList as DocumentArray
44

55
docarray_v2 = True
66

jina/proto/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Build proto
2+
3+
Jina support two versions of protobuf, before 3.19 and after (which is a breaking change for python), therefore we have
4+
duplicate python file generation from proto based on the installed protobuf version.
5+
6+
Moreover, jina is compatible with docarray v1 and docarray v2 that introduce breaking change in the proto definition.
7+
8+
Therefore, we end with 2 proto files, one for each version of docarray that we support. (Note in the future
9+
docarray v1 support will be dropped, and we will come back to have proto version)
10+
11+
This complex setup lead to a not straightforward way to generate the python code from the proto
12+
13+
this guide explain how to do it properly.
14+
15+
# how to build the proto
16+
17+
## 1. build docker image for protobuf generation
18+
19+
20+
``` cmd
21+
docker build -f Dockerfiles/protogen.Dockerfile -t jinaai/protogen:local .
22+
```
23+
24+
This build the docker image that will be used to generate the python code from the proto for proto **before** 3.19
25+
26+
``` cmd
27+
docker build -f Dockerfiles/protogen-3.21.Dockerfile -t jinaai/protogen-3.21:local .
28+
```
29+
30+
This build the docker image that will be used to generate the python code from the proto for proto **after* 3.19
31+
32+
## 2. generate the python code from the proto
33+
34+
note: you need to be in the root of the repo to do the following steps
35+
36+
### For DocArray v1
37+
38+
``` cmd
39+
docker run -it -v $(pwd)/jina/proto/docarray_v1:/jina/proto jinaai/protogen:local
40+
```
41+
42+
``` cmd
43+
docker run -it -v $(pwd)/jina/proto/docarray_v1:/jina/proto jinaai/protogen-3.21:local
44+
```
45+
46+
### For DocArray v2
47+
48+
``` cmd
49+
docker run -it -v $(pwd)/jina/proto/docarray_v2:/jina/proto jinaai/protogen:local
50+
```
51+
52+
``` cmd
53+
docker run -it -v $(pwd)/jina/proto/docarray_v2:/jina/proto jinaai/protogen-3.21:local
54+
```
55+
File renamed without changes.

jina/proto/build-proto.sh jina/proto/docarray_v1/build-proto.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -e
88
#
99
# current dir: jina root (the one with README.md)
1010
# run the following in bash:
11-
# docker run -v $(pwd)/jina/proto:/jina/proto jinaai/protogen
11+
# docker run -v $(pwd)/jina/proto/docarray_v2:/jina/proto jinaai/protogen
1212
# finally, set back owner of the generated files using: sudo chown -R $(id -u ${USER}):$(id -g ${USER}) ./jina/proto
1313

1414
# The protogen docker image can also be build locally using:
@@ -22,7 +22,7 @@ SRC_NAME="${MODULE}.proto"
2222

2323
COMP_PROTO_OUT_NAME="${MODULE}_pb2.py"
2424
COMP_GRPC_OUT_NAME="${MODULE}_pb2_grpc.py"
25-
PB_NAME="${2:-pb2}"
25+
PB_NAME="${2:-pb}"
2626
OUT_FOLDER="${PB_NAME}/"
2727

2828
VER_FILE=../__init__.py
@@ -50,7 +50,7 @@ printf "using linux sed syntax, if you are running this on mac, you may want to
5050
# sed -i '' -e 's/import\ jina_pb2\ as\ jina__pb2/from\ \.\ import\ jina_pb2\ as\ jina__pb2/' ${SRC_DIR}jina_pb2_grpc.py
5151
# for linux
5252
sed -i "s/import\ docarray_pb2/import\ docarray.proto.${PB_NAME}.docarray_pb2/" "${SRC_DIR}${OUT_FOLDER}jina_pb2.py"
53-
sed -i 's/import\ jina_pb2\ as\ jina__pb2/from\ \.\.\ import\ serializer\ as\ jina__pb2/' "${SRC_DIR}${OUT_FOLDER}jina_pb2_grpc.py"
53+
sed -i 's/import\ jina_pb2\ as\ jina__pb2/from\ \.\.\.\ import\ serializer\ as\ jina__pb2/' "${SRC_DIR}${OUT_FOLDER}jina_pb2_grpc.py"
5454

5555
OLDVER=$(sed -n 's/^__proto_version__ = '\''\(.*\)'\''$/\1/p' $VER_FILE)
5656
printf "current proto version:\t\e[1;33m$OLDVER\e[0m\n"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

jina/proto/pb/jina_pb2_grpc.py jina/proto/docarray_v1/pb/jina_pb2_grpc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import grpc
44

55
from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2
6-
from .. import serializer as jina__pb2
6+
from ... import serializer as jina__pb2
77

88

99
class JinaDataRequestRPCStub(object):

jina/proto/docarray_v1/pb2/__init__.py

Whitespace-only changes.

jina/proto/pb2/jina_pb2.py jina/proto/docarray_v1/pb2/jina_pb2.py

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

jina/proto/pb2/jina_pb2_grpc.py jina/proto/docarray_v1/pb2/jina_pb2_grpc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
22
"""Client and server classes corresponding to protobuf-defined services."""
33
import grpc
4-
54
from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2
6-
from .. import serializer as jina__pb2
5+
6+
from ... import serializer as jina__pb2
77

88

99
class JinaDataRequestRPCStub(object):

jina/proto/docarray_v2/__init__.py

Whitespace-only changes.

jina/proto/docarray_v2/build-proto.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Do NOT use this directly, use jinaai/protogen image
5+
# use jinaai/protogen:v21 in order to use compiler version == 21 (creates pb/docarray_pb2.py)
6+
# and use jinaai/protogen:latest to use compiler version <= 20 (creates pb2/docarray_pb2.py)
7+
# make sure to use jinaai/protogen:v21 to avoid overwriting the module
8+
#
9+
# current dir: jina root (the one with README.md)
10+
# run the following in bash:
11+
# docker run -v $(pwd)/jina/proto/docarray_v2:/jina/proto jinaai/protogen
12+
# finally, set back owner of the generated files using: sudo chown -R $(id -u ${USER}):$(id -g ${USER}) ./jina/proto
13+
14+
# The protogen docker image can also be build locally using:
15+
# docker build -f Dockerfiles/protogen.Dockerfile -t jinaai/protogen:local .
16+
# or
17+
# docker build -f Dockerfiles/protogen-3.21.Dockerfile -t jinaai/protogen-3.21:local .
18+
19+
SRC_DIR=./
20+
MODULE=jina
21+
SRC_NAME="${MODULE}.proto"
22+
23+
COMP_PROTO_OUT_NAME="${MODULE}_pb2.py"
24+
COMP_GRPC_OUT_NAME="${MODULE}_pb2_grpc.py"
25+
PB_NAME="${2:-pb}"
26+
OUT_FOLDER="${PB_NAME}/"
27+
28+
VER_FILE=../__init__.py
29+
30+
if [ "$#" -ne 1 ] && [ "$#" -ne 2 ]; then
31+
echo "Error: Please specify the [PATH_TO_GRPC_PYTHON_PLUGIN], refer more details at " \
32+
"https://docs.jina.ai/"
33+
printf "\n"
34+
echo "USAGE:"
35+
printf "\t"
36+
echo "bash ./build-proto.sh [PATH_TO_GRPC_PYTHON_PLUGIN]"
37+
exit 1
38+
fi
39+
40+
PLUGIN_PATH=${1} # /Volumes/TOSHIBA-4T/Documents/grpc/bins/opt/grpc_python_plugin
41+
42+
printf "\e[1;33mgenerating protobuf and grpc python interface\e[0m\n"
43+
44+
protoc -I ${SRC_DIR} --python_out="${SRC_DIR}${OUT_FOLDER}" --grpc_python_out="${SRC_DIR}${OUT_FOLDER}" --plugin=protoc-gen-grpc_python=${PLUGIN_PATH} ${SRC_DIR}${SRC_NAME}
45+
46+
printf "\e[1;33mfixing grpc import\e[0m\n"
47+
printf "using linux sed syntax, if you are running this on mac, you may want to comment out the sed for linux"
48+
# fix import bug in google generator
49+
# for mac
50+
# sed -i '' -e 's/import\ jina_pb2\ as\ jina__pb2/from\ \.\ import\ jina_pb2\ as\ jina__pb2/' ${SRC_DIR}jina_pb2_grpc.py
51+
# for linux
52+
sed -i "s/import\ docarray_pb2/import\ docarray.proto.${PB_NAME}.docarray_pb2/" "${SRC_DIR}${OUT_FOLDER}jina_pb2.py"
53+
sed -i 's/import\ jina_pb2\ as\ jina__pb2/from\ \.\.\.\ import\ serializer\ as\ jina__pb2/' "${SRC_DIR}${OUT_FOLDER}jina_pb2_grpc.py"
54+
55+
OLDVER=$(sed -n 's/^__proto_version__ = '\''\(.*\)'\''$/\1/p' $VER_FILE)
56+
printf "current proto version:\t\e[1;33m$OLDVER\e[0m\n"
57+
58+
NEWVER=$(echo $OLDVER | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{$NF=sprintf("%0*d", length($NF), ($NF+1)); print}')
59+
printf "bump proto version to:\t\e[1;32m$NEWVER\e[0m\n"
60+
61+
# for mac
62+
# sed -i '' -e 's/^__proto_version__ = '\''\(.*\)'\''/__proto_version__ = '\'"$NEWVER"\''/' $VER_FILE
63+
# for linux
64+
sed -i 's/^__proto_version__ = '\''\(.*\)'\''/__proto_version__ = '\'"$NEWVER"\''/' $VER_FILE
65+
66+
67+
68+
printf "\e[1;32mAll done!\e[0m\n"
69+
printf "if you are running this inside Docker container, you may manually bump proto version to:\t\e[1;32m$NEWVER\e[0m\n"

jina/proto/docarray_v2/docarray.proto

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
3+
package docarray;
4+
5+
/*
6+
this file is just a placeholder for the DA coming from jina._docarray dependency
7+
*/
8+
message DocListProto {
9+
10+
}

0 commit comments

Comments
 (0)