language/python: add helper methods for pth detection
This commit is contained in:
parent
b3838675ec
commit
b584689afa
@ -8,6 +8,10 @@ module Language
|
|||||||
Version.new(version.to_s)
|
Version.new(version.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.homebrew_site_packages(version="2.7")
|
||||||
|
HOMEBREW_PREFIX/"lib/python#{version}/site-packages"
|
||||||
|
end
|
||||||
|
|
||||||
def self.each_python build, &block
|
def self.each_python build, &block
|
||||||
original_pythonpath = ENV["PYTHONPATH"]
|
original_pythonpath = ENV["PYTHONPATH"]
|
||||||
["python", "python3"].each do |python|
|
["python", "python3"].each do |python|
|
||||||
@ -16,11 +20,33 @@ module Language
|
|||||||
ENV["PYTHONPATH"] = if Formulary.factory(python).installed?
|
ENV["PYTHONPATH"] = if Formulary.factory(python).installed?
|
||||||
nil
|
nil
|
||||||
else
|
else
|
||||||
"#{HOMEBREW_PREFIX}/lib/python#{version}/site-packages"
|
homebrew_site_packages(version)
|
||||||
end
|
end
|
||||||
block.call python, version if block
|
block.call python, version if block
|
||||||
end
|
end
|
||||||
ENV["PYTHONPATH"] = original_pythonpath
|
ENV["PYTHONPATH"] = original_pythonpath
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.reads_brewed_pth_files? python
|
||||||
|
version = major_minor_version python
|
||||||
|
return unless homebrew_site_packages(version).directory?
|
||||||
|
probe_file = homebrew_site_packages(version)/"homebrew-pth-probe.pth"
|
||||||
|
probe_file.atomic_write("import site; site.homebrew_was_here = True")
|
||||||
|
result = quiet_system python, "-c", "import site; assert(site.homebrew_was_here)"
|
||||||
|
probe_file.unlink
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.user_site_packages python
|
||||||
|
Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.in_sys_path? python, path
|
||||||
|
script = <<-EOS.undent
|
||||||
|
import os, sys
|
||||||
|
[os.path.realpath(p) for p in sys.path].index(os.path.realpath("#{path}"))
|
||||||
|
EOS
|
||||||
|
quiet_system python, "-c", script
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user