language/python: support pypy

This commit is contained in:
Jonathan Chang 2018-09-05 23:01:52 -04:00
parent 899bfbcaf1
commit 0fe273be5f

View File

@ -9,13 +9,21 @@ module Language
Version.create(version.to_s) Version.create(version.to_s)
end end
def self.homebrew_site_packages(version = "3.7") def self.homebrew_site_packages(python = "python3.7")
HOMEBREW_PREFIX/"lib/python#{version}/site-packages" HOMEBREW_PREFIX/site_packages(python)
end
def self.site_packages(python = "python3.7")
if python == "pypy"
"site-packages"
else
"lib/python#{major_minor_version python}/site-packages"
end
end end
def self.each_python(build, &block) def self.each_python(build, &block)
original_pythonpath = ENV["PYTHONPATH"] original_pythonpath = ENV["PYTHONPATH"]
{ "python@3" => "python3", "python@2" => "python2.7" }.each do |python_formula, python| { "python@3" => "python3", "python@2" => "python2.7", "pypy" => "pypy" }.each do |python_formula, python|
python_formula = Formulary.factory(python_formula) python_formula = Formulary.factory(python_formula)
next if build.without? python_formula.to_s next if build.without? python_formula.to_s
@ -23,7 +31,7 @@ module Language
ENV["PYTHONPATH"] = if python_formula.installed? ENV["PYTHONPATH"] = if python_formula.installed?
nil nil
else else
homebrew_site_packages(version) homebrew_site_packages(python)
end end
block&.call python, version block&.call python, version
end end
@ -31,11 +39,10 @@ module Language
end end
def self.reads_brewed_pth_files?(python) def self.reads_brewed_pth_files?(python)
version = major_minor_version python return unless homebrew_site_packages(python).directory?
return unless homebrew_site_packages(version).directory? return unless homebrew_site_packages(python).writable_real?
return unless homebrew_site_packages(version).writable_real?
probe_file = homebrew_site_packages(version)/"homebrew-pth-probe.pth" probe_file = homebrew_site_packages(python)/"homebrew-pth-probe.pth"
begin begin
probe_file.atomic_write("import site; site.homebrew_was_here = True") probe_file.atomic_write("import site; site.homebrew_was_here = True")
with_homebrew_path { quiet_system python, "-c", "import site; assert(site.homebrew_was_here)" } with_homebrew_path { quiet_system python, "-c", "import site; assert(site.homebrew_was_here)" }
@ -69,6 +76,7 @@ module Language
--no-user-cfg --no-user-cfg
install install
--prefix=#{prefix} --prefix=#{prefix}
--install-scripts=#{prefix}/bin
--single-version-externally-managed --single-version-externally-managed
--record=installed.txt --record=installed.txt
] ]
@ -101,17 +109,16 @@ module Language
# Find any Python bindings provided by recursive dependencies # Find any Python bindings provided by recursive dependencies
formula_deps = formula.recursive_dependencies formula_deps = formula.recursive_dependencies
xy = Language::Python.major_minor_version python
pth_contents = formula_deps.map do |d| pth_contents = formula_deps.map do |d|
next if d.build? next if d.build?
dep_site_packages = Formula[d.name].opt_lib/"python#{xy}/site-packages" dep_site_packages = Formula[d.name].opt_prefix/Language::Python.site_packages(python)
next unless dep_site_packages.exist? next unless dep_site_packages.exist?
"import site; site.addsitedir('#{dep_site_packages}')\n" "import site; site.addsitedir('#{dep_site_packages}')\n"
end.compact end.compact
unless pth_contents.empty? unless pth_contents.empty?
(venv_root/"lib/python#{xy}/site-packages/homebrew_deps.pth").write pth_contents.join (venv_root/Language::Python.site_packages(python)/"homebrew_deps.pth").write pth_contents.join
end end
venv venv
@ -140,7 +147,7 @@ module Language
def virtualenv_install_with_resources(options = {}) def virtualenv_install_with_resources(options = {})
python = options[:using] python = options[:using]
if python.nil? if python.nil?
wanted = %w[python python@2 python2 python3 python@3].select { |py| needs_python?(py) } wanted = %w[python python@2 python2 python3 python@3 pypy].select { |py| needs_python?(py) }
raise FormulaAmbiguousPythonError, self if wanted.size > 1 raise FormulaAmbiguousPythonError, self if wanted.size > 1
python = wanted.first || "python2.7" python = wanted.first || "python2.7"
@ -177,9 +184,8 @@ module Language
@formula.resource("homebrew-virtualenv").stage do |stage| @formula.resource("homebrew-virtualenv").stage do |stage|
old_pythonpath = ENV.delete "PYTHONPATH" old_pythonpath = ENV.delete "PYTHONPATH"
begin begin
xy = Language::Python.major_minor_version(@python)
staging = Pathname.new(stage.staging.tmpdir) staging = Pathname.new(stage.staging.tmpdir)
ENV.prepend_create_path "PYTHONPATH", staging/"target/lib/python#{xy}/site-packages" ENV.prepend_create_path "PYTHONPATH", staging/"target"/Language::Python.site_packages(@python)
@formula.system @python, *Language::Python.setup_install_args(staging/"target") @formula.system @python, *Language::Python.setup_install_args(staging/"target")
@formula.system @python, "-s", staging/"target/bin/virtualenv", "-p", @python, @venv_root @formula.system @python, "-s", staging/"target/bin/virtualenv", "-p", @python, @venv_root
ensure ensure