setup.py 5.0 KB

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