Skip to content

Commit af9e959

Browse files
fix(pypi): allow pip_compile to work with read-only sources (bazel-contrib#2712)
The validating `py_test` generated by `compile_pip_requirements` chokes when the source `requirements.txt` is stored read-only, such as when managed by the Perforce Helix Core SCM. Though `dependency_resolver` makes a temporary copy of this file, it does so w/ `shutil.copy` which preserves the original read-only file mode. To address this, this commit replaces `shutil.copy` with a `shutil.copyfileobj` such that the temporary file is created w/ permissions according to the user's umask. Resolves (bazel-contrib#2608). --------- Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
1 parent ce80db6 commit af9e959

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ END_UNRELEASED_TEMPLATE
9393
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
9494
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
9595
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
96+
* (pypi) When running under `bazel test`, be sure that temporary `requirements` file
97+
remains writable.
9698
* (py_test, py_binary) Allow external files to be used for main
9799

98100
{#v0-0-0-added}

python/private/pypi/dependency_resolver/dependency_resolver.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,16 @@ def main(
151151
requirements_out = os.path.join(
152152
os.environ["TEST_TMPDIR"], os.path.basename(requirements_file) + ".out"
153153
)
154+
# Why this uses shutil.copyfileobj:
155+
#
154156
# Those two files won't necessarily be on the same filesystem, so we can't use os.replace
155157
# or shutil.copyfile, as they will fail with OSError: [Errno 18] Invalid cross-device link.
156-
shutil.copy(resolved_requirements_file, requirements_out)
158+
#
159+
# Further, shutil.copy preserves the source file's mode, and so if
160+
# our source file is read-only (the default under Perforce Helix),
161+
# this scratch file will also be read-only, defeating its purpose.
162+
with open(resolved_requirements_file, "rb") as fsrc, open(requirements_out, "wb") as fdst:
163+
shutil.copyfileobj(fsrc, fdst)
157164

158165
update_command = (
159166
os.getenv("CUSTOM_COMPILE_COMMAND") or f"bazel run {target_label_prefix}.update"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy