13
13
# limitations under the License.
14
14
"""Package Setup script for TensorFlow Data Validation."""
15
15
16
+ # pylint:disable=g-bad-import-order
17
+ # setuptools must be imported prior to distutils.
18
+ import setuptools
19
+ from distutils import spawn
20
+ from distutils .command import build
21
+ # pylint:enable=g-bad-import-order
22
+ import os
23
+ import platform
24
+ import subprocess
25
+
16
26
from setuptools import find_packages
17
27
from setuptools import setup
18
28
from setuptools .command .install import install
19
29
from setuptools .dist import Distribution
20
30
21
31
32
+ class _BuildCommand (build .build ):
33
+ """Build everything that is needed to install.
34
+
35
+ This overrides the original distutils "build" command to to run bazel_build
36
+ command before any sub_commands.
37
+
38
+ build command is also invoked from bdist_wheel and install command, therefore
39
+ this implementation covers the following commands:
40
+ - pip install . (which invokes bdist_wheel)
41
+ - python setup.py install (which invokes install command)
42
+ - python setup.py bdist_wheel (which invokes bdist_wheel command)
43
+ """
44
+
45
+ def _build_cc_extensions (self ):
46
+ return True
47
+
48
+ # Add "bazel_build" command as the first sub_command of "build". Each
49
+ # sub_command of "build" (e.g. "build_py", "build_ext", etc.) is executed
50
+ # sequentially when running a "build" command, if the second item in the tuple
51
+ # (predicate method) is evaluated to true.
52
+ sub_commands = [
53
+ ('bazel_build' , _build_cc_extensions ),
54
+ ] + build .build .sub_commands
55
+
56
+
57
+ class _BazelBuildCommand (setuptools .Command ):
58
+ """Build TFDV C++ extensions and public protos with Bazel.
59
+
60
+ Running this command will populate foo_pb2.py file next to your foo.proto
61
+ file.
62
+ """
63
+
64
+ def initialize_options (self ):
65
+ pass
66
+
67
+ def finalize_options (self ):
68
+ self ._bazel_cmd = spawn .find_executable ('bazel' )
69
+ if not self ._bazel_cmd :
70
+ raise RuntimeError (
71
+ 'Could not find "bazel" binary. Please visit '
72
+ 'https://docs.bazel.build/versions/master/install.html for '
73
+ 'installation instruction.' )
74
+ self ._additional_build_options = []
75
+ if platform .system () == 'Darwin' :
76
+ self ._additional_build_options = ['--macos_minimum_os=10.9' ]
77
+ elif platform .system () == 'Windows' :
78
+ self ._additional_build_options = ['--copt=-DWIN32_LEAN_AND_MEAN' ]
79
+
80
+ def run (self ):
81
+ subprocess .check_call (
82
+ [self ._bazel_cmd , 'run' , '-c' , 'opt' ] + self ._additional_build_options +
83
+ ['//tensorflow_data_validation:move_generated_files' ],
84
+ # Bazel should be invoked in a directory containing bazel WORKSPACE
85
+ # file, which is the root directory.
86
+ cwd = os .path .dirname (os .path .realpath (__file__ )),
87
+ )
88
+
89
+
22
90
# TFDV is not a purelib. However because of the extension module is not built
23
91
# by setuptools, it will be incorrectly treated as a purelib. The following
24
92
# works around that bug.
25
- class _InstallPlatlib (install ):
93
+ class _InstallPlatlibCommand (install ):
26
94
27
95
def finalize_options (self ):
28
96
install .finalize_options (self )
@@ -57,7 +125,7 @@ def _make_all_extra_requirements():
57
125
# Get version from version module.
58
126
with open ('tensorflow_data_validation/version.py' ) as fp :
59
127
globals_dict = {}
60
- exec (fp .read (), globals_dict ) # pylint: disable=exec-used
128
+ exec (fp .read (), globals_dict ) # pylint: disable=exec-used
61
129
__version__ = globals_dict ['__version__' ]
62
130
63
131
# Get the long description from the README file.
@@ -81,7 +149,6 @@ def _make_all_extra_requirements():
81
149
'Operating System :: Microsoft :: Windows' ,
82
150
'Programming Language :: Python' ,
83
151
'Programming Language :: Python :: 3' ,
84
- 'Programming Language :: Python :: 3.5' ,
85
152
'Programming Language :: Python :: 3.6' ,
86
153
'Programming Language :: Python :: 3.7' ,
87
154
'Programming Language :: Python :: 3 :: Only' ,
@@ -129,5 +196,9 @@ def _make_all_extra_requirements():
129
196
url = 'https://www.tensorflow.org/tfx/data_validation' ,
130
197
download_url = 'https://github.com/tensorflow/data-validation/tags' ,
131
198
requires = [],
132
- cmdclass = {'install' : _InstallPlatlib })
199
+ cmdclass = {
200
+ 'install' : _InstallPlatlibCommand ,
201
+ 'build' : _BuildCommand ,
202
+ 'bazel_build' : _BazelBuildCommand ,
203
+ })
133
204
0 commit comments