setup.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. elif os.path.exists("../src/protoc.exe"):
  18. protoc = "../src/protoc.exe"
  19. else:
  20. protoc = find_executable("protoc")
  21. def generate_proto(source):
  22. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  23. .proto file. Does nothing if the output already exists and is newer than
  24. the input."""
  25. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  26. if not os.path.exists(source):
  27. print "Can't find required file: " + source
  28. sys.exit(-1)
  29. if (not os.path.exists(output) or
  30. (os.path.exists(source) and
  31. os.path.getmtime(source) > os.path.getmtime(output))):
  32. print "Generating %s..." % output
  33. if protoc == None:
  34. sys.stderr.write(
  35. "protoc is not installed nor found in ../src. Please compile it "
  36. "or install the binary package.\n")
  37. sys.exit(-1)
  38. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  39. if subprocess.call(protoc_command) != 0:
  40. sys.exit(-1)
  41. def MakeTestSuite():
  42. # This is apparently needed on some systems to make sure that the tests
  43. # work even if a previous version is already installed.
  44. if 'google' in sys.modules:
  45. del sys.modules['google']
  46. generate_proto("../src/google/protobuf/unittest.proto")
  47. generate_proto("../src/google/protobuf/unittest_import.proto")
  48. generate_proto("../src/google/protobuf/unittest_mset.proto")
  49. generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
  50. generate_proto("google/protobuf/internal/more_extensions.proto")
  51. generate_proto("google/protobuf/internal/more_messages.proto")
  52. import unittest
  53. import google.protobuf.internal.generator_test as generator_test
  54. import google.protobuf.internal.descriptor_test as descriptor_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. descriptor_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.2.1-pre',
  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.containers',
  91. 'google.protobuf.internal.decoder',
  92. 'google.protobuf.internal.encoder',
  93. 'google.protobuf.internal.message_listener',
  94. 'google.protobuf.internal.type_checkers',
  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 = 'New BSD License',
  107. description = 'Protocol Buffers',
  108. long_description =
  109. "Protocol Buffers are Google's data interchange format.",
  110. )