Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traverse directories in stable order when building a PEX #2220

Merged
merged 8 commits into from
Aug 21, 2023

Conversation

ento
Copy link
Contributor

@ento ento commented Aug 14, 2023

Addresses one part of #2155.

Adds a wrapper for os.walk that sorts directory entries as it traverses directories. As discussed in the issue, os.walk may yield directory entries in a different order depending on (possibly) the file system.

I don't know if the two os.walk calls that this PR replaces are enough for addressing this class of non-determinism in pex, but these were sufficient for solving the issue in my case. Happy to expand the use of the wrapper if there are other places that it should be used.

Copy link
Member

@jsirois jsirois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ento. This LGTM.

@@ -791,7 +791,7 @@ def __call__(self, *args, **kwargs):
def _increment_counter(self, counter_key):
# type: (str) -> int
self._counters[counter_key] += 1
return self._counters[counter_key]
return cast(int, self._counters[counter_key])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to type self._counters = defaultdict(int) # type: DefaultDict[str, int] where you declare / initialize, but it would be arguably even better to use collections.Counter (with similar declaration site typing) which is exactly built for this.

root_dir = os.path.join(tmp, "root")
os.mkdir(root_dir)
dir_a = os.path.join(root_dir, "a")
os.mkdir(dir_a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The os.mkdirs are fine, but touch takes care of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forgot to follow up here. Looking into it now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Fingers crossed the new commit doesn't end up needing multiple CI runs.

Also thanks for the other review comments - I learned a few things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fingers crossed the new commit doesn't end up needing multiple CI runs.

The change in #2198, which just landed aheaded of this PR, should help with things going forward. If you continue to do development on Pex, you too can take advantage with tox -e<test env> -- --devpi.

pex/common.py Outdated
# type: (*Any, **Any) -> Iterator[Tuple[str, List[str], List[str]]]
"""Walk the specified directory tree in deterministic order.

Takes the same parameters as os.walk and yields tuples of the same shape.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like calling out the topdown restriction, would be good.

@jsirois jsirois merged commit c6fba6d into pex-tool:main Aug 21, 2023
30 checks passed
@jsirois jsirois mentioned this pull request Aug 21, 2023
1 task
@jsirois
Copy link
Member

jsirois commented Aug 21, 2023

Thanks again @ento. This should go out in 2.1.144 tomorrow.

@jsirois jsirois mentioned this pull request Aug 22, 2023
1 task
@jsirois
Copy link
Member

jsirois commented Aug 22, 2023

Well, tomorrow came and went, but this is now released @ento:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants