Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2 | # |
| 3 | # See README for usage instructions. |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 4 | import glob |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 5 | import os |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 6 | import subprocess |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 7 | import sys |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 9 | # We must use setuptools, not distutils, because we need to use the |
| 10 | # namespace_packages option for the "google" package. |
| 11 | try: |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 12 | from setuptools import setup, Extension, find_packages |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 13 | except ImportError: |
jieluo@google.com | 7580a89 | 2014-08-12 21:28:31 +0000 | [diff] [blame] | 14 | try: |
| 15 | from ez_setup import use_setuptools |
| 16 | use_setuptools() |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 17 | from setuptools import setup, Extension, find_packages |
jieluo@google.com | 7580a89 | 2014-08-12 21:28:31 +0000 | [diff] [blame] | 18 | except ImportError: |
| 19 | sys.stderr.write( |
| 20 | "Could not import setuptools; make sure you have setuptools or " |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 21 | "ez_setup installed.\n" |
| 22 | ) |
jieluo@google.com | 7580a89 | 2014-08-12 21:28:31 +0000 | [diff] [blame] | 23 | raise |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 24 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 25 | from distutils.command.clean import clean as _clean |
| 26 | |
| 27 | if sys.version_info[0] == 3: |
| 28 | # Python 3 |
| 29 | from distutils.command.build_py import build_py_2to3 as _build_py |
| 30 | else: |
| 31 | # Python 2 |
| 32 | from distutils.command.build_py import build_py as _build_py |
| 33 | from distutils.spawn import find_executable |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 34 | |
| 35 | # Find the Protocol Compiler. |
liujisi@google.com | e34f1f6 | 2012-12-05 01:25:12 +0000 | [diff] [blame] | 36 | if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): |
| 37 | protoc = os.environ['PROTOC'] |
| 38 | elif os.path.exists("../src/protoc"): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | protoc = "../src/protoc" |
kenton@google.com | 4152d55 | 2009-04-22 03:20:21 +0000 | [diff] [blame] | 40 | elif os.path.exists("../src/protoc.exe"): |
| 41 | protoc = "../src/protoc.exe" |
kenton@google.com | 80aa23d | 2010-09-28 21:58:36 +0000 | [diff] [blame] | 42 | elif os.path.exists("../vsprojects/Debug/protoc.exe"): |
| 43 | protoc = "../vsprojects/Debug/protoc.exe" |
| 44 | elif os.path.exists("../vsprojects/Release/protoc.exe"): |
| 45 | protoc = "../vsprojects/Release/protoc.exe" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | else: |
| 47 | protoc = find_executable("protoc") |
| 48 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 49 | |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 50 | def GetVersion(): |
| 51 | """Gets the version from google/protobuf/__init__.py |
| 52 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 53 | Do not import google.protobuf.__init__ directly, because an installed |
| 54 | protobuf library may be loaded instead.""" |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 55 | |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 56 | with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file: |
Behzad Tabibian | 2bf92b3 | 2015-05-07 19:04:56 +0200 | [diff] [blame] | 57 | exec(version_file.read(), globals()) |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 58 | return __version__ |
| 59 | |
| 60 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 61 | def generate_proto(source): |
| 62 | """Invokes the Protocol Compiler to generate a _pb2.py from the given |
| 63 | .proto file. Does nothing if the output already exists and is newer than |
| 64 | the input.""" |
| 65 | |
| 66 | output = source.replace(".proto", "_pb2.py").replace("../src/", "") |
| 67 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 68 | if (not os.path.exists(output) or |
| 69 | (os.path.exists(source) and |
| 70 | os.path.getmtime(source) > os.path.getmtime(output))): |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 71 | print("Generating %s..." % output) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 72 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 73 | if not os.path.exists(source): |
| 74 | sys.stderr.write("Can't find required file: %s\n" % source) |
| 75 | sys.exit(-1) |
| 76 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 77 | if protoc is None: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 78 | sys.stderr.write( |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 79 | "protoc is not installed nor found in ../src. " |
| 80 | "Please compile it or install the binary package.\n" |
| 81 | ) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 82 | sys.exit(-1) |
| 83 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 84 | protoc_command = [protoc, "-I../src", "-I.", "--python_out=.", source] |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 85 | if subprocess.call(protoc_command) != 0: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 86 | sys.exit(-1) |
| 87 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 88 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 89 | def GenerateUnittestProtos(): |
Tamir Duberstein | e4f4d9f | 2015-05-07 07:40:38 -0400 | [diff] [blame] | 90 | # Unittest protos are only needed for development. |
| 91 | if not os.path.exists("../.git"): |
| 92 | return |
| 93 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 94 | generate_proto("../src/google/protobuf/map_unittest.proto") |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 95 | generate_proto("../src/google/protobuf/unittest.proto") |
| 96 | generate_proto("../src/google/protobuf/unittest_custom_options.proto") |
| 97 | generate_proto("../src/google/protobuf/unittest_import.proto") |
| 98 | generate_proto("../src/google/protobuf/unittest_import_public.proto") |
| 99 | generate_proto("../src/google/protobuf/unittest_mset.proto") |
| 100 | generate_proto("../src/google/protobuf/unittest_no_generic_services.proto") |
Jisi Liu | ada6556 | 2015-02-25 16:39:11 -0800 | [diff] [blame] | 101 | generate_proto("../src/google/protobuf/unittest_proto3_arena.proto") |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 102 | generate_proto("google/protobuf/internal/descriptor_pool_test1.proto") |
| 103 | generate_proto("google/protobuf/internal/descriptor_pool_test2.proto") |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 104 | generate_proto("google/protobuf/internal/factory_test1.proto") |
| 105 | generate_proto("google/protobuf/internal/factory_test2.proto") |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 106 | generate_proto("google/protobuf/internal/import_test_package/inner.proto") |
| 107 | generate_proto("google/protobuf/internal/import_test_package/outer.proto") |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 108 | generate_proto("google/protobuf/internal/missing_enum_values.proto") |
| 109 | generate_proto("google/protobuf/internal/more_extensions.proto") |
| 110 | generate_proto("google/protobuf/internal/more_extensions_dynamic.proto") |
| 111 | generate_proto("google/protobuf/internal/more_messages.proto") |
| 112 | generate_proto("google/protobuf/internal/test_bad_identifiers.proto") |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 113 | generate_proto("google/protobuf/pyext/python.proto") |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 114 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 115 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 116 | class clean(_clean): |
| 117 | def run(self): |
| 118 | # Delete generated files in the code tree. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 119 | for (dirpath, dirnames, filenames) in os.walk("."): |
| 120 | for filename in filenames: |
| 121 | filepath = os.path.join(dirpath, filename) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 122 | if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 123 | filepath.endswith(".so") or filepath.endswith(".o") or \ |
| 124 | filepath.endswith('google/protobuf/compiler/__init__.py'): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 125 | os.remove(filepath) |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 126 | # _clean is an old-style class, so super() doesn't work. |
| 127 | _clean.run(self) |
| 128 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 129 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 130 | class build_py(_build_py): |
| 131 | def run(self): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 132 | # Generate necessary .proto file if it doesn't exist. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 133 | generate_proto("../src/google/protobuf/descriptor.proto") |
liujisi@google.com | 9b7f6c5 | 2010-12-08 03:45:27 +0000 | [diff] [blame] | 134 | generate_proto("../src/google/protobuf/compiler/plugin.proto") |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 135 | GenerateUnittestProtos() |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 136 | |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 137 | # Make sure google.protobuf/** are valid packages. |
| 138 | for path in ['', 'internal/', 'compiler/', 'pyext/']: |
| 139 | try: |
| 140 | open('google/protobuf/%s__init__.py' % path, 'a').close() |
| 141 | except EnvironmentError: |
| 142 | pass |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 143 | # _build_py is an old-style class, so super() doesn't work. |
| 144 | _build_py.run(self) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 145 | # TODO(mrovner): Subclass to run 2to3 on some files only. |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 146 | # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's |
| 147 | # "Approach 2" section on how to get 2to3 to run on source files during |
| 148 | # install under Python 3. This class seems like a good place to put logic |
| 149 | # that calls python3's distutils.util.run_2to3 on the subset of the files we |
| 150 | # have in our release that are subject to conversion. |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 151 | # See code reference in previous code review. |
| 152 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 153 | if __name__ == '__main__': |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 154 | ext_module_list = [] |
jieluo@google.com | a21bf2e | 2014-08-25 23:26:40 +0000 | [diff] [blame] | 155 | cpp_impl = '--cpp_implementation' |
| 156 | if cpp_impl in sys.argv: |
jieluo@google.com | b70e586 | 2014-08-13 21:05:19 +0000 | [diff] [blame] | 157 | sys.argv.remove(cpp_impl) |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 158 | # C++ implementation extension |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 159 | ext_module_list.append( |
| 160 | Extension( |
| 161 | "google.protobuf.pyext._message", |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 162 | glob.glob('google/protobuf/pyext/*.cc'), |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 163 | define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')], |
| 164 | include_dirs=[".", "../src"], |
| 165 | libraries=['protobuf'], |
| 166 | library_dirs=['../src/.libs'], |
| 167 | ) |
| 168 | ) |
Jisi Liu | ada6556 | 2015-02-25 16:39:11 -0800 | [diff] [blame] | 169 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 170 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 171 | setup( |
| 172 | name='protobuf', |
| 173 | version=GetVersion(), |
| 174 | description='Protocol Buffers', |
| 175 | long_description="Protocol Buffers are Google's data interchange format", |
| 176 | url='https://developers.google.com/protocol-buffers/', |
| 177 | maintainer='protobuf@googlegroups.com', |
| 178 | maintainer_email='protobuf@googlegroups.com', |
| 179 | license='New BSD License', |
| 180 | classifiers=[ |
| 181 | 'Programming Language :: Python :: 2.7', |
| 182 | ], |
| 183 | namespace_packages=['google'], |
| 184 | packages=find_packages( |
| 185 | exclude=[ |
| 186 | 'import_test_package', |
| 187 | ], |
| 188 | ), |
| 189 | test_suite='google.protobuf.internal', |
| 190 | cmdclass={ |
| 191 | 'clean': clean, |
| 192 | 'build_py': build_py, |
| 193 | }, |
| 194 | install_requires=['setuptools'], |
| 195 | ext_modules=ext_module_list, |
| 196 | ) |