setup.py 7.8 KB

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