blob: 6b59f1e981c03621b2855206c3e40fc0cd9c85fe [file] [log] [blame]
Zsolt Harasztibae12752016-10-10 09:55:30 -07001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import base64
17import json
18import os
19import sys
20from commands import getstatusoutput
21from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest
22
23from chameleon.protoc_plugins.swagger_template import \
24 ProtobufCompilationFailedError
25from chameleon.protos import third_party
26
27this_dir = os.path.abspath(os.path.dirname(__file__))
28third_party_dir = os.path.dirname(third_party.__file__)
29
30
31def unindent(str):
32 """eat leading space in front of lines based on the smallest one"""
33 min_leading_spaces = len(str)
34 lines = str.splitlines()
35 for line in lines:
36 if line:
37 min_leading_spaces = min(len(line) - len(line.lstrip(' ')),
38 min_leading_spaces)
39 return '\n'.join(l[min_leading_spaces:] for l in lines)
40
41
42def mkdir(path):
43 """equivalent of command line mkdir -p <path>"""
44 if os.path.exists(path):
45 assert os.path.isdir(path)
46 return
47 head, tail = os.path.split(os.path.abspath(path))
48 if not os.path.exists(head):
49 mkdir(path)
50 assert os.path.isdir(head)
51 os.mkdir(path)
52
53
54def save_file(path, content, mode=0644):
55 """save content into file of path"""
56 with file(path, 'w') as f:
57 f.write(content)
58 os.chmod(path, mode)
59
60
61def load_file(path, read_mode='r'):
62 """load content from file of path"""
63 with file(path, read_mode) as f:
64 content = f.read()
65 return content
66
67
68def generate_plugin_request(proto):
69 """save proto file and run protoc to generate a plugin request protobuf"""
70
71 workdir = '/tmp/chameleon_tests'
72
73 mkdir(workdir)
74 save_file(os.path.join(workdir, 'test.proto'), proto)
75 cmd = (
76 'cd {this_dir} && '
77 'env PATH={extended_path} '
78 'python -m grpc.tools.protoc '
79 '-I{workdir} '
80 '-I{third_party_dir} '
81 '-I{this_dir} '
82 '--plugin=protoc-gen-null=null_plugin.py '
83 '--null_out={workdir} '
84 '{workdir}/test.proto'
85 .format(
86 extended_path=os.path.dirname(sys.executable),
87 python=sys.executable,
88 this_dir=this_dir,
89 workdir=workdir,
90 third_party_dir=third_party_dir
91 ))
92
93 code, output = getstatusoutput(cmd)
94 if code != 0:
95 raise ProtobufCompilationFailedError(output)
96
97 content = base64.decodestring(
98 load_file(os.path.join(workdir, 'protoc.request'), 'rb'))
99 request = CodeGeneratorRequest()
100 request.ParseFromString(content)
101
102 return request
103
104
105def json_rt(data):
106 """
107 JSON round-trip is to simply get rid of OrderedDict, to allow cleaner
108 testing.
109 """
110 return json.loads(json.dumps(data))