1#! /usr/bin/python
2#
3# See README for usage instructions.
4import sys
5import os
6import subprocess
7
8# 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
26maintainer_email = "protobuf@googlegroups.com"
27
28# Find the Protocol Compiler.
29if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
30  protoc = os.environ['PROTOC']
31elif os.path.exists("../src/protoc"):
32  protoc = "../src/protoc"
33elif os.path.exists("../src/protoc.exe"):
34  protoc = "../src/protoc.exe"
35elif 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"
39else:
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
49  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
54    if not os.path.exists(source):
55      sys.stderr.write("Can't find required file: %s\n" % source)
56      sys.exit(-1)
57
58    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
64    protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
65    if subprocess.call(protoc_command) != 0:
66      sys.exit(-1)
67
68def 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/descriptor_pool_test1.proto")
76  generate_proto("google/protobuf/internal/descriptor_pool_test2.proto")
77  generate_proto("google/protobuf/internal/test_bad_identifiers.proto")
78  generate_proto("google/protobuf/internal/missing_enum_values.proto")
79  generate_proto("google/protobuf/internal/more_extensions.proto")
80  generate_proto("google/protobuf/internal/more_extensions_dynamic.proto")
81  generate_proto("google/protobuf/internal/more_messages.proto")
82  generate_proto("google/protobuf/internal/factory_test1.proto")
83  generate_proto("google/protobuf/internal/factory_test2.proto")
84  generate_proto("google/protobuf/pyext/python.proto")
85
86def MakeTestSuite():
87  # Test C++ implementation
88  import unittest
89  import google.protobuf.pyext.descriptor_cpp2_test as descriptor_cpp2_test
90  import google.protobuf.pyext.message_factory_cpp2_test \
91      as message_factory_cpp2_test
92  import google.protobuf.pyext.reflection_cpp2_generated_test \
93      as reflection_cpp2_generated_test
94
95  loader = unittest.defaultTestLoader
96  suite = unittest.TestSuite()
97  for test in [  descriptor_cpp2_test,
98                 message_factory_cpp2_test,
99                 reflection_cpp2_generated_test]:
100    suite.addTest(loader.loadTestsFromModule(test))
101  return suite
102
103class clean(_clean):
104  def run(self):
105    # Delete generated files in the code tree.
106    for (dirpath, dirnames, filenames) in os.walk("."):
107      for filename in filenames:
108        filepath = os.path.join(dirpath, filename)
109        if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
110          filepath.endswith(".so") or filepath.endswith(".o") or \
111          filepath.endswith('google/protobuf/compiler/__init__.py'):
112          os.remove(filepath)
113    # _clean is an old-style class, so super() doesn't work.
114    _clean.run(self)
115
116class build_py(_build_py):
117  def run(self):
118    # Generate necessary .proto file if it doesn't exist.
119    generate_proto("../src/google/protobuf/descriptor.proto")
120    generate_proto("../src/google/protobuf/compiler/plugin.proto")
121    GenerateUnittestProtos()
122
123    # Make sure google.protobuf/** are valid packages.
124    for path in ['', 'internal/', 'compiler/', 'pyext/']:
125      try:
126        open('google/protobuf/%s__init__.py' % path, 'a').close()
127      except EnvironmentError:
128        pass
129    # _build_py is an old-style class, so super() doesn't work.
130    _build_py.run(self)
131  # TODO(mrovner): Subclass to run 2to3 on some files only.
132  # Tracing what https://wiki.python.org/moin/PortingPythonToPy3k's "Approach 2"
133  # section on how to get 2to3 to run on source files during install under
134  # Python 3.  This class seems like a good place to put logic that calls
135  # python3's distutils.util.run_2to3 on the subset of the files we have in our
136  # release that are subject to conversion.
137  # See code reference in previous code review.
138
139if __name__ == '__main__':
140  ext_module_list = []
141  cpp_impl = '--cpp_implementation'
142  if cpp_impl in sys.argv:
143    sys.argv.remove(cpp_impl)
144    # C++ implementation extension
145    ext_module_list.append(Extension(
146        "google.protobuf.pyext._message",
147        [ "google/protobuf/pyext/descriptor.cc",
148          "google/protobuf/pyext/message.cc",
149          "google/protobuf/pyext/extension_dict.cc",
150          "google/protobuf/pyext/repeated_scalar_container.cc",
151          "google/protobuf/pyext/repeated_composite_container.cc" ],
152        define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')],
153        include_dirs = [ ".", "../src"],
154        libraries = [ "protobuf" ],
155        library_dirs = [ '../src/.libs' ],
156        ))
157
158  setup(name = 'protobuf',
159        version = '2.6.1',
160        packages = [ 'google' ],
161        namespace_packages = [ 'google' ],
162        test_suite = 'setup.MakeTestSuite',
163        google_test_dir = "google/protobuf/internal",
164        # Must list modules explicitly so that we don't install tests.
165        py_modules = [
166          'google.protobuf.internal.api_implementation',
167          'google.protobuf.internal.containers',
168          'google.protobuf.internal.cpp_message',
169          'google.protobuf.internal.decoder',
170          'google.protobuf.internal.encoder',
171          'google.protobuf.internal.enum_type_wrapper',
172          'google.protobuf.internal.message_listener',
173          'google.protobuf.internal.python_message',
174          'google.protobuf.internal.type_checkers',
175          'google.protobuf.internal.wire_format',
176          'google.protobuf.descriptor',
177          'google.protobuf.descriptor_pb2',
178          'google.protobuf.compiler.plugin_pb2',
179          'google.protobuf.message',
180          'google.protobuf.descriptor_database',
181          'google.protobuf.descriptor_pool',
182          'google.protobuf.message_factory',
183          'google.protobuf.pyext.cpp_message',
184          'google.protobuf.reflection',
185          'google.protobuf.service',
186          'google.protobuf.service_reflection',
187          'google.protobuf.symbol_database',
188          'google.protobuf.text_encoding',
189          'google.protobuf.text_format'],
190        cmdclass = { 'clean': clean, 'build_py': build_py },
191        install_requires = ['setuptools'],
192        setup_requires = ['google-apputils'],
193        ext_modules = ext_module_list,
194        url = 'https://developers.google.com/protocol-buffers/',
195        maintainer = maintainer_email,
196        maintainer_email = 'protobuf@googlegroups.com',
197        license = 'New BSD License',
198        description = 'Protocol Buffers',
199        long_description =
200          "Protocol Buffers are Google's data interchange format.",
201        )
202