Skip to content

Commit c9ad5a5

Browse files
authored
Merge pull request #251186 from Homebrew/python-setuptool-symlink
python-setuptools: build once and symlink
2 parents 46322b0 + 6a1349d commit c9ad5a5

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

Formula/p/python-setuptools.rb

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,51 @@ class PythonSetuptools < Formula
77
revision 1
88

99
bottle do
10-
sha256 cellar: :any_skip_relocation, all: "90d1373d4b921632e3da896be98326b463867c0a4f9e742c6a98c09caae96fd7"
10+
rebuild 1
11+
sha256 cellar: :any_skip_relocation, all: "3f1216774825c551075a7a28ca8c7412e56ddb635f495275e5c231801d3a5abd"
1112
end
1213

13-
depends_on "[email protected]" => [:build, :test]
14-
depends_on "[email protected]" => [:build, :test]
1514
depends_on "[email protected]" => [:build, :test]
15+
depends_on "[email protected]" => :test # keep on oldest python to support (externally managed and not EOL)
1616

1717
def pythons
18-
deps.map(&:to_formula)
19-
.select { |f| f.name.match?(/^python@\d\.\d+$/) }
20-
.map { |f| f.opt_libexec/"bin/python" }
18+
deps.filter_map { |dep| dep.to_formula if dep.name.start_with?("python@") }
2119
end
2220

2321
def install
24-
inreplace_paths = %w[
25-
_distutils/compilers/C/unix.py
26-
_vendor/platformdirs/unix.py
27-
]
28-
29-
pythons.each do |python|
30-
system python, "-m", "pip", "install", *std_pip_args, "."
31-
32-
# Ensure uniform bottles
33-
setuptools_site_packages = prefix/Language::Python.site_packages(python)/"setuptools"
34-
inreplace setuptools_site_packages/"_vendor/platformdirs/macos.py", "/opt/homebrew", HOMEBREW_PREFIX
35-
36-
inreplace_files = inreplace_paths.map { |file| setuptools_site_packages/file }
37-
inreplace_files += setuptools_site_packages.glob("_vendor/platformdirs-*dist-info/METADATA")
38-
inreplace inreplace_files, "/usr/local", HOMEBREW_PREFIX
22+
odie "Need exactly 2 python dependencies!" if pythons.count != 2
23+
oldest_python, python = pythons.sort_by(&:version)
24+
python_exe = python.opt_libexec/"bin/python"
25+
system python_exe, "-m", "pip", "install", *std_pip_args, "."
26+
27+
# Pure python setuptools installation can be used on different Python versions
28+
site_packages = prefix/Language::Python.site_packages(python_exe)
29+
python.versioned_formulae.each do |extra_python|
30+
next if extra_python.version < oldest_python.version
31+
32+
# Cannot use Python.site_packages as that requires formula to be installed
33+
extra_site_packages = lib/"python#{extra_python.version.major_minor}/site-packages"
34+
site_packages.find do |path|
35+
next unless path.file?
36+
37+
target = extra_site_packages/path.relative_path_from(site_packages)
38+
target.dirname.install_symlink path
39+
end
3940
end
41+
42+
# Ensure uniform bottles
43+
setuptools_site_packages = site_packages/"setuptools"
44+
inreplace_files = %W[
45+
#{setuptools_site_packages}/_distutils/compilers/C/unix.py
46+
#{setuptools_site_packages}/_vendor/platformdirs/unix.py
47+
] + setuptools_site_packages.glob("_vendor/platformdirs-*dist-info/METADATA")
48+
inreplace inreplace_files, "/usr/local", HOMEBREW_PREFIX
49+
inreplace setuptools_site_packages/"_vendor/platformdirs/macos.py", "/opt/homebrew", HOMEBREW_PREFIX
4050
end
4151

4252
test do
4353
pythons.each do |python|
44-
system python, "-c", "import setuptools"
54+
system python.opt_libexec/"bin/python", "-c", "import setuptools"
4555
end
4656
end
4757
end

0 commit comments

Comments
 (0)