Files
2026-07-06 15:42:05 -07:00

537 lines
24 KiB
Ruby

# frozen_string_literal: true
# Generated by generate_tap.py for local/mirror
class PythonAT312 < Formula
desc "Interpreted, interactive, object-oriented programming language"
homepage "https://www.python.org/"
url "https://homebrew.hyprnet/sources/cfa99215f876a1a0e713cdffb92f478801ba165f707dd1ebb1f7e03069065d54--python@3.12--3.12.13_4.x86_64_linux.bottle.1.tar.gz"
sha256 "0816c4761c97ecdb3f50a3924de0a93fd78cb63ee8e6c04201ddfaedca500b0b"
license "Python-2.0"
revision 4
compatibility_version 1
livecheck do
url "https://www.python.org/downloads/source/"
regex(%r{href=.*?/Python[._-]v?(3\.12(?:\.\d+)*)\.t}i)
end
bottle do
root_url "https://homebrew.hyprnet/bottles"
rebuild 1
sha256 arm64_tahoe: "4b2cf1174957cdfb7f95dacb9135ef47c57a7cc52f2ac590a9f7284cdfea0e63"
sha256 arm64_sequoia: "4f06f0e4e2b57b84574e69acb259a999cea8973aab52dff12efd7362edf8fa23"
sha256 arm64_sonoma: "d9028cedd1255cff9178ce3d999a32da15d56c297367c9f7c51d5f913bdfce1d"
sha256 tahoe: "3c17a3059af9efd973bfa165ab137a6918df1e03de0bde427ef8d192064d5277"
sha256 sequoia: "afc9e76b23fb712f8cec447fdf2d6e470e064cb0470d82a4b3eac6f367b84666"
sha256 sonoma: "552d3ad743dbce80ef456f042c7d58297fa05a5e46e4cd2fe0bd2f3ef305cfb0"
sha256 arm64_linux: "2f6ffdbeb1c72207efa2644c2ac26ac11f1ab8b514165cba67c9a89c48ba54cb"
sha256 x86_64_linux: "6d8adc5a6ad3c7d0085c87892a2ffb571e71739d5199b87413bb4db046eed266"
end
depends_on "pkgconf" => :build
depends_on "mpdecimal"
depends_on "openssl@3"
depends_on "sqlite"
depends_on "xz"
uses_from_macos "bzip2"
uses_from_macos "expat", since: :sequoia
uses_from_macos "libedit"
uses_from_macos "libffi"
uses_from_macos "libxcrypt"
uses_from_macos "ncurses"
on_linux do
depends_on "libnsl"
depends_on "libtirpc"
depends_on "zlib-ng-compat"
end
link_overwrite "lib/python3.12/site-packages/pip*"
link_overwrite "lib/python3.12/site-packages/wheel*"
pypi_packages package_name: "",
extra_packages: %w[flit-core pip wheel]
resource "flit-core" do
url "https://files.pythonhosted.org/packages/69/59/b6fc2188dfc7ea4f936cd12b49d707f66a1cb7a1d2c16172963534db741b/flit_core-3.12.0.tar.gz"
sha256 "18f63100d6f94385c6ed57a72073443e1a71a4acb4339491615d0f16d6ff01b2"
end
resource "packaging" do
url "https://files.pythonhosted.org/packages/d7/f1/e7a6dd94a8d4a5626c03e4e99c87f241ba9e350cd9e6d75123f992427270/packaging-26.2.tar.gz"
sha256 "ff452ff5a3e828ce110190feff1178bb1f2ea2281fa2075aadb987c2fb221661"
end
resource "pip" do
url "https://files.pythonhosted.org/packages/01/91/47e7d486260f618783899587af63ccf7980fb60245c3e63dd4571c6b57ad/pip-26.1.2.tar.gz"
sha256 "f49cd134c61cf2fd75e0ce2676db03e4054504a5a4986d00f8299ae632dc4605"
end
resource "wheel" do
url "https://files.pythonhosted.org/packages/39/62/75f18a0f03b4219c456652c7780e4d749b929eb605c098ce3a5b6b6bc081/wheel-0.47.0.tar.gz"
sha256 "cc72bd1009ba0cf63922e28f94d9d83b920aa2bb28f798a31d0691b02fa3c9b3"
end
# Modify default sysconfig to match the brew install layout.
# Remove when a non-patching mechanism is added (https://bugs.python.org/issue43976).
# We (ab)use osx_framework_library to exploit pip behaviour to allow --prefix to still work.
patch do
file "Patches/python/3.11-sysconfig.diff"
end
def lib_cellar
on_macos do
return frameworks/"Python.framework/Versions"/version.major_minor/"lib/python#{version.major_minor}"
end
on_linux do
return lib/"python#{version.major_minor}"
end
end
def site_packages_cellar = lib_cellar/"site-packages"
# The HOMEBREW_PREFIX location of site-packages.
def site_packages = HOMEBREW_PREFIX/"lib/python#{version.major_minor}/site-packages"
def altinstall? = name != Formula["python3"].name
def python3 = bin/"python#{version.major_minor}"
def install
# Unset these so that installing pip and setuptools puts them where we want
# and not into some other Python the user has installed.
ENV["PYTHONHOME"] = nil
ENV["PYTHONPATH"] = nil
# Override the auto-detection of libmpdec, which assumes a universal build.
# This is currently an inreplace due to https://github.com/python/cpython/issues/98557.
if OS.mac?
inreplace "configure", "libmpdec_machine=universal",
"libmpdec_machine=#{ENV["PYTHON_DECIMAL_WITH_MACHINE"] = Hardware::CPU.arm? ? "uint128" : "x64"}"
end
# The --enable-optimization and --with-lto flags diverge from what upstream
# python does for their macOS binary releases. They have chosen not to apply
# these flags because they want one build that will work across many macOS
# releases. Homebrew is not so constrained because the bottling
# infrastructure specializes for each macOS major release.
args = %W[
--prefix=#{prefix}
--enable-ipv6
--datarootdir=#{share}
--datadir=#{share}
--without-ensurepip
--enable-loadable-sqlite-extensions
--with-openssl=#{formula_opt_prefix("openssl@3")}
--enable-optimizations
--with-system-expat
--with-system-libmpdec
--with-readline=editline
]
# Python re-uses flags when building native modules.
# Since we don't want native modules prioritizing the brew
# include path, we move them to [C|LD]FLAGS_NODIST.
# Note: Changing CPPFLAGS causes issues with dbm, so we
# leave it as-is.
cflags = []
cflags_nodist = ["-I#{HOMEBREW_PREFIX}/include"]
ldflags = []
ldflags_nodist = ["-L#{HOMEBREW_PREFIX}/lib", "-Wl,-rpath,#{HOMEBREW_PREFIX}/lib"]
cppflags = ["-I#{HOMEBREW_PREFIX}/include"]
if OS.mac?
# Enabling LTO on Linux makes libpython3.*.a unusable for anyone whose GCC
# install does not match the one in CI _exactly_ (major and minor version).
# https://github.com/orgs/Homebrew/discussions/3734
args << "--with-lto"
args << "--enable-framework=#{frameworks}"
args << "--with-dtrace"
args << "--with-dbmliborder=ndbm"
# Avoid linking to libgcc https://mail.python.org/pipermail/python-dev/2012-February/116205.html
args << "MACOSX_DEPLOYMENT_TARGET=#{MacOS.version}"
else
args << "--enable-shared"
args << "--with-dbmliborder="
end
if OS.linux?
# Python's configure adds the system ncurses include entry to CPPFLAGS
# when doing curses header check. The check may fail when there exists
# a 32-bit system ncurses (conflicts with the brewed 64-bit one).
# See https://github.com/Homebrew/linuxbrew-core/pull/22307#issuecomment-781896552
# We want our ncurses! Override system ncurses includes!
inreplace "configure", 'CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw"',
"CPPFLAGS=\"$CPPFLAGS -I#{formula_opt_include("ncurses")}\""
end
# Allow python modules to use ctypes.find_library to find homebrew's stuff
# even if homebrew is not a /usr/local/lib. Try this with:
# `brew install enchant && pip install pyenchant`
inreplace "./Lib/ctypes/macholib/dyld.py" do |f|
f.gsub! "DEFAULT_LIBRARY_FALLBACK = [",
"DEFAULT_LIBRARY_FALLBACK = [ '#{HOMEBREW_PREFIX}/lib', '#{formula_opt_lib("openssl@3")}',"
f.gsub! "DEFAULT_FRAMEWORK_FALLBACK = [", "DEFAULT_FRAMEWORK_FALLBACK = [ '#{HOMEBREW_PREFIX}/Frameworks',"
end
args << "CFLAGS=#{cflags.join(" ")}" unless cflags.empty?
args << "CFLAGS_NODIST=#{cflags_nodist.join(" ")}" unless cflags_nodist.empty?
args << "LDFLAGS=#{ldflags.join(" ")}" unless ldflags.empty?
args << "LDFLAGS_NODIST=#{ldflags_nodist.join(" ")}" unless ldflags_nodist.empty?
args << "CPPFLAGS=#{cppflags.join(" ")}" unless cppflags.empty?
# Disabled modules - provided in separate formulae
args += %w[
py_cv_module__tkinter=n/a
]
system "./configure", *args
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)
target = "#{"alt" if altinstall?}install"
system "make", target, "PYTHONAPPSDIR=#{prefix}"
system "make", "frameworkinstallextras", "PYTHONAPPSDIR=#{pkgshare}" if OS.mac?
end
if OS.mac?
# Any .app get a " 3" attached, so it does not conflict with python 2.x.
prefix.glob("*.app") { |app| mv app, app.to_s.sub(/\.app$/, " 3.app") }
pc_dir = frameworks/"Python.framework/Versions"/version.major_minor/"lib/pkgconfig"
# Symlink the pkgconfig files into HOMEBREW_PREFIX so they're accessible.
(lib/"pkgconfig").install_symlink pc_dir.children
# Prevent third-party packages from building against fragile Cellar paths
bad_cellar_path_files = [
lib_cellar/"_sysconfigdata__darwin_darwin.py",
lib_cellar/"config-#{version.major_minor}-darwin/Makefile",
pc_dir/"python-#{version.major_minor}.pc",
pc_dir/"python-#{version.major_minor}-embed.pc",
]
inreplace bad_cellar_path_files, prefix, opt_prefix
# Help third-party packages find the Python framework
inreplace lib_cellar/"config-#{version.major_minor}-darwin/Makefile",
/^LINKFORSHARED=(.*)PYTHONFRAMEWORKDIR(.*)/,
"LINKFORSHARED=\\1PYTHONFRAMEWORKINSTALLDIR\\2"
# Symlink the pkgconfig files into HOMEBREW_PREFIX so they're accessible.
(lib/"pkgconfig").install_symlink pc_dir.children
# Fix for https://github.com/Homebrew/homebrew-core/issues/21212
inreplace lib_cellar/"_sysconfigdata__darwin_darwin.py",
%r{('LINKFORSHARED': .*?)'(Python.framework/Versions/3.\d+/Python)'}m,
"\\1'#{opt_prefix}/Frameworks/\\2'"
# For an altinstall, remove symlinks that conflict with the main Python formula.
rm frameworks.glob("Python.framework/{Headers,Python,Resources,Versions/Current}") if altinstall?
else
# Prevent third-party packages from building against fragile Cellar paths
inreplace Dir[lib_cellar/"**/_sysconfigdata_*linux_x86_64-*.py",
lib_cellar/"config*/Makefile",
bin/"python#{version.major_minor}-config",
lib/"pkgconfig/python-3*.pc"],
prefix, opt_prefix
inreplace bin/"python#{version.major_minor}-config",
'prefix_real=$(installed_prefix "$0")',
"prefix_real=#{opt_prefix}"
# For an altinstall, remove symlinks that conflict with the main Python formula.
rm lib/"libpython3.so" if altinstall?
end
# Remove the site-packages that Python created in its Cellar.
rm_r site_packages_cellar.children
# Prepare a wheel of wheel to install later.
common_pip_args = %w[
-v
--no-deps
--no-binary :all:
--no-index
--no-build-isolation
]
whl_build = buildpath/"whl_build"
system python3, "-m", "venv", whl_build
%w[flit-core wheel].each do |r|
resource(r).stage do
system whl_build/"bin/pip3", "install", *common_pip_args, "."
end
end
resource("wheel").stage do
system whl_build/"bin/pip3", "wheel", *common_pip_args,
"--wheel-dir=#{libexec}",
"."
end
# Replace bundled pip with our own.
rm lib_cellar.glob("ensurepip/_bundled/pip-*.whl")
resource("pip").stage do
system whl_build/"bin/pip3", "wheel", *common_pip_args,
"--wheel-dir=#{lib_cellar}/ensurepip/_bundled",
"."
end
# Patch ensurepip to bootstrap our updated version of pip
inreplace lib_cellar/"ensurepip/__init__.py" do |s|
s.gsub!(/_PIP_VERSION = .*/, "_PIP_VERSION = \"#{resource("pip").version}\"")
end
# Ensure that our new pip wheel is globally readable.
chmod "ugo+r", lib_cellar.glob("ensurepip/_bundled/pip-*.whl")
# Install unversioned (and for an altinstall, major-versioned) symlinks in libexec/bin.
{
"idle" => "idle#{version.major_minor}",
"pydoc" => "pydoc#{version.major_minor}",
"python" => "python#{version.major_minor}",
"python-config" => "python#{version.major_minor}-config",
}.merge(altinstall? ? {
"idle3" => "idle#{version.major_minor}",
"pydoc3" => "pydoc#{version.major_minor}",
"python3" => "python#{version.major_minor}",
"python3-config" => "python#{version.major_minor}-config",
} : {}).each do |short_name, long_name|
(libexec/"bin").install_symlink (bin/long_name).realpath => short_name
end
# Bootstrap initial install of pip.
system python3, "-Im", "ensurepip"
# Install desired versions of pip, wheel using the version of
# pip bootstrapped by ensurepip.
# Note that while we replaced the ensurepip wheels, there's no guarantee
# ensurepip actually used them, since other existing installations could
# have been picked up (and we can't pass --ignore-installed).
root_site_packages = lib/"python#{version.major_minor}/site-packages"
bundled = lib_cellar/"ensurepip/_bundled"
system python3, "-Im", "pip", "install", "-v",
"--no-deps",
"--no-index",
"--upgrade",
"--isolated",
"--target=#{root_site_packages}",
bundled/"pip-#{resource("pip").version}-py3-none-any.whl",
libexec/"wheel-#{resource("wheel").version}-py3-none-any.whl"
# pip install with --target flag will just place the bin folder into the
# target, so move its contents into the appropriate location
mv (root_site_packages/"bin").children, bin
rmdir root_site_packages/"bin"
rm bin/"pip"
rm bin/"pip3" if altinstall?
mv bin/"wheel", bin/"wheel#{version.major_minor}"
bin.install_symlink "wheel#{version.major_minor}" => "wheel3" unless altinstall?
# Install unversioned (and for an altinstall, major-versioned) symlinks in libexec/bin.
{
"pip" => "pip#{version.major_minor}",
"wheel" => "wheel#{version.major_minor}",
}.merge(altinstall? ? {
"pip3" => "pip#{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
if OS.mac?
# Replace framework site-packages with a symlink to the real one.
rm_r site_packages_cellar
site_packages_cellar.parent.install_symlink root_site_packages
end
# Write out sitecustomize.py
(lib_cellar/"sitecustomize.py").atomic_write(sitecustomize)
# Mark Homebrew python as externally managed: https://peps.python.org/pep-0668/#marking-an-interpreter-as-using-an-external-package-manager
# Placed after ensurepip since it invokes pip in isolated mode, meaning
# we can't pass --break-system-packages.
(lib_cellar/"EXTERNALLY-MANAGED").write <<~INI
[externally-managed]
Error=To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.
If you wish to install a Python library that isn't in Homebrew,
use a virtual environment:
python3 -m venv path/to/venv
source path/to/venv/bin/activate
python3 -m pip install xyz
If you wish to install a Python application that isn't in Homebrew,
it may be easiest to use 'pipx install xyz', which will manage a
virtual environment for you. You can install pipx with
brew install pipx
You may restore the old behavior of pip by passing
the '--break-system-packages' flag to pip, or by adding
'break-system-packages = true' to your pip.conf file. The latter
will permanently disable this error.
If you disable this error, we STRONGLY recommend that you additionally
pass the '--user' flag to pip, or set 'user = true' in your pip.conf
file. Failure to do this can result in a broken Homebrew installation.
Read more about this behavior here: <https://peps.python.org/pep-0668/>
INI
end
def sitecustomize
<<~PYTHON
# This file is created by Homebrew and is executed on each python startup.
# Don't print from here, or else python command line scripts may fail!
# <https://docs.brew.sh/Homebrew-and-Python>
import re
import os
import site
import sys
if sys.version_info[:2] != (#{version.major}, #{version.minor}):
# This can only happen if the user has set the PYTHONPATH to a mismatching site-packages directory.
# Every Python looks at the PYTHONPATH variable and we can't fix it here in sitecustomize.py,
# because the PYTHONPATH is evaluated after the sitecustomize.py. Many modules (e.g. PyQt4) are
# built only for a specific version of Python and will fail with cryptic error messages.
# In the end this means: Don't set the PYTHONPATH permanently if you use different Python versions.
exit(f'Your PYTHONPATH points to a site-packages dir for Python #{version.major_minor} '
f'but you are running Python {sys.version_info[0]}.{sys.version_info[1]}!\\n'
f' PYTHONPATH is currently: "{os.environ["PYTHONPATH"]}"\\n'
f' You should `unset PYTHONPATH` to fix this.')
# Only do this for a brewed python:
if os.path.realpath(sys.executable).startswith('#{rack}'):
# Shuffle /Library site-packages to the end of sys.path
library_site = '/Library/Python/#{version.major_minor}/site-packages'
library_packages = [p for p in sys.path if p.startswith(library_site)]
sys.path = [p for p in sys.path if not p.startswith(library_site)]
# .pth files have already been processed so don't use addsitedir
sys.path.extend(library_packages)
# the Cellar site-packages is a symlink to the HOMEBREW_PREFIX
# site_packages; prefer the shorter paths
long_prefix = re.compile(r'#{rack}/[0-9\\._abrc]+/(?:Frameworks/Python\\.framework/Versions/#{version.major_minor}/)?lib/python#{version.major_minor}/site-packages')
sys.path = [long_prefix.sub('#{site_packages}', p) for p in sys.path]
# Set the sys.executable to use the opt_prefix. Only do this if PYTHONEXECUTABLE is not
# explicitly set and we are not in a virtualenv:
if 'PYTHONEXECUTABLE' not in os.environ and sys.prefix == sys.base_prefix:
sys.executable = sys._base_executable = '#{opt_bin}/python#{version.major_minor}'
if 'PYTHONHOME' not in os.environ:
cellar_prefix = re.compile(r'#{rack}/[0-9\\._abrc]+(?=/|$)')
if os.path.realpath(sys.base_prefix).startswith('#{rack}'):
new_prefix = cellar_prefix.sub('#{opt_prefix}', sys.base_prefix)
site.PREFIXES[:] = [new_prefix if x == sys.base_prefix else x for x in site.PREFIXES]
if sys.prefix == sys.base_prefix:
sys.prefix = new_prefix
sys.base_prefix = new_prefix
if os.path.realpath(sys.base_exec_prefix).startswith('#{rack}'):
new_exec_prefix = cellar_prefix.sub('#{opt_prefix}', sys.base_exec_prefix)
site.PREFIXES[:] = [new_exec_prefix if x == sys.base_exec_prefix else x for x in site.PREFIXES]
if sys.exec_prefix == sys.base_exec_prefix:
sys.exec_prefix = new_exec_prefix
sys.base_exec_prefix = new_exec_prefix
# Make site.getsitepackages() return the HOMEBREW_PREFIX site-packages,
# but only if we are outside a venv or in one with include-system-site-packages.
if sys.base_prefix in site.PREFIXES:
site.PREFIXES.insert(site.PREFIXES.index(sys.base_prefix), '#{HOMEBREW_PREFIX}')
site.addsitedir('#{site_packages}')
# Check for and add the prefix of split Python formulae.
for split_module in ["tk", "gdbm"]:
split_prefix = f"#{HOMEBREW_PREFIX}/opt/python-{split_module}@#{version.major_minor}/libexec"
if os.path.isdir(split_prefix):
sys.path.append(split_prefix)
PYTHON
end
def caveats
dbm_is = "`dbm.gnu` is"
on_linux do
dbm_is = "`dbm.gnu` and `dbm.ndbm` are"
end
<<~EOS
Python is installed as
#{HOMEBREW_PREFIX}/bin/python#{version.major_minor}
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, are installed into
#{opt_libexec}/bin
If you do not need a specific version of Python, and always want Homebrew's `python3` in your PATH:
brew install python3
`idle#{version.major_minor}` requires tkinter, which is available separately:
brew install python-tk@#{version.major_minor}
#{dbm_is} available separately:
brew install python-gdbm@#{version.major_minor}
For more information about Homebrew and Python, see: https://docs.brew.sh/Homebrew-and-Python
EOS
end
test do
# Check if sqlite is ok, because we build with --enable-loadable-sqlite-extensions
# and it can occur that building sqlite silently fails if OSX's sqlite is used.
system python3, "-c", "import sqlite3"
# check to see if we can create a venv
system python3, "-m", "venv", testpath/"myvenv"
# Check if some other modules import. Then the linked libs are working.
system python3, "-c", "import _ctypes"
system python3, "-c", "import _decimal"
system python3, "-c", "import pyexpat"
system python3, "-c", "import readline"
system python3, "-c", "import zlib"
# tkinter is provided in a separate formula
assert_match "ModuleNotFoundError: No module named '_tkinter'",
shell_output("#{python3} -Sc 'import tkinter' 2>&1", 1)
# gdbm is provided in a separate formula
assert_match "ModuleNotFoundError: No module named '_gdbm'",
shell_output("#{python3} -Sc 'import _gdbm' 2>&1", 1)
assert_match "ModuleNotFoundError: No module named '_gdbm'",
shell_output("#{python3} -Sc 'import dbm.gnu' 2>&1", 1)
# Verify that the selected DBM interface works on macOS.
# Linux requires installing python-gdbm formula
(testpath/"dbm_test.py").write <<~PYTHON
import dbm
with dbm.ndbm.open("test", "c") as db:
db[b"foo \\xbd"] = b"bar \\xbd"
with dbm.ndbm.open("test", "r") as db:
assert list(db.keys()) == [b"foo \\xbd"]
assert b"foo \\xbd" in db
assert db[b"foo \\xbd"] == b"bar \\xbd"
PYTHON
system python3, "dbm_test.py" if OS.mac?
system bin/"pip#{version.major_minor}", "list", "--format=columns"
# Check our sitecustomize.py
assert_match HOMEBREW_CELLAR.to_s, shell_output("#{python3} -Sc 'import sys; print(sys.base_prefix)'")
refute_match HOMEBREW_CELLAR.to_s, shell_output("#{python3} -c 'import sys; print(sys.base_prefix)'")
refute_match site_packages_cellar.to_s, shell_output("#{python3} -c 'import sys; print(sys.path)'")
# Verify our sysconfig patches
sysconfig_path = "import sysconfig; print(sysconfig.get_paths(\"osx_framework_library\")[\"data\"])"
assert_equal HOMEBREW_PREFIX.to_s, shell_output("#{python3} -c '#{sysconfig_path}'").strip
linkforshared_var = "import sysconfig; print(sysconfig.get_config_var(\"LINKFORSHARED\"))"
assert_match opt_prefix.to_s, shell_output("#{python3} -c '#{linkforshared_var}'") if OS.mac?
# Check our externally managed marker
assert_match "If you wish to install a Python library",
shell_output("#{python3} -m pip install pip 2>&1", 1)
end
end