forked from hz-b/bact-archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_buffer.py
79 lines (64 loc) · 2.62 KB
/
protocol_buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
# Author: Andreas Schälicke <[email protected]>
# Pierre Schnizer <[email protected]>
# Date: 2017, 2020
"""Generate the interface code for Google Protobuf
For Google protocol buffer see
https://developers.google.com/protocol-buffers
"""
import os.path
import setuptools
import logging
logger = logging.getLogger('setup')
_log = setuptools.distutils.log
class GenerateProtocolBuffer(setuptools.Command):
"""Custom build step for protobuf
Generate the python and c++ wrapper code from the protocol buffer specifications
"""
description = "Run protoc to generate python and cpp wrapper files"
user_options = [
# The format is (long option, short option, description).
('inplace', None, 'create files inplace of proto-file (default)'),
('python', None, 'create python wrapper'),
('cpp', None, 'create C++ wrapper'),
('source', None, 'files to be processed by protoc'),
('src-dir', None, 'directory, where the input files are located'),
('pydir', None, 'directroy, where the python output will be placed'),
('build-dir', None, 'directroy, where the output will be placed'),
('protoc=', None, 'protoc executable to use'),
]
def initialize_options(self):
"""Set default values for options."""
# Each user option must be listed here with their default value.
self.inplace = True
self.python = False
self.cpp = False
self.protoc = None
cwd = os.getcwd()
self.source = ''
self.pydir = cwd
self.src_dir = cwd
self.build_dir = cwd
def finalize_options(self):
"""Post-process options."""
if not self.inplace:
self.announce('inplace False is not support yet',
level=_log.WARN)
if not self.python and not self.cpp:
self.python = True
self.cpp = True
self.announce('select python and C++ wrapper',
level=_log.INFO)
def run(self):
self.announce("creating Wrapper for Archiver Protocol Buffers",
level=_log.INFO)
if self.protoc is None:
protoc = 'protoc'
else:
protoc = self.protoc
#self.announce('using protoc "%s"' %(protoc,), level=_log.INFO)
args = [protoc, self.source, '--proto_path={}'.format(self.src_dir)]
if self.cpp:
self.spawn(args + ['--cpp_out={}'.format(self.build_dir)])
if self.python:
self.spawn(args + ['--python_out={}'.format(self.pydir)])