Class: Cleaner Private

Inherits:
Object show all
Includes:
Context, OS::Linux::Cleaner, OS::Mac::Cleaner, Utils::Output::Mixin
Defined in:
cleaner.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.

Cleans a newly installed keg. By default:

  • removes .la files
  • removes .tbd files
  • removes perllocal.pod files
  • removes .packlist files
  • removes empty directories
  • sets permissions on executables
  • removes unresolved symlinks

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_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods included from Context

current, current=, #debug?, #deferred_environment_expansion?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(formula) ⇒ 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.

Create a cleaner for the given formula.

Parameters:



22
23
24
# File 'cleaner.rb', line 22

def initialize(formula)
  @formula = formula
end

Instance Method Details

#clean ⇒ 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.

Clean the keg of the formula.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'cleaner.rb', line 28

def clean
  ObserverPathnameExtension.reset_counts!

  # Many formulae include `lib/charset.alias`, but it is not strictly needed
  # and will conflict if more than one formula provides it.
  observe_file_removal @formula.lib/"charset.alias"

  # Recursively fix file permissions and remove `.la` files, unless they (or
  # their parent directories) are protected by `skip_clean`.
  [@formula.bin, @formula.sbin, @formula.lib].each do |directory|
    next unless directory.exist?

    directory.find do |path|
      path.extend(ObserverPathnameExtension)

      Find.prune if @formula.skip_clean? path

      next if path.directory?

      if path.extname == ".la" || path.extname == ".tbd" || PERL_BASENAMES.include?(path.basename.to_s)
        path.unlink
      elsif path.symlink?
        # Skip it.
      else
        # Set permissions for executables and non-executables.
        perms = if executable_path?(path)
          0555
        else
          0444
        end
        if debug?
          old_perms = path.stat.mode & 0777
          odebug "Fixing #{path} permissions from #{old_perms.to_s(8)} to #{perms.to_s(8)}" if perms != old_perms
        end
        path.chmod perms
      end
    end
  end

  # Get rid of any info `dir` files, so they don't conflict at the link stage.
  #
  # The `dir` files come in at least 3 locations:
  #
  # 1. `info/dir`
  # 2. `info/#{name}/dir`
  # 3. `info/#{arch}/dir`
  #
  # Of these 3 only `info/#{name}/dir` is safe to keep since the rest will
  # conflict with other formulae because they use a shared location.
  #
  # See
  # [cleaner: recursively delete info `dir`s][1],
  # [emacs 28.1 bottle does not contain `dir` file][2] and
  # [Keep `info/#{f.name}/dir` files in cleaner][3]
  # for more info.
  #
  # [1]: https://github.com/Homebrew/brew/pull/11597
  # [2]: https://github.com/Homebrew/homebrew-core/issues/100190
  # [3]: https://github.com/Homebrew/brew/pull/13215
  @formula.info.glob("**/dir").each do |info_dir_file|
    next unless info_dir_file.file?
    next if info_dir_file == @formula.info/@formula.name/"dir"
    next if @formula.skip_clean?(info_dir_file)

    observe_file_removal info_dir_file
  end

  rewrite_shebangs

  # Remove non-reproducible pip direct_url.json which records the /tmp build directory.
  # Remove RECORD files to prevent changes to the installed Python package.
  # Modify INSTALLER to provide information that files are managed by brew.
  #
  # @see https://packaging.python.org/en/latest/specifications/recording-installed-packages/
  @formula.prefix.realpath.find do |path|
    Find.prune if @formula.skip_clean?(path)

    next if path.directory? || path.symlink?
    next if path.parent.extname != ".dist-info"

    case path.basename.to_s
    when "direct_url.json", "RECORD"
      observe_file_removal path
    when "INSTALLER"
      odebug "Modifying #{path} contents from #{path.read.chomp} to brew"
      path.atomic_write("brew\n")
    end
  end

  prune
end