setup.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. elif os.path.exists("../vsprojects/Debug/protoc.exe"):
  20. protoc = "../vsprojects/Debug/protoc.exe"
  21. elif os.path.exists("../vsprojects/Release/protoc.exe"):
  22. protoc = "../vsprojects/Release/protoc.exe"
  23. else:
  24. protoc = find_executable("protoc")
  25. def generate_proto(source):
  26. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  27. .proto file. Does nothing if the output already exists and is newer than
  28. the input."""
  29. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  30. if not os.path.exists(source):
  31. print "Can't find required file: " + source
  32. sys.exit(-1)
  33. if (not os.path.exists(output) or
  34. (os.path.exists(source) and
  35. os.path.getmtime(source) > os.path.getmtime(output))):
  36. print "Generating %s..." % output
  37. if protoc == None:
  38. sys.stderr.write(
  39. "protoc is not installed nor found in ../src. Please compile it "
  40. "or install the binary package.\n")
  41. sys.exit(-1)
  42. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  43. if subprocess.call(protoc_command) != 0:
  44. sys.exit(-1)
  45. def MakeTestSuite():
  46. # This is apparently needed on some systems to make sure that the tests
  47. # work even if a previous version is already installed.
  48. if 'google' in sys.modules:
  49. del sys.modules['google']
  50. generate_proto("../src/google/protobuf/unittest.proto")
  51. generate_proto("../src/google/protobuf/unittest_import.proto")
  52. generate_proto("../src/google/protobuf/unittest_mset.proto")
  53. generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
  54. generate_proto("google/protobuf/internal/more_extensions.proto")
  55. generate_proto("google/protobuf/internal/more_messages.proto")
  56. import unittest
  57. import google.protobuf.internal.generator_test as generator_test
  58. import google.protobuf.internal.descriptor_test as descriptor_test
  59. import google.protobuf.internal.reflection_test as reflection_test
  60. import google.protobuf.internal.service_reflection_test \
  61. as service_reflection_test
  62. import google.protobuf.internal.text_format_test as text_format_test
  63. import google.protobuf.internal.wire_format_test as wire_format_test
  64. loader = unittest.defaultTestLoader
  65. suite = unittest.TestSuite()
  66. for test in [ generator_test,
  67. descriptor_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.3.1-pre',
  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.message_listener',
  98. 'google.protobuf.internal.type_checkers',
  99. 'google.protobuf.internal.wire_format',
  100. 'google.protobuf.descriptor',
  101. 'google.protobuf.descriptor_pb2',
  102. 'google.protobuf.message',
  103. 'google.protobuf.reflection',
  104. 'google.protobuf.service',
  105. 'google.protobuf.service_reflection',
  106. 'google.protobuf.text_format' ],
  107. url = 'http://code.google.com/p/protobuf/',
  108. maintainer = maintainer_email,
  109. maintainer_email = 'protobuf@googlegroups.com',
  110. license = 'New BSD License',
  111. description = 'Protocol Buffers',
  112. long_description =
  113. "Protocol Buffers are Google's data interchange format.",
  114. )