有什么方法可以在不使用子过程的情况下使用Python中的Protobuf编译器?

发布于 2025-02-07 18:24:25 字数 1278 浏览 1 评论 0原文

我正在编写一个脚本,该脚本应该越过我作为参数的路径,然后逐一编译所有.proto文件。 目前,我正在使用Popen来做到这一点,但是我们可以在不使用子过程的情况下使用Python内部的Protobuf编译器。 知道该怎么做吗?

谢谢!

这是我当前的代码:

def dir_path(string):
    if os.path.isdir(string):
        return string
    else:
        raise NotADirectoryError(string)


def compProtos(frompath, topath):
    for dirpath, dirs, files in os.walk(frompath):
        for filename in files:
            if filename.endswith('.proto'):
                print(
                    f'Currently generating file: {filename}, from path: {dirpath}')
                process = Popen(
                    f'python -m grpc_tools.protoc -I {dirpath} --python_out={topath} --grpc_python_out={topath} {dirpath}/{filename}')


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Run gRPC on protobuffs')
    parser.add_argument('-from', dest="frompath", type=dir_path,
                        required=True, help="Enter protobuffs path", metavar="FILE")
    parser.add_argument('-to', dest="topath", type=dir_path, default=".",
                        help="Enter saving path", metavar="FILE")
    args = parser.parse_args()
    compProtos(args.frompath, args.topath)
    print("Done generating protobuffs, results in folder: " + args.topath)

I'm writing a script that should run over a path I get as an argument, and compile all .proto files one by one.
Currently I'm using Popen to do that, but it makes sense that we could use the protobuf compiler from within python without using subprocesses.
any idea how to do so?

thanks!

this is my current code:

def dir_path(string):
    if os.path.isdir(string):
        return string
    else:
        raise NotADirectoryError(string)


def compProtos(frompath, topath):
    for dirpath, dirs, files in os.walk(frompath):
        for filename in files:
            if filename.endswith('.proto'):
                print(
                    f'Currently generating file: {filename}, from path: {dirpath}')
                process = Popen(
                    f'python -m grpc_tools.protoc -I {dirpath} --python_out={topath} --grpc_python_out={topath} {dirpath}/{filename}')


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Run gRPC on protobuffs')
    parser.add_argument('-from', dest="frompath", type=dir_path,
                        required=True, help="Enter protobuffs path", metavar="FILE")
    parser.add_argument('-to', dest="topath", type=dir_path, default=".",
                        help="Enter saving path", metavar="FILE")
    args = parser.parse_args()
    compProtos(args.frompath, args.topath)
    print("Done generating protobuffs, results in folder: " + args.topath)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2025-02-14 18:24:25

grpcio_tools 软件包具有protoc作为函数:

import grpc_tools.protoc
grpc_tools.protoc.main(['protoc', '-I.', '--python_out=.', 'foo.proto'])

The grpcio_tools package has protoc available as a function:

import grpc_tools.protoc
grpc_tools.protoc.main(['protoc', '-I.', '--python_out=.', 'foo.proto'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文