PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/python/setup.py

http://protobuf.googlecode.com/
Python | 195 lines | 171 code | 10 blank | 14 comment | 13 complexity | 2ca285c3e0cf8120abf16ecb911c3417 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #! /usr/bin/python
  2. #
  3. # See README for usage instructions.
  4. import sys
  5. import os
  6. import subprocess
  7. # We must use setuptools, not distutils, because we need to use the
  8. # namespace_packages option for the "google" package.
  9. try:
  10. from setuptools import setup, Extension
  11. except ImportError:
  12. try:
  13. from ez_setup import use_setuptools
  14. use_setuptools()
  15. from setuptools import setup, Extension
  16. except ImportError:
  17. sys.stderr.write(
  18. "Could not import setuptools; make sure you have setuptools or "
  19. "ez_setup installed.\n")
  20. raise
  21. from distutils.command.clean import clean as _clean
  22. from distutils.command.build_py import build_py as _build_py
  23. from distutils.spawn import find_executable
  24. maintainer_email = "protobuf@googlegroups.com"
  25. # Find the Protocol Compiler.
  26. if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
  27. protoc = os.environ['PROTOC']
  28. elif os.path.exists("../src/protoc"):
  29. protoc = "../src/protoc"
  30. elif os.path.exists("../src/protoc.exe"):
  31. protoc = "../src/protoc.exe"
  32. elif os.path.exists("../vsprojects/Debug/protoc.exe"):
  33. protoc = "../vsprojects/Debug/protoc.exe"
  34. elif os.path.exists("../vsprojects/Release/protoc.exe"):
  35. protoc = "../vsprojects/Release/protoc.exe"
  36. else:
  37. protoc = find_executable("protoc")
  38. def generate_proto(source):
  39. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  40. .proto file. Does nothing if the output already exists and is newer than
  41. the input."""
  42. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  43. if (not os.path.exists(output) or
  44. (os.path.exists(source) and
  45. os.path.getmtime(source) > os.path.getmtime(output))):
  46. print "Generating %s..." % output
  47. if not os.path.exists(source):
  48. sys.stderr.write("Can't find required file: %s\n" % source)
  49. sys.exit(-1)
  50. if protoc == None:
  51. sys.stderr.write(
  52. "protoc is not installed nor found in ../src. Please compile it "
  53. "or install the binary package.\n")
  54. sys.exit(-1)
  55. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  56. if subprocess.call(protoc_command) != 0:
  57. sys.exit(-1)
  58. def GenerateUnittestProtos():
  59. generate_proto("../src/google/protobuf/unittest.proto")
  60. generate_proto("../src/google/protobuf/unittest_custom_options.proto")
  61. generate_proto("../src/google/protobuf/unittest_import.proto")
  62. generate_proto("../src/google/protobuf/unittest_import_public.proto")
  63. generate_proto("../src/google/protobuf/unittest_mset.proto")
  64. generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
  65. generate_proto("google/protobuf/internal/test_bad_identifiers.proto")
  66. generate_proto("google/protobuf/internal/more_extensions.proto")
  67. generate_proto("google/protobuf/internal/more_extensions_dynamic.proto")
  68. generate_proto("google/protobuf/internal/more_messages.proto")
  69. generate_proto("google/protobuf/internal/factory_test1.proto")
  70. generate_proto("google/protobuf/internal/factory_test2.proto")
  71. def MakeTestSuite():
  72. # This is apparently needed on some systems to make sure that the tests
  73. # work even if a previous version is already installed.
  74. if 'google' in sys.modules:
  75. del sys.modules['google']
  76. GenerateUnittestProtos()
  77. import unittest
  78. import google.protobuf.internal.generator_test as generator_test
  79. import google.protobuf.internal.descriptor_test as descriptor_test
  80. import google.protobuf.internal.reflection_test as reflection_test
  81. import google.protobuf.internal.service_reflection_test \
  82. as service_reflection_test
  83. import google.protobuf.internal.text_format_test as text_format_test
  84. import google.protobuf.internal.wire_format_test as wire_format_test
  85. import google.protobuf.internal.unknown_fields_test as unknown_fields_test
  86. import google.protobuf.internal.descriptor_database_test \
  87. as descriptor_database_test
  88. import google.protobuf.internal.descriptor_pool_test as descriptor_pool_test
  89. import google.protobuf.internal.message_factory_test as message_factory_test
  90. import google.protobuf.internal.message_cpp_test as message_cpp_test
  91. import google.protobuf.internal.reflection_cpp_generated_test \
  92. as reflection_cpp_generated_test
  93. loader = unittest.defaultTestLoader
  94. suite = unittest.TestSuite()
  95. for test in [ generator_test,
  96. descriptor_test,
  97. reflection_test,
  98. service_reflection_test,
  99. text_format_test,
  100. wire_format_test ]:
  101. suite.addTest(loader.loadTestsFromModule(test))
  102. return suite
  103. class clean(_clean):
  104. def run(self):
  105. # Delete generated files in the code tree.
  106. for (dirpath, dirnames, filenames) in os.walk("."):
  107. for filename in filenames:
  108. filepath = os.path.join(dirpath, filename)
  109. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
  110. filepath.endswith(".so") or filepath.endswith(".o") or \
  111. filepath.endswith('google/protobuf/compiler/__init__.py'):
  112. os.remove(filepath)
  113. # _clean is an old-style class, so super() doesn't work.
  114. _clean.run(self)
  115. class build_py(_build_py):
  116. def run(self):
  117. # Generate necessary .proto file if it doesn't exist.
  118. generate_proto("../src/google/protobuf/descriptor.proto")
  119. generate_proto("../src/google/protobuf/compiler/plugin.proto")
  120. # Make sure google.protobuf.compiler is a valid package.
  121. open('google/protobuf/compiler/__init__.py', 'a').close()
  122. # _build_py is an old-style class, so super() doesn't work.
  123. _build_py.run(self)
  124. if __name__ == '__main__':
  125. ext_module_list = []
  126. # C++ implementation extension
  127. if os.getenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python") == "cpp":
  128. print "Using EXPERIMENTAL C++ Implmenetation."
  129. ext_module_list.append(Extension(
  130. "google.protobuf.internal._net_proto2___python",
  131. [ "google/protobuf/pyext/python_descriptor.cc",
  132. "google/protobuf/pyext/python_protobuf.cc",
  133. "google/protobuf/pyext/python-proto2.cc" ],
  134. include_dirs = [ "." ],
  135. libraries = [ "protobuf" ]))
  136. setup(name = 'protobuf',
  137. version = '2.5.1-pre',
  138. packages = [ 'google' ],
  139. namespace_packages = [ 'google' ],
  140. test_suite = 'setup.MakeTestSuite',
  141. # Must list modules explicitly so that we don't install tests.
  142. py_modules = [
  143. 'google.protobuf.internal.api_implementation',
  144. 'google.protobuf.internal.containers',
  145. 'google.protobuf.internal.cpp_message',
  146. 'google.protobuf.internal.decoder',
  147. 'google.protobuf.internal.encoder',
  148. 'google.protobuf.internal.enum_type_wrapper',
  149. 'google.protobuf.internal.message_listener',
  150. 'google.protobuf.internal.python_message',
  151. 'google.protobuf.internal.type_checkers',
  152. 'google.protobuf.internal.wire_format',
  153. 'google.protobuf.descriptor',
  154. 'google.protobuf.descriptor_pb2',
  155. 'google.protobuf.compiler.plugin_pb2',
  156. 'google.protobuf.message',
  157. 'google.protobuf.descriptor_database',
  158. 'google.protobuf.descriptor_pool',
  159. 'google.protobuf.message_factory',
  160. 'google.protobuf.reflection',
  161. 'google.protobuf.service',
  162. 'google.protobuf.service_reflection',
  163. 'google.protobuf.text_format' ],
  164. cmdclass = { 'clean': clean, 'build_py': build_py },
  165. install_requires = ['setuptools'],
  166. ext_modules = ext_module_list,
  167. url = 'http://code.google.com/p/protobuf/',
  168. maintainer = maintainer_email,
  169. maintainer_email = 'protobuf@googlegroups.com',
  170. license = 'New BSD License',
  171. description = 'Protocol Buffers',
  172. long_description =
  173. "Protocol Buffers are Google's data interchange format.",
  174. )