Skip to content

Commit

Permalink
Add handling for user site packages
Browse files Browse the repository at this point in the history
Workaround issue with python/cpython#109117 by manipulating sys.path
Detect if installed in a user site and manage schema appropriately
Add new versions of `pip` and `setuptools` to testing matrix

fixes #10
  • Loading branch information
arcivanov committed Sep 8, 2023
1 parent 74ee2ea commit ed84f1a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ jobs:
- '3.10'
- '3.9'
pip-version:
- '23.2'
- '23.1'
- '23.0'
- '22.3'
- '22.2'
setuptools-version:
- '68.2'
- '68.1'
- '68.0'
- '67.0'
- '66.0'
Expand Down Expand Up @@ -81,9 +84,12 @@ jobs:
- '3.8'
- '3.7'
pip-version:
- '23.2'
- '23.1'
- '22.0'
setuptools-version:
- '68.2'
- '68.1'
- '68.0'
- '67.0'
- '66.0'
Expand Down Expand Up @@ -118,9 +124,12 @@ jobs:
python-version:
- '3.13-dev'
pip-version:
- '23.2'
- '23.1'
- '23.0'
setuptools-version:
- '68.2'
- '68.1'
- '68.0'
- '67.0'
env:
Expand Down
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/wheel-axle-runtime.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ There are several security requirements and implications of having post-install

### TODOs

* Support schema detection for `home` and `prefix` installations.
* Support schema detection for `prefix` installations.
* Validate and verify Windows support.
45 changes: 37 additions & 8 deletions src/main/python/wheel_axle/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#

import os
from os.path import exists, join as jp
import contextlib
import sys
import site
from os.path import exists, join as jp, commonpath
from threading import RLock

from wheel_axle.runtime.constants import AXLE_DONE_FILE, AXLE_LOCK_FILE
Expand All @@ -28,6 +31,37 @@
inter_thread_lock = RLock()


def _run_installers(dist_info_dir):
# Get metadata
from wheel_axle.runtime._symlinks import SymlinksInstaller
from wheel_axle.runtime._axle import AxleFinalizer

installers = [SymlinksInstaller, AxleFinalizer] # AxleFinalizer is always last!
for installer in installers:
installer(dist_info_dir).run()


@contextlib.contextmanager
def user_site_handler(pth_path):
if sys.flags.no_site or (not site.check_enableusersite()):
yield
return

user_site_path = site.getusersitepackages()
in_user_site_path = False
try:
if commonpath((user_site_path, pth_path)) == user_site_path:
in_user_site_path = True

if in_user_site_path:
current_sys_path = sys.path[:]
sys.path[:] = site.addsitepackages(set(sys.path))
yield
finally:
if in_user_site_path:
sys.path[:] = current_sys_path


def finalize(pth_path):
dist_info_dir = pth_path[:-3] + _DIST_INFO
axle_done_path = jp(dist_info_dir, AXLE_DONE_FILE)
Expand All @@ -48,13 +82,8 @@ def finalize(pth_path):
if exists(axle_done_path):
return

# Get metadata
from wheel_axle.runtime._symlinks import SymlinksInstaller
from wheel_axle.runtime._axle import AxleFinalizer

installers = [SymlinksInstaller, AxleFinalizer] # AxleFinalizer is always last!
for installer in installers:
installer(dist_info_dir).run()
with user_site_handler(pth_path):
_run_installers(dist_info_dir)

# Always the last step
try:
Expand Down
19 changes: 11 additions & 8 deletions src/main/python/wheel_axle/runtime/_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#

import os
import site
import sys
from email.message import Message
from email.parser import Parser
from os.path import dirname, join as jp
from os.path import dirname, join as jp, commonpath
from typing import Tuple

from pip._internal.exceptions import UnsupportedWheel
Expand Down Expand Up @@ -59,7 +61,14 @@ def _get_dist(metadata_directory: str) -> Distribution:
def get_current_scheme(dist_info_dir, dist_name, wheel_meta):
install_dir = dirname(dist_info_dir)

scheme = get_scheme(dist_name)
if not sys.flags.no_site and site.check_enableusersite():
user_site_path = site.getusersitepackages()
if commonpath((install_dir, user_site_path)) == user_site_path:
scheme = get_scheme(dist_name, user=True)
else:
scheme = get_scheme(dist_name)
else:
scheme = get_scheme(dist_name)

def check_correct_scheme(scheme, install_dir):
if wheel_root_is_purelib(wheel_meta):
Expand All @@ -74,12 +83,6 @@ def check_correct_scheme(scheme, install_dir):

if check_correct_scheme(scheme, install_dir):
return scheme
else:
if not virtualenv_no_global():
scheme = get_scheme(dist_name, True)

if check_correct_scheme(scheme, install_dir):
return scheme

raise RuntimeError("unable to determine current installation scheme")

Expand Down

0 comments on commit ed84f1a

Please sign in to comment.