Skip to content

Commit

Permalink
fix: ensure every path is decoded as utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
shawwn authored and cpcloud committed Dec 4, 2021
1 parent 4e2470a commit a936f08
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions protoletariat/fdsetgen.py
Expand Up @@ -5,6 +5,7 @@
import re
import subprocess
import tempfile
import os
from pathlib import Path
from typing import Callable, Iterable, Sequence

Expand All @@ -14,7 +15,6 @@

_PROTO_SUFFIX_PATTERN = re.compile(r"^(.+)\.proto$")


def _remove_proto_suffix(name: str) -> str:
"""Remove the `.proto` suffix from `name`."""
return _PROTO_SUFFIX_PATTERN.sub(r"\1", name)
Expand All @@ -41,10 +41,11 @@ def fix_imports(
exclude_imports_glob: Sequence[str],
) -> None:
"""Fix imports from protoc/buf generated code."""
python_out = Path(os.fsdecode(python_out))
fdset = FileDescriptorSet.FromString(self.generate_file_descriptor_set_bytes())

for fd in fdset.file:
name = fd.name
name = os.fsdecode(fd.name)
if _should_ignore(name, exclude_imports_glob):
continue

Expand Down Expand Up @@ -94,9 +95,9 @@ def __init__(
proto_files: Iterable[Path],
proto_paths: Iterable[Path],
) -> None:
self.protoc_path = protoc_path
self.proto_files = list(proto_files)
self.proto_paths = list(proto_paths)
self.protoc_path = os.fsdecode(protoc_path)
self.proto_files = [os.fsdecode(file) for file in proto_files]
self.proto_paths = [os.fsdecode(path) for path in proto_paths]

def generate_file_descriptor_set_bytes(self) -> bytes:
with tempfile.NamedTemporaryFile(delete=False) as f:
Expand All @@ -105,9 +106,9 @@ def generate_file_descriptor_set_bytes(self) -> bytes:
[
self.protoc_path,
"--include_imports",
f"--descriptor_set_out={filename}",
*map("--proto_path={}".format, self.proto_paths),
*map(str, self.proto_files),
"--descriptor_set_out=" + os.fsdecode(filename),
*["--proto_path=" + path for path in self.proto_paths],
*self.proto_files,
]
)

Expand Down

0 comments on commit a936f08

Please sign in to comment.