Class: Cask::Audit Private

Inherits:
Object show all
Includes:
SystemCommand::Mixin, Utils::Curl, Utils::Output::Mixin
Defined in:
cask/audit.rb

Overview

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

Audit a cask for various problems.

Constant Summary collapse

Error =

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

T.type_alias do
  {
    message:   T.nilable(String),
    location:  T.nilable(Homebrew::SourceLocation),
    corrected: T::Boolean,
  }
end

Instance Attribute Summary collapse

Instance 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_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods included from Utils::Curl

clear_path_cache, curl, curl_args, curl_check_http_content, curl_download, curl_executable, curl_headers, curl_http_content_headers_and_checksum, curl_output, curl_path, curl_response_follow_redirections, curl_response_last_location, curl_supports_fail_with_body?, curl_supports_tls13?, curl_version, curl_with_workarounds, http_status_ok?, https_redirect_curl_args, insecure_redirect?, no_insecure_redirect_curl_args, parse_curl_output, parse_curl_response, strip_progress_bar, url_protected_by_cloudflare?, url_protected_by_incapsula?

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Constructor Details

#initialize(cask, download: false, online: nil, strict: nil, signing: nil, new_cask: nil, only: [], except: []) ⇒ 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.

Parameters:

  • cask (::Cask::Cask)
  • download (Boolean) (defaults to: false)
  • online (Boolean, nil) (defaults to: nil)
  • strict (Boolean, nil) (defaults to: nil)
  • signing (Boolean, nil) (defaults to: nil)
  • new_cask (Boolean, nil) (defaults to: nil)
  • only (Array<String>) (defaults to: [])
  • except (Array<String>) (defaults to: [])


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'cask/audit.rb', line 49

def initialize(
  cask,
  download: false,
  online: nil, strict: nil, signing: nil,
  new_cask: nil, only: [], except: []
)
  # `new_cask` implies `online`, `strict` and `signing`
  online = new_cask if online.nil?
  strict = new_cask if strict.nil?
  signing = new_cask if signing.nil?

  # `online` and `signing` imply `download`
  download ||= online || signing

  @cask = cask
  @download = T.let(nil, T.nilable(Download))
  @download = Download.new(cask) if download
  @online = online
  @strict = strict
  @signing = signing
  @new_cask = new_cask
  @only = only
  @except = except
  @livecheck_result = T.let(nil, T.nilable(T.any(T::Boolean, Symbol)))
end

Instance Attribute Details

#caskCask (readonly)

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:



34
35
36
# File 'cask/audit.rb', line 34

def cask
  @cask
end

#downloadDownload? (readonly)

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:



37
38
39
# File 'cask/audit.rb', line 37

def download
  @download
end

#livecheck_result=(value) ⇒ void (writeonly)

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.



40
41
42
# File 'cask/audit.rb', line 40

def livecheck_result=(value)
  @livecheck_result = value
end

Instance Method Details

#add_error(message, location: nil, strict_only: false) ⇒ 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:



129
130
131
132
133
134
# File 'cask/audit.rb', line 129

def add_error(message, location: nil, strict_only: false)
  # Only raise non-critical audits if the user specified `--strict`.
  return if strict_only && !@strict

  errors << { message:, location:, corrected: false }
end

#errorsArray<Error>

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:



108
109
110
# File 'cask/audit.rb', line 108

def errors
  @errors ||= T.let([], T.nilable(T::Array[Error]))
end

#errors?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.

Returns:

  • (Boolean)


113
114
115
# File 'cask/audit.rb', line 113

def errors?
  errors.any?
end

#extract_artifacts(include_manual_installers: false, &_block) {|artifacts, @tmpdir| ... } ⇒ 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:

Yields:

  • (artifacts, @tmpdir)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'cask/audit.rb', line 163

def extract_artifacts(include_manual_installers: false, &_block)
  return unless online?
  return if (download = self.download).nil?

  artifacts = cask.artifacts.select do |artifact|
    artifact.is_a?(Artifact::Pkg) ||
      artifact.is_a?(Artifact::App) ||
      artifact.is_a?(Artifact::Binary) ||
      (include_manual_installers &&
        artifact.is_a?(Artifact::Installer) &&
        artifact.manual_install &&
        [".app", ".pkg"].include?(artifact.path.extname.downcase))
  end

  if @artifacts_extracted && @tmpdir
    yield artifacts, @tmpdir if block_given?
    return
  end

  return if artifacts.empty?

  @tmpdir ||= T.let(Pathname(Dir.mktmpdir("cask-audit", HOMEBREW_TEMP)), T.nilable(Pathname))

  # Clean up tmp dir when @tmpdir object is destroyed
  ObjectSpace.define_finalizer(
    @tmpdir,
    proc { FileUtils.remove_entry(@tmpdir) },
  )

  ohai "Downloading and extracting artifacts"

  downloaded_path = download.fetch

  primary_container = UnpackStrategy.detect(downloaded_path, type: @cask.container&.type, merge_xattrs: true)
  return if primary_container.nil?

  # If the container has any dependencies we need to install them or unpacking will fail.
  if primary_container.dependencies.any?

    install_options = {
      show_header:          true,
      installed_on_request: false,
      verbose:              false,
    }.compact

    Homebrew::Install.perform_preinstall_checks_once
    formula_installers = primary_container.dependencies.filter_map do |dep|
      next unless dep.is_a?(Formula)
      next if dep.linked?

      FormulaInstaller.new(
        dep,
        **install_options,
      )
    end
    valid_formula_installers = Homebrew::Install.fetch_formulae(formula_installers)

    formula_installers.each do |fi|
      next unless valid_formula_installers.include?(fi)

      fi.install
      fi.finish
    end
  end

  # Extract the container to the temporary directory.
  primary_container.extract_nestedly(to: @tmpdir, basename: downloaded_path.basename, verbose: false)

  if (nested_container = @cask.container&.nested)
    FileUtils.chmod_R "+rw", @tmpdir/nested_container, force: true, verbose: false
    UnpackStrategy.detect(@tmpdir/nested_container, merge_xattrs: true)
                  .extract_nestedly(to: @tmpdir, verbose: false)
  end

  # Propagate quarantine attributes from the downloaded file to extracted contents.
  # This is necessary because some extraction tools (like 7zr) don't preserve xattrs.
  if Quarantine.available? && Quarantine.detect(downloaded_path)
    Quarantine.propagate(from: downloaded_path, to: @tmpdir)
  end

  # Process rename operations after extraction
  # Create a temporary installer to process renames in the audit directory
  temp_installer = Installer.new(@cask)
  temp_installer.process_rename_operations(target_dir: @tmpdir)

  # Set the flag to indicate that extraction has occurred.
  @artifacts_extracted = T.let(true, T.nilable(TrueClass))

  # Yield the artifacts and temp directory to the block if provided.
  yield artifacts, @tmpdir if block_given?
end

#new_cask?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.

Returns:

  • (Boolean)


76
# File 'cask/audit.rb', line 76

def new_cask? = !!@new_cask

#normalize_min_os(min_os) ⇒ MacOSVersion?

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:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'cask/audit.rb', line 256

def normalize_min_os(min_os)
  return if min_os.nil?
  return if min_os.is_a?(String) && min_os.blank?

  min_os = if min_os.is_a?(MacOSVersion)
    min_os.strip_patch
  else
    MacOSVersion.new(min_os).strip_patch
  end

  # Big Sur is sometimes identified as 10.16, so we override it to the
  # expected macOS version (11).
  min_os = MacOSVersion.new("11") if min_os == "10.16"

  min_os
rescue MacOSVersion::Error
  nil
end

#online?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.

Returns:

  • (Boolean)


79
# File 'cask/audit.rb', line 79

def online? =!!@online

#resultString?

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:



137
138
139
# File 'cask/audit.rb', line 137

def result
  Formatter.error("failed") if errors?
end

#run!::Cask::Audit

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:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'cask/audit.rb', line 88

def run!
  only_audits = @only
  except_audits = @except

  private_methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name|
    name = audit_method_name.delete_prefix("audit_")
    next if !only_audits.empty? && only_audits.exclude?(name)
    next if except_audits.include?(name)

    send(audit_method_name)
  end

  self
rescue => e
  odebug e, ::Utils::Backtrace.clean(e)
  add_error "exception while auditing #{cask}: #{e.message}"
  self
end

#signing?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.

Returns:

  • (Boolean)


82
# File 'cask/audit.rb', line 82

def signing? = !!@signing

#strict?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.

Returns:

  • (Boolean)


85
# File 'cask/audit.rb', line 85

def strict? = !!@strict

#success?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.

Returns:

  • (Boolean)


118
119
120
# File 'cask/audit.rb', line 118

def success?
  !errors?
end

#summaryString?

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:



142
143
144
145
146
147
148
149
150
151
152
# File 'cask/audit.rb', line 142

def summary
  return if success?

  summary = ["audit for #{cask}: #{result}"]

  errors.each do |error|
    summary << " #{Formatter.error("-")} #{error[:message]}"
  end

  summary.join("\n")
end