Skip to content

Commit

Permalink
[antlir] One more fix for fs_util
Browse files Browse the repository at this point in the history
Summary: It looks like Antlir doesn't always materialize Python resources properly. Existing check for `path.name` passes but that still doesn't guarantee that the file exists. Switching to `os.path.exists` check instead.

Test Plan:
```
buck2 test fbcode//antlir:test-fs-utils
buck2 test fbcode//antlir:test-fs-utils-path-resource-zip
buck2 test fbcode//antlir/bzl/genrule/facebook/chef_solo:test-centos9-fbcode-chef-solo-in-layer
buck2 test fbcode//antlir/bzl/genrule/facebook/chef_solo:test-centos8-fbcode-chef-solo-in-layer
```

Reviewed By: justintrudell

Differential Revision: D50604944

fbshipit-source-id: 64752cbf6a53b58605ca40b939498cfa2372d8a1
  • Loading branch information
Serge Dubrouski authored and facebook-github-bot committed Oct 24, 2023
1 parent cff2b80 commit 5b6b44b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions antlir/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,22 @@ def resource(cls, package, name: str, *, exe: bool) -> Iterator["Path"]:
open-source Python package formats: "zip", "fastzip", "pex", "xar".
"""
with importlib.resources.open_binary(package, name) as rsrc_in:
# This check is clowny, but `importlib` doesn't provide a clean
# way to ask if the resource already exists on disk.
if hasattr(rsrc_in, "name"):
# Future: once the bug with the XAR `access` implementation
# is fixed (https://fburl.com/42s41c0g), this can just check
# for boolean equality.
if not exe or (exe and os.access(rsrc_in.name, os.X_OK)):
yield Path(rsrc_in.name).abspath()
return
else: # pragma: no cover
# why does this happen? who knows but we can make a copy of
# the binary that _is_ executable
log.warning(f"{package}.{name} is not executable")
# Future: once the bug with the XAR `access` implementation
# is fixed (https://fburl.com/42s41c0g), this can just check
# for boolean equality.
if (
hasattr(rsrc_in, "name")
and os.path.exists(rsrc_in.name)
and (not exe or (exe and os.access(rsrc_in.name, os.X_OK)))
):
yield Path(rsrc_in.name).abspath()
return

# why does this happen? who knows but we can make a copy of
# the binary that _is_ executable
log.warning(
f"{package}.{name} doesn't exist or is not executable"
) # pragma: no cover
# The resource has no path, so we have to materialize it.
#
# This code path is not reached by our coverage harness,
Expand Down

0 comments on commit 5b6b44b

Please sign in to comment.