setup.py 7.3 KB

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