blob: 2bf0048b4b4b49cac62c30e7e0814db4c4042ae4 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001#! /usr/bin/python
2#
3# See README for usage instructions.
temporal40ee5512008-07-10 02:12:20 +00004import sys
5import os
kenton@google.coma6de64a2009-04-18 02:28:15 +00006import subprocess
temporal40ee5512008-07-10 02:12:20 +00007
liujisi@google.com9ced30c2012-08-01 06:22:19 +00008# We must use setuptools, not distutils, because we need to use the
9# namespace_packages option for the "google" package.
10try:
11 from setuptools import setup, Extension
12except ImportError:
13 try:
14 from ez_setup import use_setuptools
15 use_setuptools()
16 from setuptools import setup, Extension
17 except ImportError:
18 sys.stderr.write(
19 "Could not import setuptools; make sure you have setuptools or "
20 "ez_setup installed.\n")
21 raise
22from distutils.command.clean import clean as _clean
23from distutils.command.build_py import build_py as _build_py
24from distutils.spawn import find_executable
25
temporal40ee5512008-07-10 02:12:20 +000026maintainer_email = "protobuf@googlegroups.com"
27
28# Find the Protocol Compiler.
liujisi@google.come34f1f62012-12-05 01:25:12 +000029if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
30 protoc = os.environ['PROTOC']
31elif os.path.exists("../src/protoc"):
temporal40ee5512008-07-10 02:12:20 +000032 protoc = "../src/protoc"
kenton@google.com4152d552009-04-22 03:20:21 +000033elif os.path.exists("../src/protoc.exe"):
34 protoc = "../src/protoc.exe"
kenton@google.com80aa23d2010-09-28 21:58:36 +000035elif os.path.exists("../vsprojects/Debug/protoc.exe"):
36 protoc = "../vsprojects/Debug/protoc.exe"
37elif os.path.exists("../vsprojects/Release/protoc.exe"):
38 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000039else:
40 protoc = find_executable("protoc")
41
42def generate_proto(source):
43 """Invokes the Protocol Compiler to generate a _pb2.py from the given
44 .proto file. Does nothing if the output already exists and is newer than
45 the input."""
46
47 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
48
temporal40ee5512008-07-10 02:12:20 +000049 if (not os.path.exists(output) or
50 (os.path.exists(source) and
51 os.path.getmtime(source) > os.path.getmtime(output))):
52 print "Generating %s..." % output
53
liujisi@google.com9ced30c2012-08-01 06:22:19 +000054 if not os.path.exists(source):
55 sys.stderr.write("Can't find required file: %s\n" % source)
56 sys.exit(-1)
57
temporal40ee5512008-07-10 02:12:20 +000058 if protoc == None:
59 sys.stderr.write(
60 "protoc is not installed nor found in ../src. Please compile it "
61 "or install the binary package.\n")
62 sys.exit(-1)
63
kenton@google.coma6de64a2009-04-18 02:28:15 +000064 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
65 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000066 sys.exit(-1)
67
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000068def GenerateUnittestProtos():
69 generate_proto("../src/google/protobuf/unittest.proto")
70 generate_proto("../src/google/protobuf/unittest_custom_options.proto")
71 generate_proto("../src/google/protobuf/unittest_import.proto")
72 generate_proto("../src/google/protobuf/unittest_import_public.proto")
73 generate_proto("../src/google/protobuf/unittest_mset.proto")
74 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto")
75 generate_proto("google/protobuf/internal/test_bad_identifiers.proto")
76 generate_proto("google/protobuf/internal/more_extensions.proto")
77 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto")
78 generate_proto("google/protobuf/internal/more_messages.proto")
79 generate_proto("google/protobuf/internal/factory_test1.proto")
80 generate_proto("google/protobuf/internal/factory_test2.proto")
81
temporal40ee5512008-07-10 02:12:20 +000082def MakeTestSuite():
temporalbf86b542008-09-15 17:58:05 +000083 # This is apparently needed on some systems to make sure that the tests
84 # work even if a previous version is already installed.
85 if 'google' in sys.modules:
86 del sys.modules['google']
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000087 GenerateUnittestProtos()
temporal40ee5512008-07-10 02:12:20 +000088
89 import unittest
90 import google.protobuf.internal.generator_test as generator_test
temporal40ee5512008-07-10 02:12:20 +000091 import google.protobuf.internal.descriptor_test as descriptor_test
temporal40ee5512008-07-10 02:12:20 +000092 import google.protobuf.internal.reflection_test as reflection_test
93 import google.protobuf.internal.service_reflection_test \
94 as service_reflection_test
95 import google.protobuf.internal.text_format_test as text_format_test
96 import google.protobuf.internal.wire_format_test as wire_format_test
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000097 import google.protobuf.internal.unknown_fields_test as unknown_fields_test
98 import google.protobuf.internal.descriptor_database_test \
99 as descriptor_database_test
100 import google.protobuf.internal.descriptor_pool_test as descriptor_pool_test
101 import google.protobuf.internal.message_factory_test as message_factory_test
102 import google.protobuf.internal.message_cpp_test as message_cpp_test
103 import google.protobuf.internal.reflection_cpp_generated_test \
104 as reflection_cpp_generated_test
temporal40ee5512008-07-10 02:12:20 +0000105
106 loader = unittest.defaultTestLoader
107 suite = unittest.TestSuite()
108 for test in [ generator_test,
temporal40ee5512008-07-10 02:12:20 +0000109 descriptor_test,
temporal40ee5512008-07-10 02:12:20 +0000110 reflection_test,
111 service_reflection_test,
112 text_format_test,
113 wire_format_test ]:
114 suite.addTest(loader.loadTestsFromModule(test))
115
116 return suite
117
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000118
119class clean(_clean):
120 def run(self):
121 # Delete generated files in the code tree.
temporal40ee5512008-07-10 02:12:20 +0000122 for (dirpath, dirnames, filenames) in os.walk("."):
123 for filename in filenames:
124 filepath = os.path.join(dirpath, filename)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000125 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000126 filepath.endswith(".so") or filepath.endswith(".o") or \
127 filepath.endswith('google/protobuf/compiler/__init__.py'):
temporal40ee5512008-07-10 02:12:20 +0000128 os.remove(filepath)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000129 # _clean is an old-style class, so super() doesn't work.
130 _clean.run(self)
131
132class build_py(_build_py):
133 def run(self):
temporal40ee5512008-07-10 02:12:20 +0000134 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20 +0000135 generate_proto("../src/google/protobuf/descriptor.proto")
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000136 generate_proto("../src/google/protobuf/compiler/plugin.proto")
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000137
138 GenerateUnittestProtos()
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000139 # Make sure google.protobuf.compiler is a valid package.
140 open('google/protobuf/compiler/__init__.py', 'a').close()
141 # _build_py is an old-style class, so super() doesn't work.
142 _build_py.run(self)
temporal40ee5512008-07-10 02:12:20 +0000143
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000144if __name__ == '__main__':
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000145 ext_module_list = []
146
147 # C++ implementation extension
148 if os.getenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python") == "cpp":
149 print "Using EXPERIMENTAL C++ Implmenetation."
150 ext_module_list.append(Extension(
151 "google.protobuf.internal._net_proto2___python",
152 [ "google/protobuf/pyext/python_descriptor.cc",
153 "google/protobuf/pyext/python_protobuf.cc",
154 "google/protobuf/pyext/python-proto2.cc" ],
liujisi@google.com03aaa042011-04-29 02:12:48 +0000155 include_dirs = [ "." ],
156 libraries = [ "protobuf" ]))
liujisi@google.com33165fe2010-11-02 13:14:58 +0000157
temporal40ee5512008-07-10 02:12:20 +0000158 setup(name = 'protobuf',
liujisi@google.comef9acc42012-11-30 22:38:00 +0000159 version = '2.5.0-pre',
temporal40ee5512008-07-10 02:12:20 +0000160 packages = [ 'google' ],
161 namespace_packages = [ 'google' ],
162 test_suite = 'setup.MakeTestSuite',
163 # Must list modules explicitly so that we don't install tests.
164 py_modules = [
liujisi@google.com33165fe2010-11-02 13:14:58 +0000165 'google.protobuf.internal.api_implementation',
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000166 'google.protobuf.internal.containers',
liujisi@google.com33165fe2010-11-02 13:14:58 +0000167 'google.protobuf.internal.cpp_message',
temporal40ee5512008-07-10 02:12:20 +0000168 'google.protobuf.internal.decoder',
169 'google.protobuf.internal.encoder',
temporal40ee5512008-07-10 02:12:20 +0000170 'google.protobuf.internal.message_listener',
liujisi@google.com33165fe2010-11-02 13:14:58 +0000171 'google.protobuf.internal.python_message',
temporalea9d0d82008-08-18 22:38:20 +0000172 'google.protobuf.internal.type_checkers',
temporal40ee5512008-07-10 02:12:20 +0000173 'google.protobuf.internal.wire_format',
174 'google.protobuf.descriptor',
175 'google.protobuf.descriptor_pb2',
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000176 'google.protobuf.compiler.plugin_pb2',
temporal40ee5512008-07-10 02:12:20 +0000177 'google.protobuf.message',
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000178 'google.protobuf.descriptor_database',
179 'google.protobuf.descriptor_pool',
180 'google.protobuf.message_factory',
temporal40ee5512008-07-10 02:12:20 +0000181 'google.protobuf.reflection',
182 'google.protobuf.service',
183 'google.protobuf.service_reflection',
temporalea9d0d82008-08-18 22:38:20 +0000184 'google.protobuf.text_format' ],
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000185 cmdclass = { 'clean': clean, 'build_py': build_py },
186 install_requires = ['setuptools'],
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000187 ext_modules = ext_module_list,
temporal40ee5512008-07-10 02:12:20 +0000188 url = 'http://code.google.com/p/protobuf/',
189 maintainer = maintainer_email,
190 maintainer_email = 'protobuf@googlegroups.com',
kenton@google.com24bf56f2008-09-24 20:31:01 +0000191 license = 'New BSD License',
temporal40ee5512008-07-10 02:12:20 +0000192 description = 'Protocol Buffers',
193 long_description =
194 "Protocol Buffers are Google's data interchange format.",
195 )