kinow / protobuf-uml-diagram

Create UML diagrams from Protobuf compiled .proto files using Python
https://github.com/kinow/protobuf-uml-diagram
Apache License 2.0
69 stars 12 forks source link

docker/gen_uml.sh fails if directory name starts with "proto" #47

Open feedhandler opened 2 years ago

feedhandler commented 2 years ago

Hi, If proto location is directory starting with "proto" then the gen_uml.sh fails to rename the file Example

|-- foo.proto
`-- lib
    `-- protobuf
        `-- bar.proto

and foo.proto imports lib/protobuf/bar.proto Output from dockerrun:

./dockerrun.sh /tmp/tmp.Z9AnkLGBd9 /tmp/tmp.NSd9b8t2DP/
MODULE:
INFO:__main__:Imported: foo_pb2
INFO:__main__:Writing diagram to /out/foo_pb2.png
ERROR:__main__:Failed to import lib_pb2buf/bar.proto
Traceback (most recent call last):
  File "//protobuf_uml_diagram.py", line 240, in <module>
    main()
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "//protobuf_uml_diagram.py", line 234, in main
    .from_file(proto) \
  File "//protobuf_uml_diagram.py", line 187, in from_file
    raise e
  File "//protobuf_uml_diagram.py", line 184, in from_file
    self._proto_module = _module(proto_file)
  File "//protobuf_uml_diagram.py", line 168, in _module
    return import_module(no_extension.replace("/", "."))
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'lib_pb2buf'

The problem is when gen_uml.sh tries to replace .proto extension with _pb2, the regex is not working properly, it changes the "proto" in the directory name, instead of the file extension

I fixed it by changing from

    p="${p/.proto/_pb2}"

to

    p="${p%.proto}_pb2"

Thanks

kinow commented 2 years ago

Hi @feedhandler ! Thanks for reporting it. Would you be able to send a pull request with the fix? Not sure if it'd be easy/doable to have a test for that since it requires the directory + running docker. Thanks!