8000 Fix extract_single_wheel for Windows (#498) · tecton-ai/rules_python@cd64466 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd64466

Browse files
authored
Fix extract_single_wheel for Windows (bazel-contrib#498)
On Windows, when using pip_parse, pip would fail with following error: Could not open requirements file: [Errno 13] Permission denied: ... This is due to Python holding a handle to the temporary file preventing pip, which is run as a sub-process, from reading it. For more information, see: https://bugs.python.org/issue14243 Closing the requirements file before running pip solves the problem.
1 parent fbbecae commit cd64466

File tree

1 file changed

+12
-1
lines changed
  • python/pip_install/parse_requirements_to_bzl/extract_single_wheel

1 file changed

+12
-1
lines changed

python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import argparse
23
import sys
34
import glob
@@ -29,15 +30,25 @@ def main() -> None:
2930
if args.extra_pip_args:
3031
pip_args += json.loads(args.extra_pip_args)["args"]
3132

32-
with NamedTemporaryFile(mode='wb') as requirement_file:
33+
requirement_file = NamedTemporaryFile(mode='wb', delete=False)
34+
try:
3335
requirement_file.write(args.requirement.encode("utf-8"))
3436
requirement_file.flush()
37+
# Close the file so pip is allowed to read it when running on Windows.
38+
# For more information, see: https://bugs.python.org/issue14243
39+
requirement_file.close()
3540
# Requirement specific args like --hash can only be passed in a requirements file,
3641
# so write our single requirement into a temp file in case it has any of those flags.
3742
pip_args.extend(["-r", requirement_file.name])
3843

3944
# Assumes any errors are logged by pip so do nothing. This command will fail if pip fails
4045
subprocess.run(pip_args, check=True)
46+
finally:
47+
try:
48+
os.unlink(requirement_file.name)
49+
except OSError as e:
50+
if e.errno != errno.ENOENT:
51+
raise
4152

4253
name, extras_for_pkg = requirements._parse_requirement_for_extra(args.requirement)
4354
extras = {name: extras_for_pkg} if extras_for_pkg and name else dict()

0 commit comments

Comments
 (0)
0