setup.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #! /usr/bin/python
  2. #
  3. # See README for usage instructions.
  4. # We must use setuptools, not distutils, because we need to use the
  5. # namespace_packages option for the "google" package.
  6. from ez_setup import use_setuptools
  7. use_setuptools()
  8. from setuptools import setup
  9. from distutils.spawn import find_executable
  10. import sys
  11. import os
  12. import subprocess
  13. maintainer_email = "protobuf@googlegroups.com"
  14. # Find the Protocol Compiler.
  15. if os.path.exists("../src/protoc"):
  16. protoc = "../src/protoc"
  17. else:
  18. protoc = find_executable("protoc")
  19. def generate_proto(source):
  20. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  21. .proto file. Does nothing if the output already exists and is newer than
  22. the input."""
  23. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  24. if not os.path.exists(source):
  25. print "Can't find required file: " + source
  26. sys.exit(-1)
  27. if (not os.path.exists(output) or
  28. (os.path.exists(source) and
  29. os.path.getmtime(source) > os.path.getmtime(output))):
  30. print "Generating %s..." % output
  31. if protoc == None:
  32. sys.stderr.write(
  33. "protoc is not installed nor found in ../src. Please compile it "
  34. "or install the binary package.\n")
  35. sys.exit(-1)
  36. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  37. if subprocess.call(protoc_command) != 0:
  38. sys.exit(-1)
  39. def MakeTestSuite():
  40. # This is apparently needed on some systems to make sure that the tests
  41. # work even if a previous version is already installed.
  42. if 'google' in sys.modules:
  43. del sys.modules['google']
  44. generate_proto("../src/google/protobuf/unittest.proto")
  45. generate_proto("../src/google/protobuf/unittest_import.proto")
  46. generate_proto("../src/google/protobuf/unittest_mset.proto")
  47. generate_proto("google/protobuf/internal/more_extensions.proto")
  48. generate_proto("google/protobuf/internal/more_messages.proto")
  49. import unittest
  50. import google.protobuf.internal.generator_test as generator_test
  51. import google.protobuf.internal.decoder_test as decoder_test
  52. import google.protobuf.internal.descriptor_test as descriptor_test
  53. import google.protobuf.internal.encoder_test as encoder_test
  54. import google.protobuf.internal.input_stream_test as input_stream_test
  55. import google.protobuf.internal.output_stream_test as output_stream_test
  56. import google.protobuf.internal.reflection_test as reflection_test
  57. import google.protobuf.internal.service_reflection_test \
  58. as service_reflection_test
  59. import google.protobuf.internal.text_format_test as text_format_test
  60. import google.protobuf.internal.wire_format_test as wire_format_test
  61. loader = unittest.defaultTestLoader
  62. suite = unittest.TestSuite()
  63. for test in [ generator_test,
  64. decoder_test,
  65. descriptor_test,
  66. encoder_test,
  67. input_stream_test,
  68. output_stream_test,
  69. reflection_test,
  70. service_reflection_test,
  71. text_format_test,
  72. wire_format_test ]:
  73. suite.addTest(loader.loadTestsFromModule(test))
  74. return suite
  75. if __name__ == '__main__':
  76. # TODO(kenton): Integrate this into setuptools somehow?
  77. if len(sys.argv) >= 2 and sys.argv[1] == "clean":
  78. # Delete generated _pb2.py files and .pyc files in the code tree.
  79. for (dirpath, dirnames, filenames) in os.walk("."):
  80. for filename in filenames:
  81. filepath = os.path.join(dirpath, filename)
  82. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc"):
  83. os.remove(filepath)
  84. else:
  85. # Generate necessary .proto file if it doesn't exist.
  86. # TODO(kenton): Maybe we should hook this into a distutils command?
  87. generate_proto("../src/google/protobuf/descriptor.proto")
  88. setup(name = 'protobuf',
  89. version = '2.0.4-SNAPSHOT',
  90. packages = [ 'google' ],
  91. namespace_packages = [ 'google' ],
  92. test_suite = 'setup.MakeTestSuite',
  93. # Must list modules explicitly so that we don't install tests.
  94. py_modules = [
  95. 'google.protobuf.internal.containers',
  96. 'google.protobuf.internal.decoder',
  97. 'google.protobuf.internal.encoder',
  98. 'google.protobuf.internal.input_stream',
  99. 'google.protobuf.internal.message_listener',
  100. 'google.protobuf.internal.output_stream',
  101. 'google.protobuf.internal.type_checkers',
  102. 'google.protobuf.internal.wire_format',
  103. 'google.protobuf.descriptor',
  104. 'google.protobuf.descriptor_pb2',
  105. 'google.protobuf.message',
  106. 'google.protobuf.reflection',
  107. 'google.protobuf.service',
  108. 'google.protobuf.service_reflection',
  109. 'google.protobuf.text_format' ],
  110. url = 'http://code.google.com/p/protobuf/',
  111. maintainer = maintainer_email,
  112. maintainer_email = 'protobuf@googlegroups.com',
  113. license = 'New BSD License',
  114. description = 'Protocol Buffers',
  115. long_description =
  116. "Protocol Buffers are Google's data interchange format.",
  117. )