setup.py 7.5 KB

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