
import plugin_pb2

from jinja2 import Template
from pkg_resources import resource_string

import os.path
import sys
import tempfile


def main():
  log = sys.stderr

  request = plugin_pb2.CodeGeneratorRequest()
  request.ParseFromString(sys.stdin.read())

  path = tempfile.mkdtemp()

  generateFiles = set(request.file_to_generate)
  files = []
  for file in request.proto_file:
    if file.name not in generateFiles:
      continue

    name = file.name.split('.')[0]
    files.append(name)

    context = {
      'moduleName': name,
      'messages': file.message_type
    }

    cFilePath = os.path.join(path, name + '.c')
    with open(cFilePath, 'w') as f:
      t = Template(resource_string(__name__, 'template/module.jinja.c'))
      f.write(t.render(context))

  setupPyPath = os.path.join(path, 'setup.py')
  with open(setupPyPath, 'w') as f:
    t = Template(resource_string(__name__, 'template/setup.jinja.py'))
    f.write(t.render({'files': files}))

  print >> log, path


if __name__ == '__main__':
  main()


import plugin_pb2

from jinja2 import Template
from pkg_resources import resource_string

import os.path
import sys
import tempfile


def main():
  request = plugin_pb2.CodeGeneratorRequest()
  request.ParseFromString(sys.stdin.read())

  response = plugin_pb2.CodeGeneratorResponse()

  generateFiles = set(request.file_to_generate)
  files = []
  for file in request.proto_file:
    if file.name not in generateFiles:
      continue

    name = file.name.split('.')[0]
    files.append(name)

    context = {
      'moduleName': name,
      'messages': file.message_type
    }

    # Write the C file.
    t = Template(resource_string(__name__, 'template/module.jinja.c'))
    cFile = response.file.add()
    cFile.name = name + '.c'
    cFile.content = t.render(context)

  # Write setup.py.
  t = Template(resource_string(__name__, 'template/setup.jinja.py'))
  setupFile = response.file.add()
  setupFile.name = 'setup.py'
  setupFile.content = t.render({'files': files})

  sys.stdout.write(response.SerializeToString())


if __name__ == '__main__':
  main()

Use protoc for file output
Use protoc for file output

  
  import plugin_pb2
  
  from jinja2 import Template
  from pkg_resources import resource_string
  
  import os.path
  import sys
  import tempfile
  
  
  def main():
-   log = sys.stderr
- 
    request = plugin_pb2.CodeGeneratorRequest()
    request.ParseFromString(sys.stdin.read())
  
-   path = tempfile.mkdtemp()
+   response = plugin_pb2.CodeGeneratorResponse()
  
    generateFiles = set(request.file_to_generate)
    files = []
    for file in request.proto_file:
      if file.name not in generateFiles:
        continue
  
      name = file.name.split('.')[0]
      files.append(name)
  
      context = {
        'moduleName': name,
        'messages': file.message_type
      }
  
+     # Write the C file.
-     cFilePath = os.path.join(path, name + '.c')
-     with open(cFilePath, 'w') as f:
-       t = Template(resource_string(__name__, 'template/module.jinja.c'))
+     t = Template(resource_string(__name__, 'template/module.jinja.c'))
-       f.write(t.render(context))
+     cFile = response.file.add()
+     cFile.name = name + '.c'
+     cFile.content = t.render(context)
  
+   # Write setup.py.
-   setupPyPath = os.path.join(path, 'setup.py')
-   with open(setupPyPath, 'w') as f:
-     t = Template(resource_string(__name__, 'template/setup.jinja.py'))
+   t = Template(resource_string(__name__, 'template/setup.jinja.py'))
-     f.write(t.render({'files': files}))
+   setupFile = response.file.add()
+   setupFile.name = 'setup.py'
+   setupFile.content = t.render({'files': files})
  
-   print >> log, path
+   sys.stdout.write(response.SerializeToString())
  
  
  if __name__ == '__main__':
    main()
  
Use protoc for file output
## Code Before:

import plugin_pb2

from jinja2 import Template
from pkg_resources import resource_string

import os.path
import sys
import tempfile


def main():
  log = sys.stderr

  request = plugin_pb2.CodeGeneratorRequest()
  request.ParseFromString(sys.stdin.read())

  path = tempfile.mkdtemp()

  generateFiles = set(request.file_to_generate)
  files = []
  for file in request.proto_file:
    if file.name not in generateFiles:
      continue

    name = file.name.split('.')[0]
    files.append(name)

    context = {
      'moduleName': name,
      'messages': file.message_type
    }

    cFilePath = os.path.join(path, name + '.c')
    with open(cFilePath, 'w') as f:
      t = Template(resource_string(__name__, 'template/module.jinja.c'))
      f.write(t.render(context))

  setupPyPath = os.path.join(path, 'setup.py')
  with open(setupPyPath, 'w') as f:
    t = Template(resource_string(__name__, 'template/setup.jinja.py'))
    f.write(t.render({'files': files}))

  print >> log, path


if __name__ == '__main__':
  main()

## Instruction:
Use protoc for file output
## Code After:

import plugin_pb2

from jinja2 import Template
from pkg_resources import resource_string

import os.path
import sys
import tempfile


def main():
  request = plugin_pb2.CodeGeneratorRequest()
  request.ParseFromString(sys.stdin.read())

  response = plugin_pb2.CodeGeneratorResponse()

  generateFiles = set(request.file_to_generate)
  files = []
  for file in request.proto_file:
    if file.name not in generateFiles:
      continue

    name = file.name.split('.')[0]
    files.append(name)

    context = {
      'moduleName': name,
      'messages': file.message_type
    }

    # Write the C file.
    t = Template(resource_string(__name__, 'template/module.jinja.c'))
    cFile = response.file.add()
    cFile.name = name + '.c'
    cFile.content = t.render(context)

  # Write setup.py.
  t = Template(resource_string(__name__, 'template/setup.jinja.py'))
  setupFile = response.file.add()
  setupFile.name = 'setup.py'
  setupFile.content = t.render({'files': files})

  sys.stdout.write(response.SerializeToString())


if __name__ == '__main__':
  main()
