tairos-data-convert 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,609 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # This script must retain compatibility with a wide variety of Python versions
4
+ # since it is run for every py_binary target. Currently we guarantee support
5
+ # going back to Python 2.7, and try to support even Python 2.6 on a best-effort
6
+ # basis. We might abandon 2.6 support once users have the ability to control the
7
+ # above shebang string via the Python toolchain (#8685).
8
+
9
+ from __future__ import absolute_import
10
+ from __future__ import division
11
+ from __future__ import print_function
12
+
13
+ import sys
14
+
15
+ # The Python interpreter unconditionally prepends the directory containing this
16
+ # script (following symlinks) to the import path. This is the cause of #9239,
17
+ # and is a special case of #7091. We therefore explicitly delete that entry.
18
+ # TODO(#7091): Remove this hack when no longer necessary.
19
+ del sys.path[0]
20
+
21
+ import os
22
+ import subprocess
23
+ import uuid
24
+
25
+ def IsRunningFromZip():
26
+ return 0
27
+
28
+ if IsRunningFromZip():
29
+ import shutil
30
+ import tempfile
31
+ import zipfile
32
+ else:
33
+ import re
34
+
35
+ # Return True if running on Windows
36
+ def IsWindows():
37
+ return os.name == 'nt'
38
+
39
+ def GetWindowsPathWithUNCPrefix(path):
40
+ """Adds UNC prefix after getting a normalized absolute Windows path.
41
+
42
+ No-op for non-Windows platforms or if running under python2.
43
+ """
44
+ path = path.strip()
45
+
46
+ # No need to add prefix for non-Windows platforms.
47
+ # And \\?\ doesn't work in python 2 or on mingw
48
+ if not IsWindows() or sys.version_info[0] < 3:
49
+ return path
50
+
51
+ # Starting in Windows 10, version 1607(OS build 14393), MAX_PATH limitations have been
52
+ # removed from common Win32 file and directory functions.
53
+ # Related doc: https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd#enable-long-paths-in-windows-10-version-1607-and-later
54
+ import platform
55
+ if platform.win32_ver()[1] >= '10.0.14393':
56
+ return path
57
+
58
+ # import sysconfig only now to maintain python 2.6 compatibility
59
+ import sysconfig
60
+ if sysconfig.get_platform() == 'mingw':
61
+ return path
62
+
63
+ # Lets start the unicode fun
64
+ unicode_prefix = '\\\\?\\'
65
+ if path.startswith(unicode_prefix):
66
+ return path
67
+
68
+ # os.path.abspath returns a normalized absolute path
69
+ return unicode_prefix + os.path.abspath(path)
70
+
71
+ def HasWindowsExecutableExtension(path):
72
+ return path.endswith('.exe') or path.endswith('.com') or path.endswith('.bat')
73
+
74
+ PYTHON_BINARY = 'rules_python~~python~python_3_10_x86_64-unknown-linux-gnu/bin/python3'
75
+ if IsWindows() and not HasWindowsExecutableExtension(PYTHON_BINARY):
76
+ PYTHON_BINARY = PYTHON_BINARY + '.exe'
77
+
78
+ def SearchPath(name):
79
+ """Finds a file in a given search path."""
80
+ search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
81
+ for directory in search_path:
82
+ if directory:
83
+ path = os.path.join(directory, name)
84
+ if os.path.isfile(path) and os.access(path, os.X_OK):
85
+ return path
86
+ return None
87
+
88
+ def FindPythonBinary(module_space):
89
+ """Finds the real Python binary if it's not a normal absolute path."""
90
+ return FindBinary(module_space, PYTHON_BINARY)
91
+
92
+ def print_verbose(*args, mapping=None, values=None):
93
+ if os.environ.get("RULES_PYTHON_BOOTSTRAP_VERBOSE"):
94
+ if mapping is not None:
95
+ for key, value in sorted((mapping or {}).items()):
96
+ print(
97
+ "bootstrap:",
98
+ *args,
99
+ f"{key}={value!r}",
100
+ file=sys.stderr,
101
+ flush=True,
102
+ )
103
+ elif values is not None:
104
+ for i, v in enumerate(values):
105
+ print(
106
+ "bootstrap:",
107
+ *args,
108
+ f"[{i}] {v!r}",
109
+ file=sys.stderr,
110
+ flush=True,
111
+ )
112
+ else:
113
+ print("bootstrap:", *args, file=sys.stderr, flush=True)
114
+
115
+ def PrintVerboseCoverage(*args):
116
+ """Print output if VERBOSE_COVERAGE is non-empty in the environment."""
117
+ if os.environ.get("VERBOSE_COVERAGE"):
118
+ print(*args, file=sys.stderr)
119
+
120
+ def IsVerboseCoverage():
121
+ """Returns True if VERBOSE_COVERAGE is non-empty in the environment."""
122
+ return os.environ.get("VERBOSE_COVERAGE")
123
+
124
+ def FindCoverageEntryPoint(module_space):
125
+ cov_tool = ''
126
+ if cov_tool:
127
+ PrintVerboseCoverage('Using toolchain coverage_tool %r' % cov_tool)
128
+ else:
129
+ cov_tool = os.environ.get('PYTHON_COVERAGE')
130
+ if cov_tool:
131
+ PrintVerboseCoverage('PYTHON_COVERAGE: %r' % cov_tool)
132
+ if cov_tool:
133
+ return FindBinary(module_space, cov_tool)
134
+ return None
135
+
136
+ def FindBinary(module_space, bin_name):
137
+ """Finds the real binary if it's not a normal absolute path."""
138
+ if not bin_name:
139
+ return None
140
+ if bin_name.startswith("//"):
141
+ # Case 1: Path is a label. Not supported yet.
142
+ raise AssertionError(
143
+ "Bazel does not support execution of Python interpreters via labels yet"
144
+ )
145
+ elif os.path.isabs(bin_name):
146
+ # Case 2: Absolute path.
147
+ return bin_name
148
+ # Use normpath() to convert slashes to os.sep on Windows.
149
+ elif os.sep in os.path.normpath(bin_name):
150
+ # Case 3: Path is relative to the repo root.
151
+ return os.path.join(module_space, bin_name)
152
+ else:
153
+ # Case 4: Path has to be looked up in the search path.
154
+ return SearchPath(bin_name)
155
+
156
+ def CreatePythonPathEntries(python_imports, module_space):
157
+ parts = python_imports.split(':')
158
+ return [module_space] + ['%s/%s' % (module_space, path) for path in parts]
159
+
160
+ def FindModuleSpace(main_rel_path):
161
+ """Finds the runfiles tree."""
162
+ # When the calling process used the runfiles manifest to resolve the
163
+ # location of this stub script, the path may be expanded. This means
164
+ # argv[0] may no longer point to a location inside the runfiles
165
+ # directory. We should therefore respect RUNFILES_DIR and
166
+ # RUNFILES_MANIFEST_FILE set by the caller.
167
+ runfiles_dir = os.environ.get('RUNFILES_DIR', None)
168
+ if not runfiles_dir:
169
+ runfiles_manifest_file = os.environ.get('RUNFILES_MANIFEST_FILE', '')
170
+ if (runfiles_manifest_file.endswith('.runfiles_manifest') or
171
+ runfiles_manifest_file.endswith('.runfiles/MANIFEST')):
172
+ runfiles_dir = runfiles_manifest_file[:-9]
173
+ # Be defensive: the runfiles dir should contain our main entry point. If
174
+ # it doesn't, then it must not be our runfiles directory.
175
+ if runfiles_dir and os.path.exists(os.path.join(runfiles_dir, main_rel_path)):
176
+ return runfiles_dir
177
+
178
+ stub_filename = sys.argv[0]
179
+ # On Windows, the path may contain both forward and backslashes.
180
+ # Normalize to the OS separator because the regex used later assumes
181
+ # the OS-specific separator.
182
+ if IsWindows:
183
+ stub_filename = stub_filename.replace("/", os.sep)
184
+
185
+ if not os.path.isabs(stub_filename):
186
+ stub_filename = os.path.join(os.getcwd(), stub_filename)
187
+
188
+ while True:
189
+ module_space = stub_filename + ('.exe' if IsWindows() else '') + '.runfiles'
190
+ if os.path.isdir(module_space):
191
+ return module_space
192
+
193
+ runfiles_pattern = r'(.*\.runfiles)' + (r'\\' if IsWindows() else '/') + '.*'
194
+ matchobj = re.match(runfiles_pattern, stub_filename)
195
+ if matchobj:
196
+ return matchobj.group(1)
197
+
198
+ if not os.path.islink(stub_filename):
199
+ break
200
+ target = os.readlink(stub_filename)
201
+ if os.path.isabs(target):
202
+ stub_filename = target
203
+ else:
204
+ stub_filename = os.path.join(os.path.dirname(stub_filename), target)
205
+
206
+ raise AssertionError('Cannot find .runfiles directory for %s' % sys.argv[0])
207
+
208
+ def ExtractZip(zip_path, dest_dir):
209
+ """Extracts the contents of a zip file, preserving the unix file mode bits.
210
+
211
+ These include the permission bits, and in particular, the executable bit.
212
+
213
+ Ideally the zipfile module should set these bits, but it doesn't. See:
214
+ https://bugs.python.org/issue15795.
215
+
216
+ Args:
217
+ zip_path: The path to the zip file to extract
218
+ dest_dir: The path to the destination directory
219
+ """
220
+ zip_path = GetWindowsPathWithUNCPrefix(zip_path)
221
+ dest_dir = GetWindowsPathWithUNCPrefix(dest_dir)
222
+ with zipfile.ZipFile(zip_path) as zf:
223
+ for info in zf.infolist():
224
+ zf.extract(info, dest_dir)
225
+ # UNC-prefixed paths must be absolute/normalized. See
226
+ # https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-path-length-limitation
227
+ file_path = os.path.abspath(os.path.join(dest_dir, info.filename))
228
+ # The Unix st_mode bits (see "man 7 inode") are stored in the upper 16
229
+ # bits of external_attr. Of those, we set the lower 12 bits, which are the
230
+ # file mode bits (since the file type bits can't be set by chmod anyway).
231
+ attrs = info.external_attr >> 16
232
+ if attrs != 0: # Rumor has it these can be 0 for zips created on Windows.
233
+ os.chmod(file_path, attrs & 0o7777)
234
+
235
+ # Create the runfiles tree by extracting the zip file
236
+ def CreateModuleSpace():
237
+ temp_dir = tempfile.mkdtemp('', 'Bazel.runfiles_')
238
+ ExtractZip(os.path.dirname(__file__), temp_dir)
239
+ # IMPORTANT: Later code does `rm -fr` on dirname(module_space) -- it's
240
+ # important that deletion code be in sync with this directory structure
241
+ return os.path.join(temp_dir, 'runfiles')
242
+
243
+ # Returns repository roots to add to the import path.
244
+ def GetRepositoriesImports(module_space, import_all):
245
+ if import_all:
246
+ repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)]
247
+ repo_dirs.sort()
248
+ return [d for d in repo_dirs if os.path.isdir(d)]
249
+ return [os.path.join(module_space, '_main')]
250
+
251
+ def RunfilesEnvvar(module_space):
252
+ """Finds the runfiles manifest or the runfiles directory.
253
+
254
+ Returns:
255
+ A tuple of (var_name, var_value) where var_name is either 'RUNFILES_DIR' or
256
+ 'RUNFILES_MANIFEST_FILE' and var_value is the path to that directory or
257
+ file, or (None, None) if runfiles couldn't be found.
258
+ """
259
+ # If this binary is the data-dependency of another one, the other sets
260
+ # RUNFILES_MANIFEST_FILE or RUNFILES_DIR for our sake.
261
+ runfiles = os.environ.get('RUNFILES_MANIFEST_FILE', None)
262
+ if runfiles:
263
+ return ('RUNFILES_MANIFEST_FILE', runfiles)
264
+
265
+ runfiles = os.environ.get('RUNFILES_DIR', None)
266
+ if runfiles:
267
+ return ('RUNFILES_DIR', runfiles)
268
+
269
+ # If running from a zip, there's no manifest file.
270
+ if IsRunningFromZip():
271
+ return ('RUNFILES_DIR', module_space)
272
+
273
+ # Look for the runfiles "output" manifest, argv[0] + ".runfiles_manifest"
274
+ runfiles = module_space + '_manifest'
275
+ if os.path.exists(runfiles):
276
+ return ('RUNFILES_MANIFEST_FILE', runfiles)
277
+
278
+ # Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST"
279
+ # Normally .runfiles_manifest and MANIFEST are both present, but the
280
+ # former will be missing for zip-based builds or if someone copies the
281
+ # runfiles tree elsewhere.
282
+ runfiles = os.path.join(module_space, 'MANIFEST')
283
+ if os.path.exists(runfiles):
284
+ return ('RUNFILES_MANIFEST_FILE', runfiles)
285
+
286
+ # If running in a sandbox and no environment variables are set, then
287
+ # Look for the runfiles next to the binary.
288
+ if module_space.endswith('.runfiles') and os.path.isdir(module_space):
289
+ return ('RUNFILES_DIR', module_space)
290
+
291
+ return (None, None)
292
+
293
+ def Deduplicate(items):
294
+ """Efficiently filter out duplicates, keeping the first element only."""
295
+ seen = set()
296
+ for it in items:
297
+ if it not in seen:
298
+ seen.add(it)
299
+ yield it
300
+
301
+ def InstrumentedFilePaths():
302
+ """Yields tuples of realpath of each instrumented file with the relative path."""
303
+ manifest_filename = os.environ.get('COVERAGE_MANIFEST')
304
+ if not manifest_filename:
305
+ return
306
+ with open(manifest_filename, "r") as manifest:
307
+ for line in manifest:
308
+ filename = line.strip()
309
+ if not filename:
310
+ continue
311
+ try:
312
+ realpath = os.path.realpath(filename)
313
+ except OSError:
314
+ print(
315
+ "Could not find instrumented file {}".format(filename),
316
+ file=sys.stderr)
317
+ continue
318
+ if realpath != filename:
319
+ PrintVerboseCoverage("Fixing up {} -> {}".format(realpath, filename))
320
+ yield (realpath, filename)
321
+
322
+ def UnresolveSymlinks(output_filename):
323
+ # type: (str) -> None
324
+ """Replace realpath of instrumented files with the relative path in the lcov output.
325
+
326
+ Though we are asking coveragepy to use relative file names, currently
327
+ ignore that for purposes of generating the lcov report (and other reports
328
+ which are not the XML report), so we need to go and fix up the report.
329
+
330
+ This function is a workaround for that issue. Once that issue is fixed
331
+ upstream and the updated version is widely in use, this should be removed.
332
+
333
+ See https://github.com/nedbat/coveragepy/issues/963.
334
+ """
335
+ substitutions = list(InstrumentedFilePaths())
336
+ if substitutions:
337
+ unfixed_file = output_filename + '.tmp'
338
+ os.rename(output_filename, unfixed_file)
339
+ with open(unfixed_file, "r") as unfixed:
340
+ with open(output_filename, "w") as output_file:
341
+ for line in unfixed:
342
+ if line.startswith('SF:'):
343
+ for (realpath, filename) in substitutions:
344
+ line = line.replace(realpath, filename)
345
+ output_file.write(line)
346
+ os.unlink(unfixed_file)
347
+
348
+ def ExecuteFile(python_program, main_filename, args, env, module_space,
349
+ coverage_entrypoint, workspace, delete_module_space):
350
+ # type: (str, str, list[str], dict[str, str], str, str|None, str|None) -> ...
351
+ """Executes the given Python file using the various environment settings.
352
+
353
+ This will not return, and acts much like os.execv, except is much
354
+ more restricted, and handles Bazel-related edge cases.
355
+
356
+ Args:
357
+ python_program: (str) Path to the Python binary to use for execution
358
+ main_filename: (str) The Python file to execute
359
+ args: (list[str]) Additional args to pass to the Python file
360
+ env: (dict[str, str]) A dict of environment variables to set for the execution
361
+ module_space: (str) Path to the module space/runfiles tree directory
362
+ coverage_entrypoint: (str|None) Path to the coverage tool entry point file.
363
+ workspace: (str|None) Name of the workspace to execute in. This is expected to be a
364
+ directory under the runfiles tree.
365
+ delete_module_space: (bool), True if the module space should be deleted
366
+ after a successful (exit code zero) program run, False if not.
367
+ """
368
+ # We want to use os.execv instead of subprocess.call, which causes
369
+ # problems with signal passing (making it difficult to kill
370
+ # Bazel). However, these conditions force us to run via
371
+ # subprocess.call instead:
372
+ #
373
+ # - On Windows, os.execv doesn't handle arguments with spaces
374
+ # correctly, and it actually starts a subprocess just like
375
+ # subprocess.call.
376
+ # - When running in a workspace or zip file, we need to clean up the
377
+ # workspace after the process finishes so control must return here.
378
+ # - If we may need to emit a host config warning after execution, we
379
+ # can't execv because we need control to return here. This only
380
+ # happens for targets built in the host config.
381
+ # - For coverage targets, at least coveragepy requires running in
382
+ # two invocations, which also requires control to return here.
383
+ #
384
+ if not (IsWindows() or workspace or coverage_entrypoint or delete_module_space):
385
+ _RunExecv(python_program, main_filename, args, env)
386
+
387
+ if coverage_entrypoint is not None:
388
+ ret_code = _RunForCoverage(python_program, main_filename, args, env,
389
+ coverage_entrypoint, workspace)
390
+ else:
391
+ ret_code = subprocess.call(
392
+ [python_program, main_filename] + args,
393
+ env=env,
394
+ cwd=workspace
395
+ )
396
+
397
+ if delete_module_space:
398
+ # NOTE: dirname() is called because CreateModuleSpace() creates a
399
+ # sub-directory within a temporary directory, and we want to remove the
400
+ # whole temporary directory.
401
+ shutil.rmtree(os.path.dirname(module_space), True)
402
+ sys.exit(ret_code)
403
+
404
+ def _RunExecv(python_program, main_filename, args, env):
405
+ # type: (str, str, list[str], dict[str, str]) -> ...
406
+ """Executes the given Python file using the various environment settings."""
407
+ os.environ.update(env)
408
+ print_verbose("RunExecv: environ:", mapping=os.environ)
409
+ argv = [python_program, main_filename] + args
410
+ print_verbose("RunExecv: argv:", python_program, argv)
411
+ os.execv(python_program, argv)
412
+
413
+ def _RunForCoverage(python_program, main_filename, args, env,
414
+ coverage_entrypoint, workspace):
415
+ # type: (str, str, list[str], dict[str, str], str, str|None) -> int
416
+ """Collects coverage infomration for the given Python file.
417
+
418
+ Args:
419
+ python_program: (str) Path to the Python binary to use for execution
420
+ main_filename: (str) The Python file to execute
421
+ args: (list[str]) Additional args to pass to the Python file
422
+ env: (dict[str, str]) A dict of environment variables to set for the execution
423
+ coverage_entrypoint: (str|None) Path to the coverage entry point to execute with.
424
+ workspace: (str|None) Name of the workspace to execute in. This is expected to be a
425
+ directory under the runfiles tree, and will recursively delete the
426
+ runfiles directory if set.
427
+ """
428
+ # We need for coveragepy to use relative paths. This can only be configured
429
+ unique_id = uuid.uuid4()
430
+ rcfile_name = os.path.join(os.environ['COVERAGE_DIR'], ".coveragerc_{}".format(unique_id))
431
+ with open(rcfile_name, "w") as rcfile:
432
+ rcfile.write('''[run]
433
+ relative_files = True
434
+ ''')
435
+ PrintVerboseCoverage('Coverage entrypoint:', coverage_entrypoint)
436
+ # First run the target Python file via coveragepy to create a .coverage
437
+ # database file, from which we can later export lcov.
438
+ ret_code = subprocess.call(
439
+ [
440
+ python_program,
441
+ coverage_entrypoint,
442
+ "run",
443
+ "--rcfile=" + rcfile_name,
444
+ "--append",
445
+ "--branch",
446
+ main_filename
447
+ ] + args,
448
+ env=env,
449
+ cwd=workspace
450
+ )
451
+ output_filename = os.path.join(os.environ['COVERAGE_DIR'], 'pylcov.dat')
452
+
453
+ PrintVerboseCoverage('Converting coveragepy database to lcov:', output_filename)
454
+ # Run coveragepy again to convert its .coverage database file into lcov.
455
+ # Under normal conditions running lcov outputs to stdout/stderr, which causes problems for `coverage`.
456
+ params = [python_program, coverage_entrypoint, "lcov", "--rcfile=" + rcfile_name, "-o", output_filename, "--quiet"]
457
+ kparams = {"env": env, "cwd": workspace, "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
458
+ if IsVerboseCoverage():
459
+ # reconnect stdout/stderr to lcov generation. Should be useful for debugging `coverage` issues.
460
+ params.remove("--quiet")
461
+ kparams['stdout'] = sys.stderr
462
+ kparams['stderr'] = sys.stderr
463
+
464
+ ret_code = subprocess.call(
465
+ params,
466
+ **kparams
467
+ ) or ret_code
468
+
469
+ try:
470
+ os.unlink(rcfile_name)
471
+ except OSError as err:
472
+ # It's possible that the profiled program might execute another Python
473
+ # binary through a wrapper that would then delete the rcfile. Not much
474
+ # we can do about that, besides ignore the failure here.
475
+ PrintVerboseCoverage('Error removing temporary coverage rc file:', err)
476
+ if os.path.isfile(output_filename):
477
+ UnresolveSymlinks(output_filename)
478
+ return ret_code
479
+
480
+ def Main():
481
+ print_verbose("initial argv:", values=sys.argv)
482
+ print_verbose("initial cwd:", os.getcwd())
483
+ print_verbose("initial environ:", mapping=os.environ)
484
+ print_verbose("initial sys.path:", values=sys.path)
485
+ args = sys.argv[1:]
486
+
487
+ new_env = {}
488
+
489
+ # The main Python source file.
490
+ # The magic string percent-main-percent is replaced with the runfiles-relative
491
+ # filename of the main file of the Python binary in BazelPythonSemantics.java.
492
+ main_rel_path = '_main/data_pipeline/data_process/data_convert/data_convert.py'
493
+ if IsWindows():
494
+ main_rel_path = main_rel_path.replace('/', os.sep)
495
+
496
+ if IsRunningFromZip():
497
+ module_space = CreateModuleSpace()
498
+ delete_module_space = True
499
+ else:
500
+ module_space = FindModuleSpace(main_rel_path)
501
+ delete_module_space = False
502
+
503
+ python_imports = ''
504
+ python_path_entries = CreatePythonPathEntries(python_imports, module_space)
505
+ python_path_entries += GetRepositoriesImports(module_space, True)
506
+ # Remove duplicates to avoid overly long PYTHONPATH (#10977). Preserve order,
507
+ # keep first occurrence only.
508
+ python_path_entries = [
509
+ GetWindowsPathWithUNCPrefix(d)
510
+ for d in python_path_entries
511
+ ]
512
+
513
+ old_python_path = os.environ.get('PYTHONPATH')
514
+ if old_python_path:
515
+ python_path_entries += old_python_path.split(os.pathsep)
516
+
517
+ python_path = os.pathsep.join(Deduplicate(python_path_entries))
518
+
519
+ if IsWindows():
520
+ python_path = python_path.replace('/', os.sep)
521
+
522
+ new_env['PYTHONPATH'] = python_path
523
+ runfiles_envkey, runfiles_envvalue = RunfilesEnvvar(module_space)
524
+ if runfiles_envkey:
525
+ new_env[runfiles_envkey] = runfiles_envvalue
526
+
527
+ # Don't prepend a potentially unsafe path to sys.path
528
+ # See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH
529
+ new_env['PYTHONSAFEPATH'] = '1'
530
+
531
+ main_filename = os.path.join(module_space, main_rel_path)
532
+ main_filename = GetWindowsPathWithUNCPrefix(main_filename)
533
+ assert os.path.exists(main_filename), \
534
+ 'Cannot exec() %r: file not found.' % main_filename
535
+ assert os.access(main_filename, os.R_OK), \
536
+ 'Cannot exec() %r: file not readable.' % main_filename
537
+
538
+ program = python_program = FindPythonBinary(module_space)
539
+ if python_program is None:
540
+ raise AssertionError('Could not find python binary: ' + PYTHON_BINARY)
541
+
542
+ # COVERAGE_DIR is set if coverage is enabled and instrumentation is configured
543
+ # for something, though it could be another program executing this one or
544
+ # one executed by this one (e.g. an extension module).
545
+ if os.environ.get('COVERAGE_DIR'):
546
+ cov_tool = FindCoverageEntryPoint(module_space)
547
+ if cov_tool is None:
548
+ PrintVerboseCoverage('Coverage was enabled, but python coverage tool was not configured.')
549
+ else:
550
+ # Inhibit infinite recursion:
551
+ if 'PYTHON_COVERAGE' in os.environ:
552
+ del os.environ['PYTHON_COVERAGE']
553
+
554
+ if not os.path.exists(cov_tool):
555
+ raise EnvironmentError(
556
+ 'Python coverage tool %r not found. '
557
+ 'Try running with VERBOSE_COVERAGE=1 to collect more information.'
558
+ % cov_tool
559
+ )
560
+
561
+ # coverage library expects sys.path[0] to contain the library, and replaces
562
+ # it with the directory of the program it starts. Our actual sys.path[0] is
563
+ # the runfiles directory, which must not be replaced.
564
+ # CoverageScript.do_execute() undoes this sys.path[0] setting.
565
+ #
566
+ # Update sys.path such that python finds the coverage package. The coverage
567
+ # entry point is coverage.coverage_main, so we need to do twice the dirname.
568
+ python_path_entries = new_env['PYTHONPATH'].split(os.pathsep)
569
+ python_path_entries.append(os.path.dirname(os.path.dirname(cov_tool)))
570
+ new_env['PYTHONPATH'] = os.pathsep.join(Deduplicate(python_path_entries))
571
+ else:
572
+ cov_tool = None
573
+
574
+ # Some older Python versions on macOS (namely Python 3.7) may unintentionally
575
+ # leave this environment variable set after starting the interpreter, which
576
+ # causes problems with Python subprocesses correctly locating sys.executable,
577
+ # which subsequently causes failure to launch on Python 3.11 and later.
578
+ if '__PYVENV_LAUNCHER__' in os.environ:
579
+ del os.environ['__PYVENV_LAUNCHER__']
580
+
581
+ new_env.update((key, val) for key, val in os.environ.items() if key not in new_env)
582
+
583
+ workspace = None
584
+ if IsRunningFromZip():
585
+ # If RUN_UNDER_RUNFILES equals 1, it means we need to
586
+ # change directory to the right runfiles directory.
587
+ # (So that the data files are accessible)
588
+ if os.environ.get('RUN_UNDER_RUNFILES') == '1':
589
+ workspace = os.path.join(module_space, '_main')
590
+
591
+ try:
592
+ sys.stdout.flush()
593
+ # NOTE: ExecuteFile may call execve() and lines after this will never run.
594
+ ExecuteFile(
595
+ python_program, main_filename, args, new_env, module_space,
596
+ cov_tool, workspace,
597
+ delete_module_space = delete_module_space,
598
+ )
599
+
600
+ except EnvironmentError:
601
+ # This works from Python 2.4 all the way to 3.x.
602
+ e = sys.exc_info()[1]
603
+ # This exception occurs when os.execv() fails for some reason.
604
+ if not getattr(e, 'filename', None):
605
+ e.filename = program # Add info to error message
606
+ raise
607
+
608
+ if __name__ == '__main__':
609
+ Main()