Module: Language::Python

Extended by:
Utils::Output::Mixin
Defined in:
language/python.rb

Overview

Helper functions for Python formulae.

Defined Under Namespace

Modules: Shebang, Virtualenv

Class Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Class Method Details

.direct_dependency_paths(formula, pattern: /\Apython(?:@.+)?\z/, required: false) ⇒ Hash{String => Pathname}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns stable executable paths keyed by direct Python dependency name.

Parameters:

  • formula (Formula)
  • pattern (Regexp) (defaults to: /\Apython(?:@.+)?\z/)
  • required (Boolean) (defaults to: false)

Returns:



28
29
30
31
32
33
34
35
36
37
# File 'language/python.rb', line 28

def self.direct_dependency_paths(formula, pattern: /\Apython(?:@.+)?\z/, required: false)
  formula.deps.filter_map do |dependency|
    next if required && !dependency.required?

    name = Utils.name_from_full_name(dependency.name)
    next unless name.match?(pattern)

    [name, Utils::Path.formula_opt_bin(name)/name.delete("@")]
  end.to_h
end

.each_python(build, &block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'language/python.rb', line 74

def self.each_python(build, &block)
  odeprecated "Language::Python.each_python", "Formula#python3 or explicit Python dependency iteration"

  original_pythonpath = ENV.fetch("PYTHONPATH", nil)
  pythons = { "python@3" => "python3",
              "pypy"     => "pypy",
              "pypy3"    => "pypy3" }
  pythons.each do |python_formula, python|
    python_formula = Formulary.factory(python_formula)
    next if build.without? python_formula.to_s

    version = major_minor_version python
    ENV["PYTHONPATH"] = if python_formula.latest_version_installed?
      nil
    else
      (HOMEBREW_PREFIX/site_packages(python)).to_s
    end
    block&.call python, version
  end
  ENV["PYTHONPATH"] = original_pythonpath
end

.homebrew_site_packages(python = "python3.7") ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



48
49
50
51
52
# File 'language/python.rb', line 48

def self.homebrew_site_packages(python = "python3.7")
  odeprecated "Language::Python.homebrew_site_packages", "HOMEBREW_PREFIX/Language::Python.site_packages(python)"

  HOMEBREW_PREFIX/site_packages(python)
end

.in_sys_path?(python, path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
# File 'language/python.rb', line 126

def self.in_sys_path?(python, path)
  odeprecated "Language::Python.in_sys_path?", "querying `sys.path` with the Python interpreter"

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

.major_minor_version(python) ⇒ Version?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



40
41
42
43
44
45
# File 'language/python.rb', line 40

def self.major_minor_version(python)
  version = Utils.popen_read_text(python, "--version", err: :out).chomp[/(\d\.\d+)/, 1]
  return unless version

  Version.new(version)
end

.reads_brewed_pth_files?(python) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'language/python.rb', line 97

def self.reads_brewed_pth_files?(python)
  odeprecated "Language::Python.reads_brewed_pth_files?", "an isolated Python virtualenv"

  site_packages = HOMEBREW_PREFIX/site_packages(python)
  return false unless site_packages.directory?
  return false unless site_packages.writable?

  probe_file = site_packages/"homebrew-pth-probe.pth"
  begin
    probe_file.atomic_write("import site; site.homebrew_was_here = True")
    Utils::Shell.with_homebrew_path do
      SystemCommand.quiet_system python, "-c", "import site; assert(site.homebrew_was_here)"
    end
  ensure
    probe_file.unlink if probe_file.exist?
  end
end

.site_packages(python = nil) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'language/python.rb', line 55

def self.site_packages(python = nil)
  if python.nil?
    odeprecated "Language::Python.site_packages without an interpreter", "an explicit Python interpreter argument"
    python = "python3.7"
  end

  if (python == "pypy") || (python == "pypy3")
    "site-packages"
  else
    "lib/python#{major_minor_version python}/site-packages"
  end
end

.user_site_packages(python) ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



116
117
118
119
120
121
122
123
# File 'language/python.rb', line 116

def self.user_site_packages(python)
  odeprecated "Language::Python.user_site_packages",
              "querying `site.getusersitepackages()` with the Python interpreter"

  Pathname.new(
    Utils.popen_read_text(python, "-c", "import site; print(site.getusersitepackages())", err: :err).chomp,
  )
end