setup.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/descriptor_pool_test1.proto")
  66. generate_proto("google/protobuf/internal/descriptor_pool_test2.proto")
  67. generate_proto("google/protobuf/internal/test_bad_identifiers.proto")
  68. generate_proto("google/protobuf/internal/missing_enum_values.proto")
  69. generate_proto("google/protobuf/internal/more_extensions.proto")
  70. generate_proto("google/protobuf/internal/more_extensions_dynamic.proto")
  71. generate_proto("google/protobuf/internal/more_messages.proto")
  72. generate_proto("google/protobuf/internal/factory_test1.proto")
  73. generate_proto("google/protobuf/internal/factory_test2.proto")
  74. generate_proto("google/protobuf/pyext/python.proto")
  75. def MakeTestSuite():
  76. # Test C++ implementation
  77. import unittest
  78. import google.protobuf.pyext.descriptor_cpp2_test as descriptor_cpp2_test
  79. import google.protobuf.pyext.message_factory_cpp2_test \
  80. as message_factory_cpp2_test
  81. import google.protobuf.pyext.reflection_cpp2_generated_test \
  82. as reflection_cpp2_generated_test
  83. loader = unittest.defaultTestLoader
  84. suite = unittest.TestSuite()
  85. for test in [ descriptor_cpp2_test,
  86. message_factory_cpp2_test,
  87. reflection_cpp2_generated_test]:
  88. suite.addTest(loader.loadTestsFromModule(test))
  89. return suite
  90. class clean(_clean):
  91. def run(self):
  92. # Delete generated files in the code tree.
  93. for (dirpath, dirnames, filenames) in os.walk("."):
  94. for filename in filenames:
  95. filepath = os.path.join(dirpath, filename)
  96. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
  97. filepath.endswith(".so") or filepath.endswith(".o") or \
  98. filepath.endswith('google/protobuf/compiler/__init__.py'):
  99. os.remove(filepath)
  100. # _clean is an old-style class, so super() doesn't work.
  101. _clean.run(self)
  102. class build_py(_build_py):
  103. def run(self):
  104. # Generate necessary .proto file if it doesn't exist.
  105. generate_proto("../src/google/protobuf/descriptor.proto")
  106. generate_proto("../src/google/protobuf/compiler/plugin.proto")
  107. GenerateUnittestProtos()
  108. # Make sure google.protobuf/** are valid packages.
  109. for path in ['', 'internal/', 'compiler/', 'pyext/']:
  110. try:
  111. open('google/protobuf/%s__init__.py' % path, 'a').close()
  112. except EnvironmentError:
  113. pass
  114. # _build_py is an old-style class, so super() doesn't work.
  115. _build_py.run(self)
  116. # TODO(mrovner): Subclass to run 2to3 on some files only.
  117. # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's "Approach 2"
  118. # section on how to get 2to3 to run on source files during install under
  119. # Python 3. This class seems like a good place to put logic that calls
  120. # python3's distutils.util.run_2to3 on the subset of the files we have in our
  121. # release that are subject to conversion.
  122. # See code reference in previous code review.
  123. if __name__ == '__main__':
  124. ext_module_list = []
  125. cpp_impl = '--cpp_implementation'
  126. if cpp_impl in sys.argv:
  127. sys.argv.remove(cpp_impl)
  128. # C++ implementation extension
  129. ext_module_list.append(Extension(
  130. "google.protobuf.pyext._message",
  131. [ "google/protobuf/pyext/descriptor.cc",
  132. "google/protobuf/pyext/message.cc",
  133. "google/protobuf/pyext/extension_dict.cc",
  134. "google/protobuf/pyext/repeated_scalar_container.cc",
  135. "google/protobuf/pyext/repeated_composite_container.cc" ],
  136. define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')],
  137. include_dirs = [ ".", "../src"],
  138. libraries = [ "protobuf" ],
  139. library_dirs = [ '../src/.libs' ],
  140. ))
  141. setup(name = 'protobuf',
  142. version = '2.6.1',
  143. packages = [ 'google' ],
  144. namespace_packages = [ 'google' ],
  145. test_suite = 'setup.MakeTestSuite',
  146. google_test_dir = "google/protobuf/internal",
  147. # Must list modules explicitly so that we don't install tests.
  148. py_modules = [
  149. 'google.protobuf.internal.api_implementation',
  150. 'google.protobuf.internal.containers',
  151. 'google.protobuf.internal.cpp_message',
  152. 'google.protobuf.internal.decoder',
  153. 'google.protobuf.internal.encoder',
  154. 'google.protobuf.internal.enum_type_wrapper',
  155. 'google.protobuf.internal.message_listener',
  156. 'google.protobuf.internal.python_message',
  157. 'google.protobuf.internal.type_checkers',
  158. 'google.protobuf.internal.wire_format',
  159. 'google.protobuf.descriptor',
  160. 'google.protobuf.descriptor_pb2',
  161. 'google.protobuf.compiler.plugin_pb2',
  162. 'google.protobuf.message',
  163. 'google.protobuf.descriptor_database',
  164. 'google.protobuf.descriptor_pool',
  165. 'google.protobuf.message_factory',
  166. 'google.protobuf.pyext.cpp_message',
  167. 'google.protobuf.reflection',
  168. 'google.protobuf.service',
  169. 'google.protobuf.service_reflection',
  170. 'google.protobuf.symbol_database',
  171. 'google.protobuf.text_encoding',
  172. 'google.protobuf.text_format'],
  173. cmdclass = { 'clean': clean, 'build_py': build_py },
  174. install_requires = ['setuptools'],
  175. setup_requires = ['google-apputils'],
  176. ext_modules = ext_module_list,
  177. url = 'https://developers.google.com/protocol-buffers/',
  178. maintainer = maintainer_email,
  179. maintainer_email = 'protobuf@googlegroups.com',
  180. license = 'New BSD License',
  181. description = 'Protocol Buffers',
  182. long_description =
  183. "Protocol Buffers are Google's data interchange format.",
  184. )