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

Fix site.getsitepackages() broken on python2 on debian #2108

Merged
merged 8 commits into from
May 5, 2021

Conversation

freundTech
Copy link
Contributor

Fixes #2105.
This PR also adds a regression test.
There might be a merge conflict between this PR and #2107, because they both add a test to the end of tests/unit/create/test_creator.py

@gaborbernat
Copy link
Contributor

Can you rebase on the master and fix the merge conflict? Thanks!

sitepackages = orig_getsitepackages()
for prefix in (sys.prefix, sys.exec_prefix):
# os is not defined here, but will be in site.py, where we monkey-patch this function.
path = os.path.join(prefix, "lib", "python" + sys.version[:3], "site-packages") # noqa: F821
Copy link
Contributor

Choose a reason for hiding this comment

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

You can't write code like this, that's platform dependent. You must detect if you're on debian and execute this only then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh. Right. I forgot the check. I think checking for linux should be enough though, as non debian linux will already contain the path, so if path not in sitepackages will be False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code now does the same check the debians patched site.py does. This prevents the code from running on systems where it would break.

It will still run on non-debian systems, but shouldn't have any effect there. I don't think specifically detecting debian would be practical.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. Right. I forgot the check. I think checking for linux should be enough though, as non debian linux will already contain the path, so if path not in sitepackages will be False.

That's some very presumptions statement there. What stops macOS from patching similarly as does Debian? Or any other distribution? We really must restrict this to Debian only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that this should also handle all cases where an OS patches python in a way similar to debian.
The added path isn't debian specific. It is virtualenv specific and should always be in getsitepackages. The problem is just that because of debians patches it isn't.

The only thing I would have to fix is use ___EXPECTED_SITE_PACKAGES___ instead of hardcoding "site-packages".

I can however try restricting it to debian to be save.
Detecting debian and it derivatives isn't easy though. Just checking if platform.dist is debian isn't enough as we also need to detect Ubuntu and other derivatives.

After looking through all the debian patches I think the best way would be to detect the "deb_system" key in distutils.command.install.INSTALL_SCHEMES.
Alternatively it would be possible to search for occurrences of "dist-packages" in the source of getsitepackages.

Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing I would have to fix is use ___EXPECTED_SITE_PACKAGES___ instead of hardcoding "site-packages".

This might suffice.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you can do this then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just pushed a new version. I pulled some code from load_host_site into a new function to reuse it in rewrite_getsitepackages.

Because the code now makes no assumptions about the operating system other than what is already assumed by load_host_site I removed the os check.

@freundTech freundTech force-pushed the fix-getsitepackages-debian branch from 6f50ebf to 40c2751 Compare May 4, 2021 16:52
@codecov
Copy link

codecov bot commented May 4, 2021

Codecov Report

Merging #2108 (dcc2a84) into main (cc91e3f) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2108   +/-   ##
=======================================
  Coverage   93.70%   93.70%           
=======================================
  Files          88       88           
  Lines        4385     4385           
=======================================
  Hits         4109     4109           
  Misses        276      276           
Flag Coverage Δ
tests 93.70% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc91e3f...dcc2a84. Read the comment docs.

@freundTech freundTech force-pushed the fix-getsitepackages-debian branch from 59e206c to 87ba615 Compare May 4, 2021 17:23
sitepackages = orig_getsitepackages()
for prefix in (sys.prefix, sys.exec_prefix):
# os is not defined here, but will be in site.py, where we monkey-patch this function.
path = os.path.join(prefix, "lib", "python" + sys.version[:3], "site-packages") # noqa: F821
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. Right. I forgot the check. I think checking for linux should be enough though, as non debian linux will already contain the path, so if path not in sitepackages will be False.

That's some very presumptions statement there. What stops macOS from patching similarly as does Debian? Or any other distribution? We really must restrict this to Debian only.

sitepackages = orig_getsitepackages()
for prefix in (sys.prefix, sys.exec_prefix):
# os is not defined here, but will be in site.py, where we monkey-patch this function.
path = os.path.join(prefix, "lib", "python" + sys.version[:3], "site-packages") # noqa: F821
Copy link
Contributor

Choose a reason for hiding this comment

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

If you can do this then 👍

Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

I'll approve if you manage to get the CI working.

@freundTech
Copy link
Contributor Author

I'm currently looking into it. It seems to be a kind of bootstrap problem, where currently it only fails if the tests are run inside a virtualenv that already contains this PR, due to the way I get the system site-packages to test against.

@freundTech
Copy link
Contributor Author

CI should pass now.

Looks like one of the runs is locked up though.

@freundTech freundTech force-pushed the fix-getsitepackages-debian branch from 38e2050 to b5a3948 Compare May 5, 2021 15:57
@freundTech
Copy link
Contributor Author

I retriggered the CI with an empty commit and everything works now.

Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

@gaborbernat gaborbernat merged commit 7bfd444 into pypa:main May 5, 2021
@gaborbernat
Copy link
Contributor

Released via https://pypi.org/project/virtualenv/20.4.6/

@freundTech
Copy link
Contributor Author

Thanks!

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.

site.getsitepackages() broken on debian
2 participants