From 2dd9bc7159b4d37c155d5213e3532781923367ff Mon Sep 17 00:00:00 2001 From: liuwei1031 <46661762+liuwei1031@users.noreply.github.com> Date: Sun, 3 Nov 2019 12:46:08 +0800 Subject: [PATCH] fix windows python setup issue caused by (#20641) (#20928) * fix windows python setup issue caused by #20641, test=develop * tweak python/CMakeLists.txt, test=develop * redirect log inside setup.py, test=develop * fix typo, test=develop --- python/CMakeLists.txt | 27 +++++++++--------- python/setup.py.in | 66 +++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 62fd81aa54c..77a75280049 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -10,6 +10,8 @@ else() SET(PACKAGE_NAME "paddlepaddle") endif() +set(SETUP_LOG_FILE "setup.py.log") + set(FLUID_CORE_NAME "core") if(WITH_AVX AND AVX_FOUND) set(FLUID_CORE_NAME "${FLUID_CORE_NAME}_avx") @@ -70,21 +72,18 @@ add_custom_command(OUTPUT ${FLUID_CORE} add_custom_target(copy_paddle_pybind ALL DEPENDS ${FLUID_CORE_DEPS}) IF(WIN32) - add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp - COMMAND ${CMAKE_COMMAND} -E copy_directory ${PADDLE_SOURCE_DIR}/python/paddle ${PADDLE_BINARY_DIR}/python/paddle/ - COMMAND ${CMAKE_COMMAND} -E env ${py_env} ${PYTHON_EXECUTABLE} setup.py bdist_wheel - COMMAND ${CMAKE_COMMAND} -E touch ${PADDLE_PYTHON_BUILD_DIR}/.timestamp - COMMAND ${CMAKE_COMMAND} -E remove_directory ${PADDLE_PYTHON_BUILD_DIR}/lib-python - DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto profiler_py_proto ${PY_FILES}) + add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PADDLE_SOURCE_DIR}/python/paddle ${PADDLE_BINARY_DIR}/python/paddle/ + COMMAND ${CMAKE_COMMAND} -E env ${py_env} ${PYTHON_EXECUTABLE} setup.py bdist_wheel + COMMAND ${CMAKE_COMMAND} -E touch ${PADDLE_PYTHON_BUILD_DIR}/.timestamp + DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto profiler_py_proto ${PY_FILES}) ELSE(WIN32) - add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp - COMMAND touch stub.cc - COMMAND cp -r ${PADDLE_SOURCE_DIR}/python/paddle ${PADDLE_BINARY_DIR}/python - COMMAND env ${py_env} ${PYTHON_EXECUTABLE} setup.py bdist_wheel - COMMAND ${CMAKE_COMMAND} -E touch ${PADDLE_PYTHON_BUILD_DIR}/.timestamp - COMMAND ${CMAKE_COMMAND} -E remove_directory ${PADDLE_PYTHON_BUILD_DIR}/lib-python - COMMAND ${CMAKE_COMMAND} -E copy_directory ${PADDLE_PYTHON_BUILD_DIR}/lib* ${PADDLE_PYTHON_BUILD_DIR}/lib-python - DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto profiler_py_proto ${PY_FILES}) + add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp + COMMAND touch stub.cc + COMMAND cp -r ${PADDLE_SOURCE_DIR}/python/paddle ${PADDLE_BINARY_DIR}/python + COMMAND env ${py_env} ${PYTHON_EXECUTABLE} setup.py bdist_wheel + COMMAND ${CMAKE_COMMAND} -E touch ${PADDLE_PYTHON_BUILD_DIR}/.timestamp + DEPENDS copy_paddle_pybind ${FLUID_CORE} framework_py_proto profiler_py_proto ${PY_FILES}) ENDIF() add_custom_target(paddle_python ALL DEPENDS ${PADDLE_PYTHON_BUILD_DIR}/.timestamp) diff --git a/python/setup.py.in b/python/setup.py.in index 8302964c70d..61708c50b34 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -1,10 +1,12 @@ import subprocess import os +import os.path import re import shutil import sys import fnmatch +from contextlib import contextmanager from setuptools import Command from setuptools import setup, Distribution, Extension from setuptools.command.install import install as InstallCommandBase @@ -358,36 +360,40 @@ class InstallHeaders(Command): def get_outputs(self): return self.outfiles -# Saving the installation log generated from setup.py to log_file. -# The log_file is ${PADDLE_BINARY_DIR}/python/setup.py.log. -stdout = sys.stdout -stderr = sys.stderr -log_file = open('setup.py.log', 'w') -sys.stdout = log_file -sys.stderr = log_file - -setup(name='${PACKAGE_NAME}', - version='${PADDLE_VERSION}', - description='Parallel Distributed Deep Learning', - install_requires=setup_requires, - packages=packages, - ext_modules=ext_modules, - package_data=package_data, - package_dir=package_dir, - scripts=paddle_bins, - distclass=BinaryDistribution, - headers=headers, - cmdclass={ - 'install_headers': InstallHeaders, - 'install': InstallCommand, - } -) - -log_file.close() -# Revert back the stdout/stderr to their default references. -sys.stdout = stdout -sys.stderr = stderr +# we redirect setuptools log for non-windows +if sys.platform != 'win32': + @contextmanager + def redirect_stdout(): + with open('${SETUP_LOG_FILE}', 'w') as f: + origin_stdout = sys.stdout + sys.stdout = f + yield + sys.stdout = origin_stdout +else: + @contextmanager + def redirect_stdout(): + yield + +with redirect_stdout(): + setup(name='${PACKAGE_NAME}', + version='${PADDLE_VERSION}', + description='Parallel Distributed Deep Learning', + install_requires=setup_requires, + packages=packages, + ext_modules=ext_modules, + package_data=package_data, + package_dir=package_dir, + scripts=paddle_bins, + distclass=BinaryDistribution, + headers=headers, + cmdclass={ + 'install_headers': InstallHeaders, + 'install': InstallCommand, + } + ) + # As there are a lot of files in purelib which causes many logs, # we don't print them on the screen, and you can open `setup.py.log` # for the full logs. -os.system('grep -v "purelib" setup.py.log') +if os.path.exists('${SETUP_LOG_FILE}'): + os.system('grep -v "purelib" ${SETUP_LOG_FILE}') -- GitLab