setup.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. generate_proto("../src/google/protobuf/unittest.proto")
  40. generate_proto("../src/google/protobuf/unittest_import.proto")
  41. generate_proto("../src/google/protobuf/unittest_mset.proto")
  42. generate_proto("google/protobuf/internal/more_extensions.proto")
  43. generate_proto("google/protobuf/internal/more_messages.proto")
  44. import unittest
  45. import google.protobuf.internal.generator_test as generator_test
  46. import google.protobuf.internal.decoder_test as decoder_test
  47. import google.protobuf.internal.descriptor_test as descriptor_test
  48. import google.protobuf.internal.encoder_test as encoder_test
  49. import google.protobuf.internal.input_stream_test as input_stream_test
  50. import google.protobuf.internal.output_stream_test as output_stream_test
  51. import google.protobuf.internal.reflection_test as reflection_test
  52. import google.protobuf.internal.service_reflection_test \
  53. as service_reflection_test
  54. import google.protobuf.internal.text_format_test as text_format_test
  55. import google.protobuf.internal.wire_format_test as wire_format_test
  56. loader = unittest.defaultTestLoader
  57. suite = unittest.TestSuite()
  58. for test in [ generator_test,
  59. decoder_test,
  60. descriptor_test,
  61. encoder_test,
  62. input_stream_test,
  63. output_stream_test,
  64. reflection_test,
  65. service_reflection_test,
  66. text_format_test,
  67. wire_format_test ]:
  68. suite.addTest(loader.loadTestsFromModule(test))
  69. return suite
  70. if __name__ == '__main__':
  71. # TODO(kenton): Integrate this into setuptools somehow?
  72. if len(sys.argv) >= 2 and sys.argv[1] == "clean":
  73. # Delete generated _pb2.py files and .pyc files in the code tree.
  74. for (dirpath, dirnames, filenames) in os.walk("."):
  75. for filename in filenames:
  76. filepath = os.path.join(dirpath, filename)
  77. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc"):
  78. os.remove(filepath)
  79. else:
  80. # Generate necessary .proto file if it doesn't exist.
  81. # TODO(kenton): Maybe we should hook this into a distutils command?
  82. generate_proto("../src/google/protobuf/descriptor.proto")
  83. setup(name = 'protobuf',
  84. version = '2.0.1-SNAPSHOT',
  85. packages = [ 'google' ],
  86. namespace_packages = [ 'google' ],
  87. test_suite = 'setup.MakeTestSuite',
  88. # Must list modules explicitly so that we don't install tests.
  89. py_modules = [
  90. 'google.protobuf.internal.decoder',
  91. 'google.protobuf.internal.encoder',
  92. 'google.protobuf.internal.input_stream',
  93. 'google.protobuf.internal.message_listener',
  94. 'google.protobuf.internal.output_stream',
  95. 'google.protobuf.internal.wire_format',
  96. 'google.protobuf.descriptor',
  97. 'google.protobuf.descriptor_pb2',
  98. 'google.protobuf.message',
  99. 'google.protobuf.reflection',
  100. 'google.protobuf.service',
  101. 'google.protobuf.service_reflection',
  102. 'google.protobuf.text_format'],
  103. url = 'http://code.google.com/p/protobuf/',
  104. maintainer = maintainer_email,
  105. maintainer_email = 'protobuf@googlegroups.com',
  106. license = 'Apache License, Version 2.0',
  107. description = 'Protocol Buffers',
  108. long_description =
  109. "Protocol Buffers are Google's data interchange format.",
  110. )