1616import os
1717import re
1818import runpy
19- import subprocess
2019import uuid
2120
2221# ===== Template substitutions start =====
2322# We just put them in one place so its easy to tell which are used.
2423
2524# Runfiles-relative path to the main Python source file.
2625MAIN = "%main%"
27- # Runfiles-relative path to the coverage tool entry point, if any.
28- COVERAGE_TOOL = "%coverage_tool%"
2926
3027# ===== Template substitutions end =====
3128
@@ -114,45 +111,6 @@ def is_verbose_coverage():
114111 return os .environ .get ("VERBOSE_COVERAGE" ) or is_verbose ()
115112
116113
117- ##def find_coverage_entry_point(module_space):
118- ## cov_tool = COVERAGE_TOOL
119- ## if cov_tool:
120- ## print_verbose_coverage("Using toolchain coverage_tool %r" % cov_tool)
121- ## else:
122- ## cov_tool = os.environ.get("PYTHON_COVERAGE")
123- ## if cov_tool:
124- ## print_verbose_coverage("PYTHON_COVERAGE: %r" % cov_tool)
125- ## if cov_tool:
126- ## return find_binary(module_space, cov_tool)
127- ## return None
128-
129-
130- def find_binary (module_space , bin_name ):
131- """Finds the real binary if it's not a normal absolute path."""
132- if not bin_name :
133- return None
134- if bin_name .startswith ("//" ):
135- # Case 1: Path is a label. Not supported yet.
136- raise AssertionError (
137- "Bazel does not support execution of Python interpreters via labels yet"
138- )
139- elif os .path .isabs (bin_name ):
140- # Case 2: Absolute path.
141- return bin_name
142- # Use normpath() to convert slashes to os.sep on Windows.
143- elif os .sep in os .path .normpath (bin_name ):
144- # Case 3: Path is relative to the repo root.
145- return os .path .join (module_space , bin_name )
146- else :
147- # Case 4: Path has to be looked up in the search path.
148- return search_path (bin_name )
149-
150-
151- ##def create_python_path_entries(python_imports, module_space):
152- ## parts = python_imports.split(":")
153- ## return [module_space] + ["%s/%s" % (module_space, path) for path in parts]
154-
155-
156114def find_runfiles_root (main_rel_path ):
157115 """Finds the runfiles tree."""
158116 # When the calling process used the runfiles manifest to resolve the
@@ -197,15 +155,6 @@ def find_runfiles_root(main_rel_path):
197155 raise AssertionError ("Cannot find .runfiles directory for %s" % sys .argv [0 ])
198156
199157
200- # Returns repository roots to add to the import path.
201- ##def get_repositories_imports(module_space, import_all):
202- ## if import_all:
203- ## repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)]
204- ## repo_dirs.sort()
205- ## return [d for d in repo_dirs if os.path.isdir(d)]
206- ## return [os.path.join(module_space, WORKSPACE_NAME)]
207-
208-
209158def runfiles_envvar (module_space ):
210159 """Finds the runfiles manifest or the runfiles directory.
211160
@@ -245,15 +194,6 @@ def runfiles_envvar(module_space):
245194 return (None , None )
246195
247196
248- def deduplicate (items ):
249- """Efficiently filter out duplicates, keeping the first element only."""
250- seen = set ()
251- for it in items :
252- if it not in seen :
253- seen .add (it )
254- yield it
255-
256-
257197def instrumented_file_paths ():
258198 """Yields tuples of realpath of each instrumented file with the relative path."""
259199 manifest_filename = os .environ .get ("COVERAGE_MANIFEST" )
@@ -424,29 +364,12 @@ def main():
424364 #
425365 # To replicate this behavior, we add main's directory within the runfiles
426366 # when safe path isn't enabled.
427- python_path_entries = []
428- prepend_path_entries = []
429367 if not getattr (sys .flags , "safe_path" , False ):
430368 prepend_path_entries = [
431369 os .path .join (module_space , os .path .dirname (main_rel_path ))
432370 ]
433371 else :
434372 prepend_path_entries = []
435- ##python_path_entries = create_python_path_entries(IMPORTS_STR, module_space)
436- ##python_path_entries += get_repositories_imports(module_space, IMPORT_ALL)
437- python_path_entries = [
438- get_windows_path_with_unc_prefix (d ) for d in python_path_entries
439- ]
440-
441- # Remove duplicates to avoid overly long PYTHONPATH (#10977). Preserve order,
442- # keep first occurrence only.
443- python_path_entries = list (deduplicate (python_path_entries ))
444-
445- ##if is_windows():
446- ## python_path_entries = [p.replace("/", os.sep) for p in python_path_entries]
447- ##else:
448- ## # deduplicate returns a generator, but we need a list after this.
449- ## python_path_entries = list(python_path_entries)
450373
451374 runfiles_envkey , runfiles_envvalue = runfiles_envvar (module_space )
452375 if runfiles_envkey :
@@ -463,28 +386,12 @@ def main():
463386
464387 sys .stdout .flush ()
465388
466- # Add the user imports after the stdlib, but before the runtime's
467- # site-packages directory. This gives the stdlib precedence, while allowing
468- # users to override non-stdlib packages that may have been bundled with
469- # the runtime (usually pip).
470- # NOTE: There isn't a good way to identify the stdlib paths, so we just
471- # expect site-packages comes after it, per
472- # https://docs.python.org/3/library/sys_path_init.html#sys-path-init
473- for i , path in enumerate (sys .path ):
474- # dist-packages is a debian convention, see
475- # https://wiki.debian.org/Python#Deviations_from_upstream
476- if os .path .basename (path ) in ("site-packages" , "dist-packages" ):
477- sys .path [i :i ] = python_path_entries
478- break
479- else :
480- # Otherwise, no site-packages directory was found, which is odd but ok.
481- sys .path .extend (python_path_entries )
482-
483389 # NOTE: The sys.path must be modified before coverage is imported/activated
484390 # NOTE: Perform this after the user imports are appended. This avoids a
485391 # user import accidentally triggering the site-packages logic above.
486392 sys .path [0 :0 ] = prepend_path_entries
487393
394+ # todo: also check COVERAGE_DIR env var
488395 import _bazel_site_init
489396
490397 with _maybe_collect_coverage (enable = _bazel_site_init .COVERAGE_SETUP ):
0 commit comments