blob: 47646a81bcc5741947e7478228103f65a7e5e694 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001#! /usr/bin/python
2#
3# See README for usage instructions.
4
5# We must use setuptools, not distutils, because we need to use the
6# namespace_packages option for the "google" package.
7from ez_setup import use_setuptools
8use_setuptools()
9
liujisi@google.com33165fe2010-11-02 13:14:58 +000010from setuptools import setup, Extension
temporal40ee5512008-07-10 02:12:20 +000011from distutils.spawn import find_executable
12import sys
13import os
kenton@google.coma6de64a2009-04-18 02:28:15 +000014import subprocess
temporal40ee5512008-07-10 02:12:20 +000015
16maintainer_email = "protobuf@googlegroups.com"
17
18# Find the Protocol Compiler.
19if os.path.exists("../src/protoc"):
20 protoc = "../src/protoc"
kenton@google.com4152d552009-04-22 03:20:21 +000021elif os.path.exists("../src/protoc.exe"):
22 protoc = "../src/protoc.exe"
kenton@google.com80aa23d2010-09-28 21:58:36 +000023elif os.path.exists("../vsprojects/Debug/protoc.exe"):
24 protoc = "../vsprojects/Debug/protoc.exe"
25elif os.path.exists("../vsprojects/Release/protoc.exe"):
26 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000027else:
28 protoc = find_executable("protoc")
29
30def generate_proto(source):
31 """Invokes the Protocol Compiler to generate a _pb2.py from the given
32 .proto file. Does nothing if the output already exists and is newer than
33 the input."""
34
35 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
36
37 if not os.path.exists(source):
38 print "Can't find required file: " + source
39 sys.exit(-1)
40
41 if (not os.path.exists(output) or
42 (os.path.exists(source) and
43 os.path.getmtime(source) > os.path.getmtime(output))):
44 print "Generating %s..." % output
45
46 if protoc == None:
47 sys.stderr.write(
48 "protoc is not installed nor found in ../src. Please compile it "
49 "or install the binary package.\n")
50 sys.exit(-1)
51
kenton@google.coma6de64a2009-04-18 02:28:15 +000052 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
53 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000054 sys.exit(-1)
55
56def MakeTestSuite():
temporalbf86b542008-09-15 17:58:05 +000057 # This is apparently needed on some systems to make sure that the tests
58 # work even if a previous version is already installed.
59 if 'google' in sys.modules:
60 del sys.modules['google']
61
temporal40ee5512008-07-10 02:12:20 +000062 generate_proto("../src/google/protobuf/unittest.proto")
liujisi@google.com33165fe2010-11-02 13:14:58 +000063 generate_proto("../src/google/protobuf/unittest_custom_options.proto")
temporal40ee5512008-07-10 02:12:20 +000064 generate_proto("../src/google/protobuf/unittest_import.proto")
65 generate_proto("../src/google/protobuf/unittest_mset.proto")
kenton@google.comfccb1462009-12-18 02:11:36 +000066 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
temporal40ee5512008-07-10 02:12:20 +000067 generate_proto("google/protobuf/internal/more_extensions.proto")
68 generate_proto("google/protobuf/internal/more_messages.proto")
69
70 import unittest
71 import google.protobuf.internal.generator_test as generator_test
temporal40ee5512008-07-10 02:12:20 +000072 import google.protobuf.internal.descriptor_test as descriptor_test
temporal40ee5512008-07-10 02:12:20 +000073 import google.protobuf.internal.reflection_test as reflection_test
74 import google.protobuf.internal.service_reflection_test \
75 as service_reflection_test
76 import google.protobuf.internal.text_format_test as text_format_test
77 import google.protobuf.internal.wire_format_test as wire_format_test
78
79 loader = unittest.defaultTestLoader
80 suite = unittest.TestSuite()
81 for test in [ generator_test,
temporal40ee5512008-07-10 02:12:20 +000082 descriptor_test,
temporal40ee5512008-07-10 02:12:20 +000083 reflection_test,
84 service_reflection_test,
85 text_format_test,
86 wire_format_test ]:
87 suite.addTest(loader.loadTestsFromModule(test))
88
89 return suite
90
91if __name__ == '__main__':
92 # TODO(kenton): Integrate this into setuptools somehow?
93 if len(sys.argv) >= 2 and sys.argv[1] == "clean":
94 # Delete generated _pb2.py files and .pyc files in the code tree.
95 for (dirpath, dirnames, filenames) in os.walk("."):
96 for filename in filenames:
97 filepath = os.path.join(dirpath, filename)
liujisi@google.com33165fe2010-11-02 13:14:58 +000098 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
99 filepath.endswith(".so") or filepath.endswith(".o"):
temporal40ee5512008-07-10 02:12:20 +0000100 os.remove(filepath)
101 else:
102 # Generate necessary .proto file if it doesn't exist.
103 # TODO(kenton): Maybe we should hook this into a distutils command?
104 generate_proto("../src/google/protobuf/descriptor.proto")
105
liujisi@google.com33165fe2010-11-02 13:14:58 +0000106 python_c_extension = Extension("google.protobuf.internal._net_proto2___python",
107 [ "google/protobuf/pyext/python_descriptor.cc",
108 "google/protobuf/pyext/python_protobuf.cc",
109 "google/protobuf/pyext/python-proto2.cc",
110 ],
111 include_dirs = [ "../src", ".", ],
112 libraries = [ "protobuf" ],
113 runtime_library_dirs = [ "../src/.libs" ],
114 library_dirs = [ "../src/.libs" ],
115 )
116
temporal40ee5512008-07-10 02:12:20 +0000117 setup(name = 'protobuf',
liujisi@google.com33165fe2010-11-02 13:14:58 +0000118 version = '2.4.0-pre',
temporal40ee5512008-07-10 02:12:20 +0000119 packages = [ 'google' ],
120 namespace_packages = [ 'google' ],
121 test_suite = 'setup.MakeTestSuite',
122 # Must list modules explicitly so that we don't install tests.
123 py_modules = [
liujisi@google.com33165fe2010-11-02 13:14:58 +0000124 'google.protobuf.internal.api_implementation',
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000125 'google.protobuf.internal.containers',
liujisi@google.com33165fe2010-11-02 13:14:58 +0000126 'google.protobuf.internal.cpp_message',
temporal40ee5512008-07-10 02:12:20 +0000127 'google.protobuf.internal.decoder',
128 'google.protobuf.internal.encoder',
temporal40ee5512008-07-10 02:12:20 +0000129 'google.protobuf.internal.message_listener',
liujisi@google.com33165fe2010-11-02 13:14:58 +0000130 'google.protobuf.internal.python_message',
temporalea9d0d82008-08-18 22:38:20 +0000131 'google.protobuf.internal.type_checkers',
temporal40ee5512008-07-10 02:12:20 +0000132 'google.protobuf.internal.wire_format',
133 'google.protobuf.descriptor',
134 'google.protobuf.descriptor_pb2',
135 'google.protobuf.message',
136 'google.protobuf.reflection',
137 'google.protobuf.service',
138 'google.protobuf.service_reflection',
temporalea9d0d82008-08-18 22:38:20 +0000139 'google.protobuf.text_format' ],
liujisi@google.com33165fe2010-11-02 13:14:58 +0000140 ext_modules = [ python_c_extension ],
temporal40ee5512008-07-10 02:12:20 +0000141 url = 'http://code.google.com/p/protobuf/',
142 maintainer = maintainer_email,
143 maintainer_email = 'protobuf@googlegroups.com',
kenton@google.com24bf56f2008-09-24 20:31:01 +0000144 license = 'New BSD License',
temporal40ee5512008-07-10 02:12:20 +0000145 description = 'Protocol Buffers',
146 long_description =
147 "Protocol Buffers are Google's data interchange format.",
148 )