Module: Language::Python

Defined in:
Library/Homebrew/language/python.rb

Defined Under Namespace

Modules: Virtualenv

Class Method Summary collapse

Class Method Details

.each_python(build, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'Library/Homebrew/language/python.rb', line 16

def self.each_python(build, &block)
  original_pythonpath = ENV["PYTHONPATH"]
  ["python", "python3"].each do |python|
    next if build.without? python
    version = major_minor_version python
    ENV["PYTHONPATH"] = if Formulary.factory(python).installed?
      nil
    else
      homebrew_site_packages(version)
    end
    block&.call python, version
  end
  ENV["PYTHONPATH"] = original_pythonpath
end

.homebrew_site_packages(version = "2.7") ⇒ Object



12
13
14
# File 'Library/Homebrew/language/python.rb', line 12

def self.homebrew_site_packages(version = "2.7")
  HOMEBREW_PREFIX/"lib/python#{version}/site-packages"
end

.in_sys_path?(python, path) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'Library/Homebrew/language/python.rb', line 48

def self.in_sys_path?(python, path)
  script = <<~EOS
    import os, sys
    [os.path.realpath(p) for p in sys.path].index(os.path.realpath("#{path}"))
  EOS
  quiet_system python, "-c", script
end

.major_minor_version(python) ⇒ Object



6
7
8
9
10
# File 'Library/Homebrew/language/python.rb', line 6

def self.major_minor_version(python)
  version = /\d\.\d/.match `#{python} --version 2>&1`
  return unless version
  Version.create(version.to_s)
end

.reads_brewed_pth_files?(python) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'Library/Homebrew/language/python.rb', line 31

def self.reads_brewed_pth_files?(python)
  version = major_minor_version python
  return unless homebrew_site_packages(version).directory?
  return unless homebrew_site_packages(version).writable_real?
  probe_file = homebrew_site_packages(version)/"homebrew-pth-probe.pth"
  begin
    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)" }
  ensure
    probe_file.unlink if probe_file.exist?
  end
end

.setup_install_args(prefix) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'Library/Homebrew/language/python.rb', line 56

def self.setup_install_args(prefix)
  shim = <<~EOS
    import setuptools, tokenize
    __file__ = 'setup.py'
    exec(compile(getattr(tokenize, 'open', open)(__file__).read()
      .replace('\\r\\n', '\\n'), __file__, 'exec'))
  EOS
  %W[
    -c
    #{shim}
    --no-user-cfg
    install
    --prefix=#{prefix}
    --single-version-externally-managed
    --record=installed.txt
  ]
end

.user_site_packages(python) ⇒ Object



44
45
46
# File 'Library/Homebrew/language/python.rb', line 44

def self.user_site_packages(python)
  Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp)
end