Skip to content

Commit

Permalink
Prefer usercustomize for readline override
Browse files Browse the repository at this point in the history
This is mostly to support Homebrew Python, which already has a
sitecustomize.py in the version-specific Cellar directory. This
directory, however, is not returned by `site.getsitepackages()` so
the override goes nowhere.

Furthermore, the sitecustomize.py is replaced with every patch release
of Homebrew Python (e.g. going from 3.11.3 to 3.11.4), requiring a new
override invocation even though the `gnureadline` package has not been
reinstalled. This is mildly irritating.

Make two changes:
  - Prefer usercustomize to sitecustomize. This override is valid for
    a minor release of any Python, making it more convenient.
  - If you do pick sitecustomize, import the module and use its actual
  `__file__` location to avoid getting the run-around.

It is easy to target sitecustomize instead by simply adding the -s flag;
i.e. `python -s -m override_readline`. No need for fancy argparsing :-)
  • Loading branch information
ludwigschwardt committed Jun 18, 2023
1 parent 1cf825e commit a39b669
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions override_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def check_module(module_name):
return module


def install_override(site_directory, customize_filename):
def install_override(customize_path):
"""Add override to specified customization module and report back."""
site_directory, customize_filename = os.path.split(customize_path)
banner = "\nAdd override to {filename}\n--------------------------------\n"
print(banner.format(filename=customize_filename))
if not os.path.exists(site_directory):
os.makedirs(site_directory)
print("Created site directory at {dir}".format(dir=site_directory))
customize_path = os.path.join(site_directory, customize_filename)
file_mode = "r+t" if os.path.exists(customize_path) else "w+t"
with io.open(customize_path, file_mode) as customize_file:
if file_mode == "w+t":
Expand All @@ -91,20 +91,51 @@ def install_override(site_directory, customize_filename):
print("(It is also pretty harmless to leave it in there...)")


def override_usercustomize():
if not site.ENABLE_USER_SITE:
print(
"Could not override usercustomize.py because user site "
"directory is not enabled (maybe you are in a virtualenv?)"
)
return False
try:
import usercustomize
except ImportError:
path = os.path.join(site.getusersitepackages(), "usercustomize.py")
else:
path = usercustomize.__file__
try:
install_override(path)
except OSError:
print("Could not override usercustomize.py because file open/write failed")
return False
else:
return True


def override_sitecustomize():
try:
import sitecustomize
except ImportError:
path = os.path.join(site.getsitepackages()[0], "sitecustomize.py")
else:
path = sitecustomize.__file__
try:
install_override(path)
except OSError:
print("Could not override sitecustomize.py because file open/write failed")
return False
else:
return True


print(HEADER)
readline = check_module("readline")
gnureadline = check_module("gnureadline")
if not gnureadline:
raise RuntimeError("Please install gnureadline first")
if readline == gnureadline:
print("It looks like readline is already overridden, but let's make sure")
try:
install_override(site.getsitepackages()[0], "sitecustomize.py")
except OSError:
print("Defaulting to user installation because normal site-packages is not writeable")
if not site.ENABLE_USER_SITE:
raise RuntimeError(
"Please enable user site directory, otherwise override won't work "
"(see site.ENABLE_USER_SITE documentation for details)"
)
install_override(site.getusersitepackages(), "usercustomize.py")

if not override_usercustomize():
override_sitecustomize()

0 comments on commit a39b669

Please sign in to comment.