Skip to content

Commit

Permalink
python@3.8: use altinstall target
Browse files Browse the repository at this point in the history
This is a backport of Homebrew#107517 to `python@3.8`.
  • Loading branch information
carlocab committed Aug 11, 2022
1 parent 8fd7e73 commit cc04394
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions Formula/python@3.8.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class PythonAT38 < Formula
# build packages later. Xcode-only systems need different flags.
pour_bottle? only_if: :clt_installed

keg_only :versioned_formula

depends_on "pkg-config" => :build
depends_on "gdbm"
depends_on "mpdecimal"
Expand Down Expand Up @@ -185,8 +183,12 @@ def install
system "make"

ENV.deparallelize do
# The `altinstall` target prevents the installation of files with only Python's major
# version in its name. This allows us to link multiple versioned Python formulae.
# https://github.com/python/cpython#installing-multiple-versions
#
# Tell Python not to install into /Applications (default for framework builds)
system "make", "install", "PYTHONAPPSDIR=#{prefix}"
system "make", "altinstall", "PYTHONAPPSDIR=#{prefix}"
system "make", "frameworkinstallextras", "PYTHONAPPSDIR=#{pkgshare}" if OS.mac?
end

Expand All @@ -209,6 +211,9 @@ def install
inreplace Dir[lib_cellar/"**/_sysconfigdata__darwin_darwin.py"],
%r{('LINKFORSHARED': .*?)'(Python.framework/Versions/3.\d+/Python)'}m,
"\\1'#{opt_prefix}/Frameworks/\\2'"

# Remove symlinks that conflict with the main Python formula.
rm %w[Headers Python Resources Versions/Current].map { |subdir| frameworks/"Python.framework"/subdir }
else
# Prevent third-party packages from building against fragile Cellar paths
inreplace Dir[lib_cellar/"**/_sysconfigdata_*linux_x86_64-*.py",
Expand All @@ -220,6 +225,9 @@ def install
inreplace bin/"python#{version.major_minor}-config",
'prefix_real=$(installed_prefix "$0")',
"prefix_real=#{opt_prefix}"

# Remove symlinks that conflict with the main Python formula.
rm lib/"libpython3.so"
end

# Symlink the pkgconfig files into HOMEBREW_PREFIX so they're accessible.
Expand All @@ -237,14 +245,18 @@ def install
rm libexec/"wheel/tox.ini"
rm_r libexec/"wheel/tests"

# Install unversioned symlinks in libexec/bin.
# Install unversioned and major-versioned symlinks in libexec/bin.
{
"idle" => "idle3",
"pydoc" => "pydoc3",
"python" => "python3",
"python-config" => "python3-config",
}.each do |unversioned_name, versioned_name|
(libexec/"bin").install_symlink (bin/versioned_name).realpath => unversioned_name
"idle" => "idle#{version.major_minor}",
"idle3" => "idle#{version.major_minor}",
"pydoc" => "pydoc#{version.major_minor}",
"pydoc3" => "pydoc#{version.major_minor}",
"python" => "python#{version.major_minor}",
"python3" => "python#{version.major_minor}",
"python-config" => "python#{version.major_minor}-config",
"python3-config" => "python#{version.major_minor}-config",
}.each do |short_name, long_name|
(libexec/"bin").install_symlink (bin/long_name).realpath => short_name
end
end

Expand Down Expand Up @@ -275,23 +287,30 @@ def post_install

%w[setuptools pip wheel].each do |pkg|
(libexec/pkg).cd do
system bin/"python3", "-s", "setup.py", "--no-user-cfg", "install",
system bin/"python#{version.major_minor}", "-s", "setup.py", "--no-user-cfg", "install",
"--force", "--verbose", "--install-scripts=#{bin}",
"--install-lib=#{site_packages}",
"--single-version-externally-managed",
"--record=installed.txt"
end
end

rm_rf [bin/"pip", bin/"easy_install"]
mv bin/"wheel", bin/"wheel3"
rm_rf bin.glob("{easy_install,pip{,3}}")
mv bin/"wheel", bin/"wheel#{version.major_minor}"

# Install unversioned symlinks in libexec/bin.
# Install unversioned and major-versioned symlinks in libexec/bin.
{
"pip" => "pip3",
"wheel" => "wheel3",
}.each do |unversioned_name, versioned_name|
(libexec/"bin").install_symlink (bin/versioned_name).realpath => unversioned_name
"pip" => "pip#{version.major_minor}",
"pip3" => "pip#{version.major_minor}",
"wheel" => "wheel#{version.major_minor}",
"wheel3" => "wheel#{version.major_minor}",
}.each do |short_name, long_name|
(libexec/"bin").install_symlink (bin/long_name).realpath => short_name
end

# post_install happens after link
%W[wheel#{version.major_minor} pip#{version.major_minor}].each do |e|
(HOMEBREW_PREFIX/"bin").install_symlink bin/e
end

# Help distutils find brewed stuff when building extensions
Expand Down Expand Up @@ -350,17 +369,20 @@ def sitecustomize
def caveats
<<~EOS
Python has been installed as
#{opt_bin}/python3
#{HOMEBREW_PREFIX}/bin/python#{version.major_minor}
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
Unversioned and major-versioned symlinks `python`, `python3`, `python-config`, `python3-config`, `pip`, `pip3`, etc. pointing to
`python#{version.major_minor}`, `python#{version.major_minor}-config`, `pip#{version.major_minor}`, etc., respectively, have been installed into
#{opt_libexec}/bin
You can install Python packages with
#{opt_bin}/pip3 install <package>
pip#{version.major_minor} install <package>
They will install into the site-package directory
#{HOMEBREW_PREFIX/"lib/python#{version.major_minor}/site-packages"}
If you do not need a specific version of Python, and always want Homebrew's `python3` in your PATH:
brew install python3
See: https://docs.brew.sh/Homebrew-and-Python
EOS
end
Expand All @@ -375,7 +397,7 @@ def caveats
system "#{bin}/python#{version.major_minor}", "-c", "import zlib"
system "#{bin}/python#{version.major_minor}", "-c", "import tkinter; root = tkinter.Tk()" if OS.mac?

system bin/"pip3", "list", "--format=columns"
system bin/"pip#{version.major_minor}", "list", "--format=columns"

if OS.mac?
assert_match "#{opt_frameworks}/Python.framework/Versions/#{version.major_minor}",
Expand Down

0 comments on commit cc04394

Please sign in to comment.