setup.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if sys.version_info[0] >= 3:
  23. # Python 3
  24. from distutils.command.build_py import build_py_2to3 as _build_py
  25. else:
  26. # Python 2
  27. from distutils.command.build_py import build_py as _build_py
  28. from distutils.spawn import find_executable
  29. maintainer_email = "protobuf@googlegroups.com"
  30. # Find the Protocol Compiler.
  31. if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
  32. protoc = os.environ['PROTOC']
  33. elif os.path.exists("../src/protoc"):
  34. protoc = "../src/protoc"
  35. elif os.path.exists("../src/protoc.exe"):
  36. protoc = "../src/protoc.exe"
  37. elif os.path.exists("../vsprojects/Debug/protoc.exe"):
  38. protoc = "../vsprojects/Debug/protoc.exe"
  39. elif os.path.exists("../vsprojects/Release/protoc.exe"):
  40. protoc = "../vsprojects/Release/protoc.exe"
  41. else:
  42. protoc = find_executable("protoc")
  43. def generate_proto(source):
  44. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  45. .proto file. Does nothing if the output already exists and is newer than
  46. the input."""
  47. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  48. if (not os.path.exists(output) or
  49. (os.path.exists(source) and
  50. os.path.getmtime(source) > os.path.getmtime(output))):
  51. print ("Generating %s..." % output)
  52. if not os.path.exists(source):
  53. sys.stderr.write("Can't find required file: %s\n" % source)
  54. sys.exit(-1)
  55. if protoc == None:
  56. sys.stderr.write(
  57. "protoc is not installed nor found in ../src. Please compile it "
  58. "or install the binary package.\n")
  59. sys.exit(-1)
  60. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  61. if subprocess.call(protoc_command) != 0:
  62. sys.exit(-1)
  63. def GenerateUnittestProtos():
  64. generate_proto("../src/google/protobuf/unittest.proto")
  65. generate_proto("../src/google/protobuf/unittest_custom_options.proto")
  66. generate_proto("../src/google/protobuf/unittest_import.proto")
  67. generate_proto("../src/google/protobuf/unittest_import_public.proto")
  68. generate_proto("../src/google/protobuf/unittest_mset.proto")
  69. generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
  70. generate_proto("google/protobuf/internal/descriptor_pool_test1.proto")
  71. generate_proto("google/protobuf/internal/descriptor_pool_test2.proto")
  72. generate_proto("google/protobuf/internal/test_bad_identifiers.proto")
  73. generate_proto("google/protobuf/internal/missing_enum_values.proto")
  74. generate_proto("google/protobuf/internal/more_extensions.proto")
  75. generate_proto("google/protobuf/internal/more_extensions_dynamic.proto")
  76. generate_proto("google/protobuf/internal/more_messages.proto")
  77. generate_proto("google/protobuf/internal/factory_test1.proto")
  78. generate_proto("google/protobuf/internal/factory_test2.proto")
  79. generate_proto("google/protobuf/internal/import_test_package/inner.proto")
  80. generate_proto("google/protobuf/internal/import_test_package/outer.proto")
  81. generate_proto("google/protobuf/pyext/python.proto")
  82. def MakeTestSuite():
  83. # Test C++ implementation
  84. import unittest
  85. import google.protobuf.pyext.descriptor_cpp2_test as descriptor_cpp2_test
  86. import google.protobuf.pyext.message_factory_cpp2_test \
  87. as message_factory_cpp2_test
  88. import google.protobuf.pyext.reflection_cpp2_generated_test \
  89. as reflection_cpp2_generated_test
  90. loader = unittest.defaultTestLoader
  91. suite = unittest.TestSuite()
  92. for test in [ descriptor_cpp2_test,
  93. message_factory_cpp2_test,
  94. reflection_cpp2_generated_test]:
  95. suite.addTest(loader.loadTestsFromModule(test))
  96. return suite
  97. class clean(_clean):
  98. def run(self):
  99. # Delete generated files in the code tree.
  100. for (dirpath, dirnames, filenames) in os.walk("."):
  101. for filename in filenames:
  102. filepath = os.path.join(dirpath, filename)
  103. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
  104. filepath.endswith(".so") or filepath.endswith(".o") or \
  105. filepath.endswith('google/protobuf/compiler/__init__.py'):
  106. os.remove(filepath)
  107. # _clean is an old-style class, so super() doesn't work.
  108. _clean.run(self)
  109. class build_py(_build_py):
  110. def run(self):
  111. # Generate necessary .proto file if it doesn't exist.
  112. generate_proto("../src/google/protobuf/descriptor.proto")
  113. generate_proto("../src/google/protobuf/compiler/plugin.proto")
  114. GenerateUnittestProtos()
  115. # Make sure google.protobuf/** are valid packages.
  116. for path in ['', 'internal/', 'compiler/', 'pyext/']:
  117. try:
  118. open('google/protobuf/%s__init__.py' % path, 'a').close()
  119. except EnvironmentError:
  120. pass
  121. # _build_py is an old-style class, so super() doesn't work.
  122. _build_py.run(self)
  123. # TODO(mrovner): Subclass to run 2to3 on some files only.
  124. # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's "Approach 2"
  125. # section on how to get 2to3 to run on source files during install under
  126. # Python 3. This class seems like a good place to put logic that calls
  127. # python3's distutils.util.run_2to3 on the subset of the files we have in our
  128. # release that are subject to conversion.
  129. # See code reference in previous code review.
  130. if __name__ == '__main__':
  131. ext_module_list = []
  132. cpp_impl = '--cpp_implementation'
  133. if cpp_impl in sys.argv:
  134. sys.argv.remove(cpp_impl)
  135. # C++ implementation extension
  136. ext_module_list.append(Extension(
  137. "google.protobuf.pyext._message",
  138. [ "google/protobuf/pyext/descriptor.cc",
  139. "google/protobuf/pyext/message.cc",
  140. "google/protobuf/pyext/extension_dict.cc",
  141. "google/protobuf/pyext/repeated_scalar_container.cc",
  142. "google/protobuf/pyext/repeated_composite_container.cc" ],
  143. define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')],
  144. include_dirs = [ ".", "..", "../src"],
  145. libraries = [ "protobuf" ],
  146. library_dirs = [ '../src/.libs' ],
  147. ))
  148. setup(name = 'protobuf',
  149. version = '3.0.0-pre',
  150. packages = [ 'google' ],
  151. namespace_packages = [ 'google' ],
  152. test_suite = 'setup.MakeTestSuite',
  153. google_test_dir = "google/protobuf/internal",
  154. # Must list modules explicitly so that we don't install tests.
  155. py_modules = [
  156. 'google.protobuf.internal.api_implementation',
  157. 'google.protobuf.internal.containers',
  158. 'google.protobuf.internal.cpp_message',
  159. 'google.protobuf.internal.decoder',
  160. 'google.protobuf.internal.encoder',
  161. 'google.protobuf.internal.enum_type_wrapper',
  162. 'google.protobuf.internal.message_listener',
  163. 'google.protobuf.internal.python_message',
  164. 'google.protobuf.internal.type_checkers',
  165. 'google.protobuf.internal.wire_format',
  166. 'google.protobuf.descriptor',
  167. 'google.protobuf.descriptor_pb2',
  168. 'google.protobuf.compiler.plugin_pb2',
  169. 'google.protobuf.message',
  170. 'google.protobuf.descriptor_database',
  171. 'google.protobuf.descriptor_pool',
  172. 'google.protobuf.message_factory',
  173. 'google.protobuf.proto_builder',
  174. 'google.protobuf.pyext.cpp_message',
  175. 'google.protobuf.reflection',
  176. 'google.protobuf.service',
  177. 'google.protobuf.service_reflection',
  178. 'google.protobuf.symbol_database',
  179. 'google.protobuf.text_encoding',
  180. 'google.protobuf.text_format'],
  181. cmdclass = { 'clean': clean, 'build_py': build_py },
  182. install_requires = ['setuptools', 'six'],
  183. # TODO: Restore dependency once a Python 3 compatible google-apputils
  184. # is released.
  185. setup_requires = (['google-apputils']
  186. if sys.version_info[0] < 3 else
  187. []),
  188. ext_modules = ext_module_list,
  189. url = 'https://developers.google.com/protocol-buffers/',
  190. maintainer = maintainer_email,
  191. maintainer_email = 'protobuf@googlegroups.com',
  192. license = 'New BSD License',
  193. description = 'Protocol Buffers',
  194. long_description =
  195. "Protocol Buffers are Google's data interchange format.",
  196. )