temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | # |
| 3 | # See README for usage instructions. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | import sys |
| 5 | import os |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 6 | import subprocess |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 7 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 8 | # We must use setuptools, not distutils, because we need to use the |
| 9 | # namespace_packages option for the "google" package. |
| 10 | try: |
jieluo@google.com | 7580a89 | 2014-08-12 21:28:31 +0000 | [diff] [blame] | 11 | from setuptools import setup, Extension |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 12 | except ImportError: |
jieluo@google.com | 7580a89 | 2014-08-12 21:28:31 +0000 | [diff] [blame] | 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 |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 22 | from distutils.command.clean import clean as _clean |
Dwayne Litzenberger | 0fd260e | 2014-10-14 13:47:06 -0700 | [diff] [blame] | 23 | if sys.version_info[0] >= 3: |
| 24 | # Python 3 |
| 25 | from distutils.command.build_py import build_py_2to3 as _build_py |
| 26 | else: |
| 27 | # Python 2 |
| 28 | from distutils.command.build_py import build_py as _build_py |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 29 | from distutils.spawn import find_executable |
| 30 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 31 | maintainer_email = "protobuf@googlegroups.com" |
| 32 | |
| 33 | # Find the Protocol Compiler. |
liujisi@google.com | e34f1f6 | 2012-12-05 01:25:12 +0000 | [diff] [blame] | 34 | if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): |
| 35 | protoc = os.environ['PROTOC'] |
| 36 | elif os.path.exists("../src/protoc"): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 37 | protoc = "../src/protoc" |
kenton@google.com | 4152d55 | 2009-04-22 03:20:21 +0000 | [diff] [blame] | 38 | elif os.path.exists("../src/protoc.exe"): |
| 39 | protoc = "../src/protoc.exe" |
kenton@google.com | 80aa23d | 2010-09-28 21:58:36 +0000 | [diff] [blame] | 40 | elif os.path.exists("../vsprojects/Debug/protoc.exe"): |
| 41 | protoc = "../vsprojects/Debug/protoc.exe" |
| 42 | elif os.path.exists("../vsprojects/Release/protoc.exe"): |
| 43 | protoc = "../vsprojects/Release/protoc.exe" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | else: |
| 45 | protoc = find_executable("protoc") |
| 46 | |
| 47 | def generate_proto(source): |
| 48 | """Invokes the Protocol Compiler to generate a _pb2.py from the given |
| 49 | .proto file. Does nothing if the output already exists and is newer than |
| 50 | the input.""" |
| 51 | |
| 52 | output = source.replace(".proto", "_pb2.py").replace("../src/", "") |
| 53 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 54 | if (not os.path.exists(output) or |
| 55 | (os.path.exists(source) and |
| 56 | os.path.getmtime(source) > os.path.getmtime(output))): |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 57 | print ("Generating %s..." % output) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 58 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 59 | if not os.path.exists(source): |
| 60 | sys.stderr.write("Can't find required file: %s\n" % source) |
| 61 | sys.exit(-1) |
| 62 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 63 | if protoc == None: |
| 64 | sys.stderr.write( |
| 65 | "protoc is not installed nor found in ../src. Please compile it " |
| 66 | "or install the binary package.\n") |
| 67 | sys.exit(-1) |
| 68 | |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 69 | protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ] |
| 70 | if subprocess.call(protoc_command) != 0: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 71 | sys.exit(-1) |
| 72 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 73 | def GenerateUnittestProtos(): |
| 74 | generate_proto("../src/google/protobuf/unittest.proto") |
| 75 | generate_proto("../src/google/protobuf/unittest_custom_options.proto") |
| 76 | generate_proto("../src/google/protobuf/unittest_import.proto") |
| 77 | generate_proto("../src/google/protobuf/unittest_import_public.proto") |
| 78 | generate_proto("../src/google/protobuf/unittest_mset.proto") |
| 79 | generate_proto("../src/google/protobuf/unittest_no_generic_services.proto") |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 80 | generate_proto("google/protobuf/internal/descriptor_pool_test1.proto") |
| 81 | generate_proto("google/protobuf/internal/descriptor_pool_test2.proto") |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 82 | generate_proto("google/protobuf/internal/test_bad_identifiers.proto") |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 83 | generate_proto("google/protobuf/internal/missing_enum_values.proto") |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 84 | generate_proto("google/protobuf/internal/more_extensions.proto") |
| 85 | generate_proto("google/protobuf/internal/more_extensions_dynamic.proto") |
| 86 | generate_proto("google/protobuf/internal/more_messages.proto") |
| 87 | generate_proto("google/protobuf/internal/factory_test1.proto") |
| 88 | generate_proto("google/protobuf/internal/factory_test2.proto") |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame^] | 89 | generate_proto("google/protobuf/internal/import_test_package/inner.proto") |
| 90 | generate_proto("google/protobuf/internal/import_test_package/outer.proto") |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 91 | generate_proto("google/protobuf/pyext/python.proto") |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 92 | |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 93 | def MakeTestSuite(): |
| 94 | # Test C++ implementation |
| 95 | import unittest |
| 96 | import google.protobuf.pyext.descriptor_cpp2_test as descriptor_cpp2_test |
| 97 | import google.protobuf.pyext.message_factory_cpp2_test \ |
| 98 | as message_factory_cpp2_test |
| 99 | import google.protobuf.pyext.reflection_cpp2_generated_test \ |
| 100 | as reflection_cpp2_generated_test |
| 101 | |
| 102 | loader = unittest.defaultTestLoader |
| 103 | suite = unittest.TestSuite() |
| 104 | for test in [ descriptor_cpp2_test, |
| 105 | message_factory_cpp2_test, |
| 106 | reflection_cpp2_generated_test]: |
| 107 | suite.addTest(loader.loadTestsFromModule(test)) |
| 108 | return suite |
| 109 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 110 | class clean(_clean): |
| 111 | def run(self): |
| 112 | # Delete generated files in the code tree. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 113 | for (dirpath, dirnames, filenames) in os.walk("."): |
| 114 | for filename in filenames: |
| 115 | filepath = os.path.join(dirpath, filename) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 116 | if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 117 | filepath.endswith(".so") or filepath.endswith(".o") or \ |
| 118 | filepath.endswith('google/protobuf/compiler/__init__.py'): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 119 | os.remove(filepath) |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 120 | # _clean is an old-style class, so super() doesn't work. |
| 121 | _clean.run(self) |
| 122 | |
| 123 | class build_py(_build_py): |
| 124 | def run(self): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 125 | # Generate necessary .proto file if it doesn't exist. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 126 | generate_proto("../src/google/protobuf/descriptor.proto") |
liujisi@google.com | 9b7f6c5 | 2010-12-08 03:45:27 +0000 | [diff] [blame] | 127 | generate_proto("../src/google/protobuf/compiler/plugin.proto") |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 128 | GenerateUnittestProtos() |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 129 | |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 130 | # Make sure google.protobuf/** are valid packages. |
| 131 | for path in ['', 'internal/', 'compiler/', 'pyext/']: |
| 132 | try: |
| 133 | open('google/protobuf/%s__init__.py' % path, 'a').close() |
| 134 | except EnvironmentError: |
| 135 | pass |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 136 | # _build_py is an old-style class, so super() doesn't work. |
| 137 | _build_py.run(self) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 138 | # TODO(mrovner): Subclass to run 2to3 on some files only. |
| 139 | # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's "Approach 2" |
| 140 | # section on how to get 2to3 to run on source files during install under |
| 141 | # Python 3. This class seems like a good place to put logic that calls |
| 142 | # python3's distutils.util.run_2to3 on the subset of the files we have in our |
| 143 | # release that are subject to conversion. |
| 144 | # See code reference in previous code review. |
| 145 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 146 | if __name__ == '__main__': |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 147 | ext_module_list = [] |
jieluo@google.com | a21bf2e | 2014-08-25 23:26:40 +0000 | [diff] [blame] | 148 | cpp_impl = '--cpp_implementation' |
| 149 | if cpp_impl in sys.argv: |
jieluo@google.com | b70e586 | 2014-08-13 21:05:19 +0000 | [diff] [blame] | 150 | sys.argv.remove(cpp_impl) |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 151 | # C++ implementation extension |
| 152 | ext_module_list.append(Extension( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 153 | "google.protobuf.pyext._message", |
| 154 | [ "google/protobuf/pyext/descriptor.cc", |
| 155 | "google/protobuf/pyext/message.cc", |
| 156 | "google/protobuf/pyext/extension_dict.cc", |
| 157 | "google/protobuf/pyext/repeated_scalar_container.cc", |
| 158 | "google/protobuf/pyext/repeated_composite_container.cc" ], |
| 159 | define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')], |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 160 | include_dirs = [ ".", "../src"], |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 161 | libraries = [ "protobuf" ], |
| 162 | library_dirs = [ '../src/.libs' ], |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 163 | )) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 164 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 165 | setup(name = 'protobuf', |
Feng Xiao | df5481c | 2014-10-21 16:47:33 -0700 | [diff] [blame] | 166 | version = '2.6.2-pre', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 167 | packages = [ 'google' ], |
| 168 | namespace_packages = [ 'google' ], |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 169 | test_suite = 'setup.MakeTestSuite', |
| 170 | google_test_dir = "google/protobuf/internal", |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 171 | # Must list modules explicitly so that we don't install tests. |
| 172 | py_modules = [ |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 173 | 'google.protobuf.internal.api_implementation', |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 174 | 'google.protobuf.internal.containers', |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 175 | 'google.protobuf.internal.cpp_message', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 176 | 'google.protobuf.internal.decoder', |
| 177 | 'google.protobuf.internal.encoder', |
xiaofeng@google.com | a655b98 | 2012-12-08 18:44:32 +0000 | [diff] [blame] | 178 | 'google.protobuf.internal.enum_type_wrapper', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 179 | 'google.protobuf.internal.message_listener', |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 180 | 'google.protobuf.internal.python_message', |
temporal | ea9d0d8 | 2008-08-18 22:38:20 +0000 | [diff] [blame] | 181 | 'google.protobuf.internal.type_checkers', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 182 | 'google.protobuf.internal.wire_format', |
| 183 | 'google.protobuf.descriptor', |
| 184 | 'google.protobuf.descriptor_pb2', |
liujisi@google.com | 9b7f6c5 | 2010-12-08 03:45:27 +0000 | [diff] [blame] | 185 | 'google.protobuf.compiler.plugin_pb2', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 186 | 'google.protobuf.message', |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 187 | 'google.protobuf.descriptor_database', |
| 188 | 'google.protobuf.descriptor_pool', |
| 189 | 'google.protobuf.message_factory', |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame^] | 190 | 'google.protobuf.proto_builder', |
David Hirschfeld | ef6eff2 | 2014-08-27 09:25:26 +0100 | [diff] [blame] | 191 | 'google.protobuf.pyext.cpp_message', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 192 | 'google.protobuf.reflection', |
| 193 | 'google.protobuf.service', |
| 194 | 'google.protobuf.service_reflection', |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 195 | 'google.protobuf.symbol_database', |
| 196 | 'google.protobuf.text_encoding', |
| 197 | 'google.protobuf.text_format'], |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 198 | cmdclass = { 'clean': clean, 'build_py': build_py }, |
| 199 | install_requires = ['setuptools'], |
Dwayne Litzenberger | b460610 | 2014-10-14 13:50:00 -0700 | [diff] [blame] | 200 | # TODO: Restore dependency once a Python 3 compatible google-apputils |
| 201 | # is released. |
| 202 | setup_requires = (['google-apputils'] |
| 203 | if sys.version_info[0] < 3 else |
| 204 | []), |
liujisi@google.com | 9b7f6c5 | 2010-12-08 03:45:27 +0000 | [diff] [blame] | 205 | ext_modules = ext_module_list, |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 206 | url = 'https://developers.google.com/protocol-buffers/', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 207 | maintainer = maintainer_email, |
| 208 | maintainer_email = 'protobuf@googlegroups.com', |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 209 | license = 'New BSD License', |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 210 | description = 'Protocol Buffers', |
| 211 | long_description = |
| 212 | "Protocol Buffers are Google's data interchange format.", |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 213 | ) |