Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hardcoded _get_resource_file_name to fix proto generation #292

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion django_socio_grpc/management/commands/generateproto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
import sys
from pathlib import Path

from asgiref.sync import async_to_sync
Expand All @@ -12,6 +13,21 @@
from django_socio_grpc.protobuf.generators import RegistryToProtoGenerator
from django_socio_grpc.settings import grpc_settings

if sys.version_info >= (3, 9, 0):
from importlib import resources
else:
import pkg_resources


def _get_resource_file_name(package_or_requirement: str, resource_name: str) -> str:
"""
Obtain the filename for a resource on the file system.
"""
if sys.version_info >= (3, 9, 0):
return (resources.files(package_or_requirement) / resource_name).resolve()
else:
return pkg_resources.resource_filename(package_or_requirement, resource_name)


class Command(BaseCommand):
help = "Generates proto."
Expand Down Expand Up @@ -128,7 +144,7 @@ def handle(self, *args, **options):
raise ProtobufGenerationException(detail="No BASE_DIR in settings")
extra_args = options["extra_args"] if options["extra_args"] else ""
# INFO - AM - 16/04/2024 - subprocess.run is safe because we are not using shell=True. Please do not change this. Unit test check this
proto_include = protoc._get_resource_file_name(
proto_include = _get_resource_file_name(
"grpc_tools", "_proto"
) # See https://github.com/grpc/grpc/blob/master/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py#L209
protoc.main(
Expand Down
6 changes: 3 additions & 3 deletions django_socio_grpc/tests/test_proto_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from django.core.management import call_command
from django.test import TestCase, override_settings
from grpc_tools import protoc

from django_socio_grpc.exceptions import ProtobufGenerationException
from django_socio_grpc.management.commands.generateproto import _get_resource_file_name
from django_socio_grpc.protobuf import RegistrySingleton
from django_socio_grpc.protobuf.protoparser import protoparser
from django_socio_grpc.services import AppHandlerRegistry
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_generate_proto_subprocess_correctly_call(self, mock_protoc_main):
with patch_open():
call_command("generateproto", *args, **opts)

proto_include = protoc._get_resource_file_name("grpc_tools", "_proto")
proto_include = _get_resource_file_name("grpc_tools", "_proto")

mock_protoc_main.assert_called_once_with(
[
Expand All @@ -484,7 +484,7 @@ def test_generate_proto_with_extra_args(self, mock_protoc_main):
with patch_open():
call_command("generateproto", *args, **opts)

proto_include = protoc._get_resource_file_name("grpc_tools", "_proto")
proto_include = _get_resource_file_name("grpc_tools", "_proto")
mock_protoc_main.assert_called_once_with(
[
"",
Expand Down
Loading