tensorflowV1.11-源码分析(2)

通过前面的run_shell函数,运行python脚本,并返回python库的路径。
def get_python_path(environ_cp, python_bin_path):
  """Get the python site package paths."""
  python_paths = []
  if environ_cp.get('PYTHONPATH'):
    python_paths = environ_cp.get('PYTHONPATH').split(':')
  try:
    library_paths = run_shell(
        [python_bin_path, '-c',
         'import site; print("\\n".join(site.getsitepackages()))']).split('\n')
  except subprocess.CalledProcessError:
    library_paths = [run_shell(
        [python_bin_path, '-c',
         'from distutils.sysconfig import get_python_lib;'
         'print(get_python_lib())'])]

  all_paths = set(python_paths + library_paths)
将python库路径放入列表paths中,并返回
  paths = []
  for path in all_paths:
    if os.path.isdir(path):
      paths.append(path)
  return paths

​
通过运行python脚本得到python主版本号
def get_python_major_version(python_bin_path):
  """Get the python major version."""
  return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])'])

​
python设置
def setup_python(environ_cp):
  """Setup python related env variables."""
  # Get PYTHON_BIN_PATH, default is the current running python.
    找到python解释器路径,
    sys.executable  python可执行文件路径
  default_python_bin_path = sys.executable
    允许客户定义python程序运行位置
  ask_python_bin_path = ('Please specify the location of python. [Default is '
                         '%s]: ') % default_python_bin_path

  while True:
    python_bin_path = get_from_env_or_user_or_default(
        environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path,
        default_python_bin_path)
                检测路径是否有效
    # Check if the path is valid
    if os.path.isfile(python_bin_path) and os.access(
        python_bin_path, os.X_OK):
      break
    elif not os.path.exists(python_bin_path):
      print('Invalid python path: %s cannot be found.' % python_bin_path)
    else:
      print('%s is not executable.  Is it the python binary?' % python_bin_path)
    environ_cp['PYTHON_BIN_PATH'] = ''

  # Convert python path to Windows style before checking lib and version
  if is_windows() or is_cygwin():
    python_bin_path = cygpath(python_bin_path)

找到python库路径 
  # Get PYTHON_LIB_PATH
  python_lib_path = environ_cp.get('PYTHON_LIB_PATH')
  if not python_lib_path:
    python_lib_paths = get_python_path(environ_cp, python_bin_path)
    if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1':
      python_lib_path = python_lib_paths[0]
    else:
      print('Found possible Python library paths:\n  %s' %
            '\n  '.join(python_lib_paths))
      default_python_lib_path = python_lib_paths[0]
      python_lib_path = get_input(
          'Please input the desired Python library path to use.  '
          'Default is [%s]\n' % python_lib_paths[0])
      if not python_lib_path:
        python_lib_path = default_python_lib_path
    environ_cp['PYTHON_LIB_PATH'] = python_lib_path

  python_major_version = get_python_major_version(python_bin_path)

  # Convert python path to Windows style before writing into bazel.rc
  if is_windows() or is_cygwin():
    python_lib_path = cygpath(python_lib_path)

  # Set-up env variables used by python_configure.bzl
  write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path)
  write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path)
  write_to_bazelrc('build --python_path=\"%s"' % python_bin_path)
  environ_cp['PYTHON_BIN_PATH'] = python_bin_path

  # Write tools/python_bin_path.sh
  with open(os.path.join(
      _TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), 'w') as f:
    f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)

        重置配置文件
def reset_tf_configure_bazelrc(workspace_path):
  """Reset file that contains customized config settings."""
  open(_TF_BAZELRC, 'w').close()
  bazelrc_path = os.path.join(workspace_path, '.bazelrc')
​
  data = []
  if os.path.exists(bazelrc_path):
    with open(bazelrc_path, 'r') as f:
      data = f.read().splitlines()
  with open(bazelrc_path, 'w') as f:
    for l in data:
      if _TF_BAZELRC_FILENAME in l:
        continue
      f.write('%s\n' % l)
    if is_windows():
      tf_bazelrc_path = _TF_BAZELRC.replace("\\", "/")
    else:
      tf_bazelrc_path = _TF_BAZELRC
    f.write('import %s\n' % tf_bazelrc_path)
​
​
清除makefile
def cleanup_makefile():
  """Delete any leftover BUILD files from the Makefile build.
​
  These files could interfere with Bazel parsing.
  """
  makefile_download_dir = os.path.join(
      _TF_WORKSPACE_ROOT, 'tensorflow', 'contrib', 'makefile', 'downloads')
  if os.path.isdir(makefile_download_dir):
    for root, _, filenames in os.walk(makefile_download_dir):
      for f in filenames:
        if f.endswith('BUILD'):
          os.remove(os.path.join(root, f))

猜你喜欢

转载自blog.51cto.com/13959448/2333561