加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

protoc 的 -I 参数,及一个 python 脚本

(2012-09-18 11:54:17)
标签:

it

执行正面命令会在 nun/proto/proto 下生成协议文件:

protoc --python_out=nun/proto proto/*.proto

要把协议文件放到 num/proto 下,可以使用 I 参数:

protoc -Iproto --python_out=nun/proto proto/*.proto

下面是一个 Python 脚本,可以多做一点点事情(创建目标目录和 __init__.py):

# -*- coding: utf-8 -*-
from __future__ import print_function
from glob import glob

import os
import sys

def protoc(srcdir, dstdir):
    """
编译协议文件

    :param srcdir: .proto
源文件目录
    :param dstdir: python
输出文件目录
    """
    #
检查源目录
    if not os.path.exists(srcdir):
        print(repr(srcdir), 'does not exists.')
        sys.exit(1)

    #
准备目标目录
    if not os.path.exists(dstdir):
        os.makedirs(dstdir)

        initpy = os.path.join(dstdir, '__init__.py')
        if not os.path.exists(initpy):
            with open(initpy, 'w'):
                pass

    #
编译 .proto 文件
    srcfiles = glob(os.path.join(srcdir, '*.proto'))
    command = ' '.join([
        'protoc',
        '-I' + srcdir,
        '--python_out=' + dstdir,
        ' '.join(srcfiles)
        ])
    print(command)
    os.system(command)

if __name__ == '__main__':
    protoc('proto', os.path.join('nun', 'proto'))

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有