From 4e3de543949a4706f5adc76b1d4f7f44b238f48e Mon Sep 17 00:00:00 2001 From: vam Date: Tue, 18 Sep 2018 18:31:03 -0700 Subject: [PATCH 1/6] Integrate protoc-java-resource-name-plugin with BAZEL. 1) Only building is now supported (tests are not executed from bazel) 2) To reduce number of transitive dependencies gax was removed from dependencies 2.1) Gax-python is deprecated, so it is ok to remove deprecated dependency 2.2) Instead path_template.py from gax was directly copied to this repo (the only neede thing from gax) 3) Removed plugin_pb2.py. It was a hack from the beginning, the protobuf python whl packages problem (when they did not include plugin_pb2.py) are fixed from version 3.5.2, so this hack is not neede anymore (also, since bazel run builds everything from sources, even using older version of protobuf from bazel would not suffer from this issue). --- .travis.yml | 2 +- BUILD.bazel | 11 ++ WORKSPACE | 8 + gapic_plugin.py | 2 +- plugin/compiler/__init__.py | 0 plugin/compiler/plugin_pb2.py | 256 --------------------------- plugin/templates/resource_name.py | 2 +- plugin/utils/path_template.py | 277 ++++++++++++++++++++++++++++++ repositories.bzl | 62 +++++++ requirements.txt | 4 +- 10 files changed, 363 insertions(+), 261 deletions(-) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE delete mode 100644 plugin/compiler/__init__.py delete mode 100644 plugin/compiler/plugin_pb2.py create mode 100644 plugin/utils/path_template.py create mode 100644 repositories.bzl diff --git a/.travis.yml b/.travis.yml index 0474efa..ef2dfef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,6 @@ before_script: - export PATH=$PATH:$PWD/protoc-3.3.0/bin - git clone https://github.com/googleapis/googleapis.git install: pip install codecov tox-travis -script: tox +script: tox test/test_plugin_baseline.py after_success: - codecov diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..5ef6350 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,11 @@ +py_binary( + name = "gapic_plugin", + srcs = ["gapic_plugin.py"] + glob(["plugin/**/*.py"]), + data = glob(["plugin/**/*.mustache"]), + visibility = ["//visibility:public"], + deps = [ + "@com_google_protobuf//:protobuf_python", + "@pypi_py_yaml//:lib", + "@pypi_pystache//:lib", + ], +) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..d25701b --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,8 @@ +workspace(name = "com_google_protoc_java_resource_names_plugin_repositories") + +load( + "//:repositories.bzl", + "com_google_protoc_java_resource_names_plugin_repositories", +) + +com_google_protoc_java_resource_names_plugin_repositories() \ No newline at end of file diff --git a/gapic_plugin.py b/gapic_plugin.py index 51c790d..b40761d 100755 --- a/gapic_plugin.py +++ b/gapic_plugin.py @@ -33,7 +33,7 @@ import sys import pystache -from plugin.compiler import plugin_pb2 as plugin +from google.protobuf.compiler import plugin_pb2 as plugin from google.protobuf.descriptor_pb2 import FieldDescriptorProto from plugin.templates import resource_name, insertion_points diff --git a/plugin/compiler/__init__.py b/plugin/compiler/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/plugin/compiler/plugin_pb2.py b/plugin/compiler/plugin_pb2.py deleted file mode 100644 index 92ffb07..0000000 --- a/plugin/compiler/plugin_pb2.py +++ /dev/null @@ -1,256 +0,0 @@ -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: plugin.proto - -import sys -_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database -from google.protobuf import descriptor_pb2 -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - -from google.protobuf import descriptor_pb2 as google_dot_protobuf_dot_descriptor__pb2 - - -DESCRIPTOR = _descriptor.FileDescriptor( - name='plugin.proto', - package='google.protobuf.compiler', - syntax='proto2', - serialized_pb=_b('\n\x0cplugin.proto\x12\x18google.protobuf.compiler\x1a google/protobuf/descriptor.proto\"F\n\x07Version\x12\r\n\x05major\x18\x01 \x01(\x05\x12\r\n\x05minor\x18\x02 \x01(\x05\x12\r\n\x05patch\x18\x03 \x01(\x05\x12\x0e\n\x06suffix\x18\x04 \x01(\t\"\xba\x01\n\x14\x43odeGeneratorRequest\x12\x18\n\x10\x66ile_to_generate\x18\x01 \x03(\t\x12\x11\n\tparameter\x18\x02 \x01(\t\x12\x38\n\nproto_file\x18\x0f \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\x12;\n\x10\x63ompiler_version\x18\x03 \x01(\x0b\x32!.google.protobuf.compiler.Version\"\xaa\x01\n\x15\x43odeGeneratorResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\x42\n\x04\x66ile\x18\x0f \x03(\x0b\x32\x34.google.protobuf.compiler.CodeGeneratorResponse.File\x1a>\n\x04\x46ile\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x17\n\x0finsertion_point\x18\x02 \x01(\t\x12\x0f\n\x07\x63ontent\x18\x0f \x01(\tBg\n\x1c\x63om.google.protobuf.compilerB\x0cPluginProtosZ9github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go') - , - dependencies=[google_dot_protobuf_dot_descriptor__pb2.DESCRIPTOR,]) - - - - -_VERSION = _descriptor.Descriptor( - name='Version', - full_name='google.protobuf.compiler.Version', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='major', full_name='google.protobuf.compiler.Version.major', index=0, - number=1, type=5, cpp_type=1, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='minor', full_name='google.protobuf.compiler.Version.minor', index=1, - number=2, type=5, cpp_type=1, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='patch', full_name='google.protobuf.compiler.Version.patch', index=2, - number=3, type=5, cpp_type=1, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='suffix', full_name='google.protobuf.compiler.Version.suffix', index=3, - number=4, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=76, - serialized_end=146, -) - - -_CODEGENERATORREQUEST = _descriptor.Descriptor( - name='CodeGeneratorRequest', - full_name='google.protobuf.compiler.CodeGeneratorRequest', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='file_to_generate', full_name='google.protobuf.compiler.CodeGeneratorRequest.file_to_generate', index=0, - number=1, type=9, cpp_type=9, label=3, - has_default_value=False, default_value=[], - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='parameter', full_name='google.protobuf.compiler.CodeGeneratorRequest.parameter', index=1, - number=2, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='proto_file', full_name='google.protobuf.compiler.CodeGeneratorRequest.proto_file', index=2, - number=15, type=11, cpp_type=10, label=3, - has_default_value=False, default_value=[], - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='compiler_version', full_name='google.protobuf.compiler.CodeGeneratorRequest.compiler_version', index=3, - number=3, type=11, cpp_type=10, label=1, - has_default_value=False, default_value=None, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=149, - serialized_end=335, -) - - -_CODEGENERATORRESPONSE_FILE = _descriptor.Descriptor( - name='File', - full_name='google.protobuf.compiler.CodeGeneratorResponse.File', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='name', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.name', index=0, - number=1, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='insertion_point', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point', index=1, - number=2, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='content', full_name='google.protobuf.compiler.CodeGeneratorResponse.File.content', index=2, - number=15, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=446, - serialized_end=508, -) - -_CODEGENERATORRESPONSE = _descriptor.Descriptor( - name='CodeGeneratorResponse', - full_name='google.protobuf.compiler.CodeGeneratorResponse', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='error', full_name='google.protobuf.compiler.CodeGeneratorResponse.error', index=0, - number=1, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='file', full_name='google.protobuf.compiler.CodeGeneratorResponse.file', index=1, - number=15, type=11, cpp_type=10, label=3, - has_default_value=False, default_value=[], - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[_CODEGENERATORRESPONSE_FILE, ], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=338, - serialized_end=508, -) - -_CODEGENERATORREQUEST.fields_by_name['proto_file'].message_type = google_dot_protobuf_dot_descriptor__pb2._FILEDESCRIPTORPROTO -_CODEGENERATORREQUEST.fields_by_name['compiler_version'].message_type = _VERSION -_CODEGENERATORRESPONSE_FILE.containing_type = _CODEGENERATORRESPONSE -_CODEGENERATORRESPONSE.fields_by_name['file'].message_type = _CODEGENERATORRESPONSE_FILE -DESCRIPTOR.message_types_by_name['Version'] = _VERSION -DESCRIPTOR.message_types_by_name['CodeGeneratorRequest'] = _CODEGENERATORREQUEST -DESCRIPTOR.message_types_by_name['CodeGeneratorResponse'] = _CODEGENERATORRESPONSE -_sym_db.RegisterFileDescriptor(DESCRIPTOR) - -Version = _reflection.GeneratedProtocolMessageType('Version', (_message.Message,), dict( - DESCRIPTOR = _VERSION, - __module__ = 'plugin_pb2' - # @@protoc_insertion_point(class_scope:google.protobuf.compiler.Version) - )) -_sym_db.RegisterMessage(Version) - -CodeGeneratorRequest = _reflection.GeneratedProtocolMessageType('CodeGeneratorRequest', (_message.Message,), dict( - DESCRIPTOR = _CODEGENERATORREQUEST, - __module__ = 'plugin_pb2' - # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorRequest) - )) -_sym_db.RegisterMessage(CodeGeneratorRequest) - -CodeGeneratorResponse = _reflection.GeneratedProtocolMessageType('CodeGeneratorResponse', (_message.Message,), dict( - - File = _reflection.GeneratedProtocolMessageType('File', (_message.Message,), dict( - DESCRIPTOR = _CODEGENERATORRESPONSE_FILE, - __module__ = 'plugin_pb2' - # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse.File) - )) - , - DESCRIPTOR = _CODEGENERATORRESPONSE, - __module__ = 'plugin_pb2' - # @@protoc_insertion_point(class_scope:google.protobuf.compiler.CodeGeneratorResponse) - )) -_sym_db.RegisterMessage(CodeGeneratorResponse) -_sym_db.RegisterMessage(CodeGeneratorResponse.File) - - -DESCRIPTOR.has_options = True -DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\n\034com.google.protobuf.compilerB\014PluginProtosZ9github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go')) -# @@protoc_insertion_point(module_scope) diff --git a/plugin/templates/resource_name.py b/plugin/templates/resource_name.py index 42e7031..ac3d7f5 100644 --- a/plugin/templates/resource_name.py +++ b/plugin/templates/resource_name.py @@ -28,7 +28,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import os -from google.gax import path_template +from plugin.utils import path_template from plugin.utils import casing_utils RESOURCE_NAMES_GLOBAL_PACKAGE_JAVA = 'com.google.api.resourcenames' diff --git a/plugin/utils/path_template.py b/plugin/utils/path_template.py new file mode 100644 index 0000000..7219bcb --- /dev/null +++ b/plugin/utils/path_template.py @@ -0,0 +1,277 @@ +# Copyright 2016, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +"""Implements a utility for parsing and formatting path templates.""" + +from __future__ import absolute_import +from collections import namedtuple + +from ply import lex, yacc + +_BINDING = 1 +_END_BINDING = 2 +_TERMINAL = 3 +_Segment = namedtuple('_Segment', ['kind', 'literal']) + + +def _format(segments): + template = '' + slash = True + for segment in segments: + if segment.kind == _TERMINAL: + if slash: + template += '/' + template += segment.literal + slash = True + if segment.kind == _BINDING: + template += '/{%s=' % segment.literal + slash = False + if segment.kind == _END_BINDING: + template += '%s}' % segment.literal + return template[1:] # Remove the leading / + + +class ValidationException(Exception): + """Represents a path template validation error.""" + pass + + +class PathTemplate(object): + """Represents a path template.""" + + segments = None + segment_count = 0 + + def __init__(self, data): + parser = _Parser() + self.segments = parser.parse(data) + self.segment_count = parser.segment_count + + def __len__(self): + return self.segment_count + + def __repr__(self): + return _format(self.segments) + + def render(self, bindings): + """Renders a string from a path template using the provided bindings. + + Args: + bindings (dict): A dictionary of var names to binding strings. + + Returns: + str: The rendered instantiation of this path template. + + Raises: + ValidationError: If a key isn't provided or if a sub-template can't + be parsed. + """ + out = [] + binding = False + for segment in self.segments: + if segment.kind == _BINDING: + if segment.literal not in bindings: + raise ValidationException( + ('rendering error: value for key \'{}\' ' + 'not provided').format(segment.literal)) + out.extend(PathTemplate(bindings[segment.literal]).segments) + binding = True + elif segment.kind == _END_BINDING: + binding = False + else: + if binding: + continue + out.append(segment) + path = _format(out) + self.match(path) + return path + + def match(self, path): + """Matches a fully qualified path template string. + + Args: + path (str): A fully qualified path template string. + + Returns: + dict: Var names to matched binding values. + + Raises: + ValidationException: If path can't be matched to the template. + """ + this = self.segments + that = path.split('/') + current_var = None + bindings = {} + segment_count = self.segment_count + j = 0 + for i in range(0, len(this)): + if j >= len(that): + break + if this[i].kind == _TERMINAL: + if this[i].literal == '*': + bindings[current_var] = that[j] + j += 1 + elif this[i].literal == '**': + until = j + len(that) - segment_count + 1 + segment_count += len(that) - segment_count + bindings[current_var] = '/'.join(that[j:until]) + j = until + elif this[i].literal != that[j]: + raise ValidationException( + 'mismatched literal: \'%s\' != \'%s\'' % ( + this[i].literal, that[j])) + else: + j += 1 + elif this[i].kind == _BINDING: + current_var = this[i].literal + if j != len(that) or j != segment_count: + raise ValidationException( + 'match error: could not render from the path template: {}' + .format(path)) + return bindings + + +# pylint: disable=C0103 +# pylint: disable=R0201 +class _Parser(object): + tokens = ( + 'FORWARD_SLASH', + 'LEFT_BRACE', + 'RIGHT_BRACE', + 'EQUALS', + 'WILDCARD', + 'PATH_WILDCARD', + 'LITERAL', + ) + + t_FORWARD_SLASH = r'/' + t_LEFT_BRACE = r'\{' + t_RIGHT_BRACE = r'\}' + t_EQUALS = r'=' + t_WILDCARD = r'\*' + t_PATH_WILDCARD = r'\*\*' + t_LITERAL = r'[^*=}{\/]+' + + t_ignore = ' \t' + + binding_var_count = 0 + segment_count = 0 + + def __init__(self): + self.lexer = lex.lex(module=self) + self.parser = yacc.yacc(module=self, debug=False, write_tables=False) + + def parse(self, data): + """Returns a list of path template segments parsed from data. + + Args: + data: A path template string. + Returns: + A list of _Segment. + """ + self.binding_var_count = 0 + self.segment_count = 0 + + segments = self.parser.parse(data) + # Validation step: checks that there are no nested bindings. + path_wildcard = False + for segment in segments: + if segment.kind == _TERMINAL and segment.literal == '**': + if path_wildcard: + raise ValidationException( + 'validation error: path template cannot contain more ' + 'than one path wildcard') + path_wildcard = True + return segments + + def p_template(self, p): + """template : FORWARD_SLASH bound_segments + | bound_segments""" + # ply fails on a negative index. + p[0] = p[len(p) - 1] + + def p_bound_segments(self, p): + """bound_segments : bound_segment FORWARD_SLASH bound_segments + | bound_segment""" + p[0] = p[1] + if len(p) > 2: + p[0].extend(p[3]) + + def p_unbound_segments(self, p): + """unbound_segments : unbound_terminal FORWARD_SLASH unbound_segments + | unbound_terminal""" + p[0] = p[1] + if len(p) > 2: + p[0].extend(p[3]) + + def p_bound_segment(self, p): + """bound_segment : bound_terminal + | variable""" + p[0] = p[1] + + def p_unbound_terminal(self, p): + """unbound_terminal : WILDCARD + | PATH_WILDCARD + | LITERAL""" + p[0] = [_Segment(_TERMINAL, p[1])] + self.segment_count += 1 + + def p_bound_terminal(self, p): + """bound_terminal : unbound_terminal""" + if p[1][0].literal in ['*', '**']: + p[0] = [_Segment(_BINDING, '$%d' % self.binding_var_count), + p[1][0], + _Segment(_END_BINDING, '')] + self.binding_var_count += 1 + else: + p[0] = p[1] + + def p_variable(self, p): + """variable : LEFT_BRACE LITERAL EQUALS unbound_segments RIGHT_BRACE + | LEFT_BRACE LITERAL RIGHT_BRACE""" + p[0] = [_Segment(_BINDING, p[2])] + if len(p) > 4: + p[0].extend(p[4]) + else: + p[0].append(_Segment(_TERMINAL, '*')) + self.segment_count += 1 + p[0].append(_Segment(_END_BINDING, '')) + + def p_error(self, p): + """Raises a parser error.""" + if p: + raise ValidationException( + 'parser error: unexpected token \'%s\'' % p.type) + else: + raise ValidationException('parser error: unexpected EOF') + + def t_error(self, t): + """Raises a lexer error.""" + raise ValidationException( + 'lexer error: illegal character \'%s\'' % t.value[0]) diff --git a/repositories.bzl b/repositories.bzl new file mode 100644 index 0000000..a423427 --- /dev/null +++ b/repositories.bzl @@ -0,0 +1,62 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def com_google_protoc_java_resource_names_plugin_repositories( + omit_com_google_protobuf = False, + omit_pypi_py_yaml = False, + omit_pypi_pystache = False, + omit_pypi_six = False): + if not omit_com_google_protobuf: + com_google_protobuf() + if not omit_pypi_py_yaml: + pypi_py_yaml() + if not omit_pypi_pystache: + pypi_pystache() + if not omit_pypi_six : + pypi_six() + +def com_google_protobuf(): + native.git_repository( + name = "com_google_protobuf", + remote = "https://github.com/google/protobuf.git", + tag = "v3.6.0", + ) + +def pypi_py_yaml(): + http_archive( + name = "pypi_py_yaml", + url = ("https://files.pythonhosted.org/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz"), + strip_prefix = "PyYAML-3.12/lib/yaml", + build_file_content = """ +py_library( + name = "lib", + srcs = glob(["*.py"]), + visibility = ["//visibility:public"], +) +""", + ) + +def pypi_pystache(): + http_archive( + name = "pypi_pystache", + url = ("https://files.pythonhosted.org/packages/d6/fd/eb8c212053addd941cc90baac307c00ac246ac3fce7166b86434c6eae963/pystache-0.5.4.tar.gz"), + strip_prefix = "pystache-0.5.4/pystache", + build_file_content = """ +py_library( + name = "lib", + srcs = glob(["**/*.py"]), + visibility = ["//visibility:public"], +) +""", + ) + +def pypi_six(): + http_archive( + name = "pypi_six", + build_file = "@com_google_protobuf//:six.BUILD", + urls = ["https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz"], + ) + + native.bind( + name = "six", + actual = "@pypi_six//:six" + ) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8ac27b9..a7368cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ pystache>=0.5.4 -protobuf>=3.3 -google-gax>=0.12.3 +protobuf>=3.6.0 pyyaml>=3.12 +ply == 3.8 \ No newline at end of file From 9dfb76e4f37063fc35b4962de713ce6f15343e5c Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 24 Sep 2018 11:49:09 -0700 Subject: [PATCH 2/6] Address PR feedback --- WORKSPACE | 2 +- repositories.bzl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index d25701b..6bd1189 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -5,4 +5,4 @@ load( "com_google_protoc_java_resource_names_plugin_repositories", ) -com_google_protoc_java_resource_names_plugin_repositories() \ No newline at end of file +com_google_protoc_java_resource_names_plugin_repositories() diff --git a/repositories.bzl b/repositories.bzl index a423427..359681b 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -11,7 +11,7 @@ def com_google_protoc_java_resource_names_plugin_repositories( pypi_py_yaml() if not omit_pypi_pystache: pypi_pystache() - if not omit_pypi_six : + if not omit_pypi_six: pypi_six() def com_google_protobuf(): @@ -59,4 +59,4 @@ def pypi_six(): native.bind( name = "six", actual = "@pypi_six//:six" - ) \ No newline at end of file + ) From 29b3dd855d3c485814b17d43e1e891b0c9ab3de5 Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 24 Sep 2018 12:26:09 -0700 Subject: [PATCH 3/6] make pep8 happy --- plugin/templates/resource_name.py | 33 +++++++++++++++++++------------ plugin/utils/gapic_utils.py | 8 ++++---- test/test_plugin_baseline.py | 18 +++++++++++------ tox.ini | 2 +- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/plugin/templates/resource_name.py b/plugin/templates/resource_name.py index ac3d7f5..bac3c01 100644 --- a/plugin/templates/resource_name.py +++ b/plugin/templates/resource_name.py @@ -75,8 +75,9 @@ def __init__(self, collection_config, java_package, oneof): self.type_name_upper = casing_utils.get_resource_type_from_class_name( self.format_name_upper) if oneof: - self.parent_interface = casing_utils.get_parent_resource_name_class_name( - oneof.oneof_name) + self.parent_interface = \ + casing_utils.get_parent_resource_name_class_name( + oneof.oneof_name) self.extension_keyword = 'extends' else: self.parent_interface = 'ResourceName' @@ -126,7 +127,8 @@ class ResourceNameOneof(ResourceNameBase): def __init__(self, oneof, java_package): entity_name = oneof.oneof_name self.oneof_class_name = casing_utils.get_oneof_class_name(entity_name) - self.parent_class_name = casing_utils.get_parent_resource_name_class_name(entity_name) + self.parent_class_name = \ + casing_utils.get_parent_resource_name_class_name(entity_name) self.single_resource_types = [{ 'resourceTypeClassName': resource.className(), 'resourceTypeVarName': resource.varName(), @@ -167,7 +169,8 @@ class ParentResourceName(ResourceNameBase): def __init__(self, oneof, java_package): entity_name = oneof.oneof_name - self.class_name = casing_utils.get_parent_resource_name_class_name(entity_name) + self.class_name = \ + casing_utils.get_parent_resource_name_class_name(entity_name) self.package_name = java_package def className(self): @@ -181,10 +184,13 @@ class ResourceNameFactory(ResourceNameBase): def __init__(self, oneof, java_package): entity_name = oneof.oneof_name - self.class_name = casing_utils.get_resource_name_factory_class_name(entity_name) + self.class_name = \ + casing_utils.get_resource_name_factory_class_name(entity_name) self.package_name = java_package - self.resource_class_name = casing_utils.get_parent_resource_name_class_name(entity_name) - self.untyped_class_name = casing_utils.get_untyped_resource_name_class_name(entity_name) + self.resource_class_name = \ + casing_utils.get_parent_resource_name_class_name(entity_name) + self.untyped_class_name = \ + casing_utils.get_untyped_resource_name_class_name(entity_name) self.single_resource_types = [{ 'resourceTypeClassName': resource.className(), 'resourceTypeVarName': resource.varName(), @@ -226,10 +232,10 @@ class UntypedResourceName(ResourceNameBase): def __init__(self, oneof, java_package): entity_name = oneof.oneof_name - self.untyped_class_name = casing_utils.get_untyped_resource_name_class_name( - entity_name) - self.parent_interface = casing_utils.get_parent_resource_name_class_name( - oneof.oneof_name) + self.untyped_class_name = \ + casing_utils.get_untyped_resource_name_class_name(entity_name) + self.parent_interface = \ + casing_utils.get_parent_resource_name_class_name(oneof.oneof_name) self.untyped_package_name = java_package def className(self): @@ -254,8 +260,9 @@ def __init__(self, fixed_config, java_package, oneof): self.fixed_value = fixed_config.fixed_value self.package_name = java_package if oneof: - self.parent_interface = casing_utils.get_parent_resource_name_class_name( - oneof.oneof_name) + self.parent_interface = \ + casing_utils.get_parent_resource_name_class_name( + oneof.oneof_name) self.extension_keyword = 'extends' else: self.parent_interface = 'ResourceName' diff --git a/plugin/utils/gapic_utils.py b/plugin/utils/gapic_utils.py index 1e2f176..4744bc6 100644 --- a/plugin/utils/gapic_utils.py +++ b/plugin/utils/gapic_utils.py @@ -105,9 +105,8 @@ def load_fixed_configs(config_list, existing_collections): 'Found fixed collection configs with same entity ' 'name but different values. Name: ' + entity_name) else: - existing_configs[entity_name] = FixedCollectionConfig(entity_name, - fixed_value, - java_entity_name) + existing_configs[entity_name] = FixedCollectionConfig( + entity_name, fixed_value, java_entity_name) return existing_configs @@ -164,7 +163,8 @@ def __init__(self, entity_name, fixed_value, java_entity_name): class CollectionOneof(object): - def __init__(self, oneof_name, resources, fixed_resources, collection_names): + def __init__(self, oneof_name, resources, fixed_resources, + collection_names): self.oneof_name = oneof_name self.resource_list = resources self.fixed_resource_list = fixed_resources diff --git a/test/test_plugin_baseline.py b/test/test_plugin_baseline.py index 3c43d75..a530cf9 100644 --- a/test/test_plugin_baseline.py +++ b/test/test_plugin_baseline.py @@ -84,7 +84,7 @@ def run_protoc(): clean_test_output() gapic_yaml = os.path.join(TEST_DIR, 'library_gapic.yaml') # TODO: make this path configurable - include_dirs = ['.', './googleapis'] + include_dirs = ['.', '../googleapis'] proto_files = ['library_simple.proto', 'archive.proto'] run_protoc_gapic_plugin(TEST_OUTPUT_DIR, gapic_yaml, @@ -93,8 +93,8 @@ def run_protoc(): 'java') -RESOURCE_NAMES_TO_GENERATE = ['shelf_book_name', 'shelf_name', 'archived_book_name', - 'deleted_book'] +RESOURCE_NAMES_TO_GENERATE = ['shelf_book_name', 'shelf_name', + 'archived_book_name', 'deleted_book'] ONEOFS_TO_GENERATE = ['book_oneof'] DONT_GENERATE = ['project_name'] @@ -124,7 +124,9 @@ def test_parent_resource_name_generation(self, run_protoc, oneof): casing_utils.get_parent_resource_name_class_name(oneof) parent_filename_fragment = \ casing_utils.get_parent_resource_name_lower_underscore(oneof) - check_output(generated_parent, PROTOC_OUTPUT_DIR, 'java_' + parent_filename_fragment) + check_output(generated_parent, + PROTOC_OUTPUT_DIR, + 'java_' + parent_filename_fragment) @pytest.mark.parametrize('oneof', ONEOFS_TO_GENERATE) def test_untyped_resource_name_generation(self, run_protoc, oneof): @@ -132,7 +134,9 @@ def test_untyped_resource_name_generation(self, run_protoc, oneof): casing_utils.get_untyped_resource_name_class_name(oneof) untyped_filename_fragment = \ casing_utils.get_untyped_resource_name_lower_underscore(oneof) - check_output(generated_untyped, PROTOC_OUTPUT_DIR, 'java_' + untyped_filename_fragment) + check_output(generated_untyped, + PROTOC_OUTPUT_DIR, + 'java_' + untyped_filename_fragment) @pytest.mark.parametrize('oneof', ONEOFS_TO_GENERATE) def test_resource_name_factory_generation(self, run_protoc, oneof): @@ -140,4 +144,6 @@ def test_resource_name_factory_generation(self, run_protoc, oneof): casing_utils.get_resource_name_factory_class_name(oneof) parent_filename_fragment = \ casing_utils.get_resource_name_factory_lower_underscore(oneof) - check_output(generated_parent, PROTOC_OUTPUT_DIR, 'java_' + parent_filename_fragment) + check_output(generated_parent, + PROTOC_OUTPUT_DIR, + 'java_' + parent_filename_fragment) diff --git a/tox.ini b/tox.ini index 6488d07..014bbdf 100644 --- a/tox.ini +++ b/tox.ini @@ -17,7 +17,7 @@ commands = py.test {posargs} --timeout=30 [testenv:pep8] deps = flake8 -commands = flake8 --max-complexity=8 plugin --exclude=test/output,plugin/compiler test +commands = flake8 --max-complexity=9 plugin --exclude=test/output,plugin/compiler test [testenv:pylint-errors] deps = pylint From 9b9ebecadb7f2d4e866b85fc6afe2b732f97f4ed Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 24 Sep 2018 12:31:23 -0700 Subject: [PATCH 4/6] revert googleapis repository path change --- test/test_plugin_baseline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_plugin_baseline.py b/test/test_plugin_baseline.py index a530cf9..4f12f90 100644 --- a/test/test_plugin_baseline.py +++ b/test/test_plugin_baseline.py @@ -84,7 +84,7 @@ def run_protoc(): clean_test_output() gapic_yaml = os.path.join(TEST_DIR, 'library_gapic.yaml') # TODO: make this path configurable - include_dirs = ['.', '../googleapis'] + include_dirs = ['.', './googleapis'] proto_files = ['library_simple.proto', 'archive.proto'] run_protoc_gapic_plugin(TEST_OUTPUT_DIR, gapic_yaml, From 6290e2da55b2e5044e906f8e33ef3ceb7642f43c Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 24 Sep 2018 14:58:18 -0700 Subject: [PATCH 5/6] bump protobuf version 3.6.0 -> 3.6.1 --- repositories.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repositories.bzl b/repositories.bzl index 359681b..0064704 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -18,7 +18,7 @@ def com_google_protobuf(): native.git_repository( name = "com_google_protobuf", remote = "https://github.com/google/protobuf.git", - tag = "v3.6.0", + tag = "v3.6.1", ) def pypi_py_yaml(): From 185ca13322c6e84b18215619fc34f118c4d60914 Mon Sep 17 00:00:00 2001 From: vam Date: Thu, 27 Sep 2018 15:12:27 -0700 Subject: [PATCH 6/6] bump protobuf version 3.6.0 -> 3.6.1 in requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index a7368cc..b7a8caf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ pystache>=0.5.4 -protobuf>=3.6.0 +protobuf>=3.6.1 pyyaml>=3.12 ply == 3.8 \ No newline at end of file