Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: third_party/protobuf/python/setup.py

Issue 1322483002: Revert https://codereview.chromium.org/1291903002 (protobuf roll). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/protobuf/python/mox.py ('k') | third_party/protobuf/ruby/.gitignore » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #! /usr/bin/env python 1 #! /usr/bin/python
2 # 2 #
3 # See README for usage instructions. 3 # See README for usage instructions.
4 import glob 4 import sys
5 import os 5 import os
6 import subprocess 6 import subprocess
7 import sys
8 7
9 # We must use setuptools, not distutils, because we need to use the 8 # We must use setuptools, not distutils, because we need to use the
10 # namespace_packages option for the "google" package. 9 # namespace_packages option for the "google" package.
11 try: 10 try:
12 from setuptools import setup, Extension, find_packages 11 from setuptools import setup, Extension
13 except ImportError: 12 except ImportError:
14 try: 13 try:
15 from ez_setup import use_setuptools 14 from ez_setup import use_setuptools
16 use_setuptools() 15 use_setuptools()
17 from setuptools import setup, Extension, find_packages 16 from setuptools import setup, Extension
18 except ImportError: 17 except ImportError:
19 sys.stderr.write( 18 sys.stderr.write(
20 "Could not import setuptools; make sure you have setuptools or " 19 "Could not import setuptools; make sure you have setuptools or "
21 "ez_setup installed.\n" 20 "ez_setup installed.\n")
22 )
23 raise 21 raise
22 from distutils.command.clean import clean as _clean
23 from distutils.command.build_py import build_py as _build_py
24 from distutils.spawn import find_executable
24 25
25 from distutils.command.clean import clean as _clean 26 maintainer_email = "protobuf@googlegroups.com"
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
34 27
35 # Find the Protocol Compiler. 28 # Find the Protocol Compiler.
36 if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): 29 if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
37 protoc = os.environ['PROTOC'] 30 protoc = os.environ['PROTOC']
38 elif os.path.exists("../src/protoc"): 31 elif os.path.exists("../src/protoc"):
39 protoc = "../src/protoc" 32 protoc = "../src/protoc"
40 elif os.path.exists("../src/protoc.exe"): 33 elif os.path.exists("../src/protoc.exe"):
41 protoc = "../src/protoc.exe" 34 protoc = "../src/protoc.exe"
42 elif os.path.exists("../vsprojects/Debug/protoc.exe"): 35 elif os.path.exists("../vsprojects/Debug/protoc.exe"):
43 protoc = "../vsprojects/Debug/protoc.exe" 36 protoc = "../vsprojects/Debug/protoc.exe"
44 elif os.path.exists("../vsprojects/Release/protoc.exe"): 37 elif os.path.exists("../vsprojects/Release/protoc.exe"):
45 protoc = "../vsprojects/Release/protoc.exe" 38 protoc = "../vsprojects/Release/protoc.exe"
46 else: 39 else:
47 protoc = find_executable("protoc") 40 protoc = find_executable("protoc")
48 41
49 42 def generate_proto(source):
50 def GetVersion():
51 """Gets the version from google/protobuf/__init__.py
52
53 Do not import google.protobuf.__init__ directly, because an installed
54 protobuf library may be loaded instead."""
55
56 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
57 exec(version_file.read(), globals())
58 return __version__
59
60
61 def generate_proto(source, require = True):
62 """Invokes the Protocol Compiler to generate a _pb2.py from the given 43 """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 44 .proto file. Does nothing if the output already exists and is newer than
64 the input.""" 45 the input."""
65 46
66 if not require and not os.path.exists(source):
67 return
68
69 output = source.replace(".proto", "_pb2.py").replace("../src/", "") 47 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
70 48
71 if (not os.path.exists(output) or 49 if (not os.path.exists(output) or
72 (os.path.exists(source) and 50 (os.path.exists(source) and
73 os.path.getmtime(source) > os.path.getmtime(output))): 51 os.path.getmtime(source) > os.path.getmtime(output))):
74 print("Generating %s..." % output) 52 print "Generating %s..." % output
75 53
76 if not os.path.exists(source): 54 if not os.path.exists(source):
77 sys.stderr.write("Can't find required file: %s\n" % source) 55 sys.stderr.write("Can't find required file: %s\n" % source)
78 sys.exit(-1) 56 sys.exit(-1)
79 57
80 if protoc is None: 58 if protoc == None:
81 sys.stderr.write( 59 sys.stderr.write(
82 "protoc is not installed nor found in ../src. " 60 "protoc is not installed nor found in ../src. Please compile it "
83 "Please compile it or install the binary package.\n" 61 "or install the binary package.\n")
84 )
85 sys.exit(-1) 62 sys.exit(-1)
86 63
87 protoc_command = [protoc, "-I../src", "-I.", "--python_out=.", source] 64 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
88 if subprocess.call(protoc_command) != 0: 65 if subprocess.call(protoc_command) != 0:
89 sys.exit(-1) 66 sys.exit(-1)
90 67
68 def 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")
91 81
92 def GenerateUnittestProtos(): 82 def MakeTestSuite():
93 generate_proto("../src/google/protobuf/map_unittest.proto", False) 83 # This is apparently needed on some systems to make sure that the tests
94 generate_proto("../src/google/protobuf/unittest.proto", False) 84 # work even if a previous version is already installed.
95 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False) 85 if 'google' in sys.modules:
96 generate_proto("../src/google/protobuf/unittest_import.proto", False) 86 del sys.modules['google']
97 generate_proto("../src/google/protobuf/unittest_import_public.proto", False) 87 GenerateUnittestProtos()
98 generate_proto("../src/google/protobuf/unittest_mset.proto", False) 88
99 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", Fa lse) 89 import unittest
100 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False) 90 import google.protobuf.internal.generator_test as generator_test
101 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False) 91 import google.protobuf.internal.descriptor_test as descriptor_test
102 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False) 92 import google.protobuf.internal.reflection_test as reflection_test
103 generate_proto("google/protobuf/internal/factory_test1.proto", False) 93 import google.protobuf.internal.service_reflection_test \
104 generate_proto("google/protobuf/internal/factory_test2.proto", False) 94 as service_reflection_test
105 generate_proto("google/protobuf/internal/import_test_package/inner.proto", Fal se) 95 import google.protobuf.internal.text_format_test as text_format_test
106 generate_proto("google/protobuf/internal/import_test_package/outer.proto", Fal se) 96 import google.protobuf.internal.wire_format_test as wire_format_test
107 generate_proto("google/protobuf/internal/missing_enum_values.proto", False) 97 import google.protobuf.internal.unknown_fields_test as unknown_fields_test
108 generate_proto("google/protobuf/internal/more_extensions.proto", False) 98 import google.protobuf.internal.descriptor_database_test \
109 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False ) 99 as descriptor_database_test
110 generate_proto("google/protobuf/internal/more_messages.proto", False) 100 import google.protobuf.internal.descriptor_pool_test as descriptor_pool_test
111 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False) 101 import google.protobuf.internal.message_factory_test as message_factory_test
112 generate_proto("google/protobuf/pyext/python.proto", False) 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
105
106 loader = unittest.defaultTestLoader
107 suite = unittest.TestSuite()
108 for test in [ generator_test,
109 descriptor_test,
110 reflection_test,
111 service_reflection_test,
112 text_format_test,
113 wire_format_test ]:
114 suite.addTest(loader.loadTestsFromModule(test))
115
116 return suite
113 117
114 118
115 class clean(_clean): 119 class clean(_clean):
116 def run(self): 120 def run(self):
117 # Delete generated files in the code tree. 121 # Delete generated files in the code tree.
118 for (dirpath, dirnames, filenames) in os.walk("."): 122 for (dirpath, dirnames, filenames) in os.walk("."):
119 for filename in filenames: 123 for filename in filenames:
120 filepath = os.path.join(dirpath, filename) 124 filepath = os.path.join(dirpath, filename)
121 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ 125 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
122 filepath.endswith(".so") or filepath.endswith(".o") or \ 126 filepath.endswith(".so") or filepath.endswith(".o") or \
123 filepath.endswith('google/protobuf/compiler/__init__.py'): 127 filepath.endswith('google/protobuf/compiler/__init__.py'):
124 os.remove(filepath) 128 os.remove(filepath)
125 # _clean is an old-style class, so super() doesn't work. 129 # _clean is an old-style class, so super() doesn't work.
126 _clean.run(self) 130 _clean.run(self)
127 131
128
129 class build_py(_build_py): 132 class build_py(_build_py):
130 def run(self): 133 def run(self):
131 # Generate necessary .proto file if it doesn't exist. 134 # Generate necessary .proto file if it doesn't exist.
132 generate_proto("../src/google/protobuf/descriptor.proto") 135 generate_proto("../src/google/protobuf/descriptor.proto")
133 generate_proto("../src/google/protobuf/compiler/plugin.proto") 136 generate_proto("../src/google/protobuf/compiler/plugin.proto")
137
134 GenerateUnittestProtos() 138 GenerateUnittestProtos()
135 139 # Make sure google.protobuf.compiler is a valid package.
136 # Make sure google.protobuf/** are valid packages. 140 open('google/protobuf/compiler/__init__.py', 'a').close()
137 for path in ['', 'internal/', 'compiler/', 'pyext/']:
138 try:
139 open('google/protobuf/%s__init__.py' % path, 'a').close()
140 except EnvironmentError:
141 pass
142 # _build_py is an old-style class, so super() doesn't work. 141 # _build_py is an old-style class, so super() doesn't work.
143 _build_py.run(self) 142 _build_py.run(self)
144 # TODO(mrovner): Subclass to run 2to3 on some files only.
145 # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's
146 # "Approach 2" section on how to get 2to3 to run on source files during
147 # install under Python 3. This class seems like a good place to put logic
148 # that calls python3's distutils.util.run_2to3 on the subset of the files we
149 # have in our release that are subject to conversion.
150 # See code reference in previous code review.
151 143
152 if __name__ == '__main__': 144 if __name__ == '__main__':
153 ext_module_list = [] 145 ext_module_list = []
154 cpp_impl = '--cpp_implementation' 146
155 if cpp_impl in sys.argv: 147 # C++ implementation extension
156 sys.argv.remove(cpp_impl) 148 if os.getenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python") == "cpp":
157 # C++ implementation extension 149 print "Using EXPERIMENTAL C++ Implmenetation."
158 ext_module_list.append( 150 ext_module_list.append(Extension(
159 Extension( 151 "google.protobuf.internal._net_proto2___python",
160 "google.protobuf.pyext._message", 152 [ "google/protobuf/pyext/python_descriptor.cc",
161 glob.glob('google/protobuf/pyext/*.cc'), 153 "google/protobuf/pyext/python_protobuf.cc",
162 define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')], 154 "google/protobuf/pyext/python-proto2.cc" ],
163 include_dirs=[".", "../src"], 155 include_dirs = [ "." ],
164 libraries=['protobuf'], 156 libraries = [ "protobuf" ]))
165 library_dirs=['../src/.libs'], 157
158 setup(name = 'protobuf',
159 version = '2.5.0-pre',
160 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 = [
165 'google.protobuf.internal.api_implementation',
166 'google.protobuf.internal.containers',
167 'google.protobuf.internal.cpp_message',
168 'google.protobuf.internal.decoder',
169 'google.protobuf.internal.encoder',
170 'google.protobuf.internal.enum_type_wrapper',
171 'google.protobuf.internal.message_listener',
172 'google.protobuf.internal.python_message',
173 'google.protobuf.internal.type_checkers',
174 'google.protobuf.internal.wire_format',
175 'google.protobuf.descriptor',
176 'google.protobuf.descriptor_pb2',
177 'google.protobuf.compiler.plugin_pb2',
178 'google.protobuf.message',
179 'google.protobuf.descriptor_database',
180 'google.protobuf.descriptor_pool',
181 'google.protobuf.message_factory',
182 'google.protobuf.reflection',
183 'google.protobuf.service',
184 'google.protobuf.service_reflection',
185 'google.protobuf.text_format' ],
186 cmdclass = { 'clean': clean, 'build_py': build_py },
187 install_requires = ['setuptools'],
188 ext_modules = ext_module_list,
189 url = 'http://code.google.com/p/protobuf/',
190 maintainer = maintainer_email,
191 maintainer_email = 'protobuf@googlegroups.com',
192 license = 'New BSD License',
193 description = 'Protocol Buffers',
194 long_description =
195 "Protocol Buffers are Google's data interchange format.",
166 ) 196 )
167 )
168 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
169
170 setup(
171 name='protobuf',
172 version=GetVersion(),
173 description='Protocol Buffers',
174 long_description="Protocol Buffers are Google's data interchange format",
175 url='https://developers.google.com/protocol-buffers/',
176 maintainer='protobuf@googlegroups.com',
177 maintainer_email='protobuf@googlegroups.com',
178 license='New BSD License',
179 classifiers=[
180 'Programming Language :: Python :: 2.7',
181 ],
182 namespace_packages=['google'],
183 packages=find_packages(
184 exclude=[
185 'import_test_package',
186 ],
187 ),
188 test_suite='google.protobuf.internal',
189 cmdclass={
190 'clean': clean,
191 'build_py': build_py,
192 },
193 install_requires=['setuptools'],
194 ext_modules=ext_module_list,
195 )
OLDNEW
« no previous file with comments | « third_party/protobuf/python/mox.py ('k') | third_party/protobuf/ruby/.gitignore » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698