diff --git a/django_socio_grpc/management/commands/generateproto.py b/django_socio_grpc/management/commands/generateproto.py index 4950acd1..30a0df74 100644 --- a/django_socio_grpc/management/commands/generateproto.py +++ b/django_socio_grpc/management/commands/generateproto.py @@ -1,5 +1,6 @@ import asyncio import os +import sys from pathlib import Path from asgiref.sync import async_to_sync @@ -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." @@ -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( diff --git a/django_socio_grpc/tests/test_proto_generation.py b/django_socio_grpc/tests/test_proto_generation.py index 48c6ac9f..d1264707 100644 --- a/django_socio_grpc/tests/test_proto_generation.py +++ b/django_socio_grpc/tests/test_proto_generation.py @@ -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 @@ -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( [ @@ -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( [ "",