Class: Keg

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
brew/Library/Homebrew/keg.rb,
brew/Library/Homebrew/os/mac/keg.rb,
brew/Library/Homebrew/keg_relocate.rb,
brew/Library/Homebrew/extend/os/mac/keg.rb,
brew/Library/Homebrew/extend/os/mac/keg_relocate.rb,
brew/Library/Homebrew/extend/os/linux/keg_relocate.rb

Defined Under Namespace

Classes: AlreadyLinkedError, ConflictError, DirectoryNotWritableError, LinkError, Relocation

Constant Summary collapse

LOCALEDIR_RX =

locale-specific directories have the form language[_territory][.codeset][@modifier]

%r{(locale|man)/([a-z]{2}|C|POSIX)(_[A-Z]{2})?(\.[a-zA-Z\-0-9]+(@.+)?)?}.freeze
INFOFILE_RX =
%r{info/([^.].*?\.info|dir)$}.freeze
(GENERIC_KEG_LINK_DIRECTORIES + ["Frameworks"]).freeze
MUST_EXIST_SUBDIRECTORIES =
(
  KEG_LINK_DIRECTORIES - %w[var] + %w[
    opt
    var/homebrew/linked
  ]
).map { |dir| HOMEBREW_PREFIX/dir }.uniq.sort.freeze
MUST_EXIST_DIRECTORIES =

Keep relatively in sync with https://github.com/Homebrew/install/blob/master/install

(MUST_EXIST_SUBDIRECTORIES + [
  HOMEBREW_CELLAR,
].sort.uniq).freeze
MUST_BE_WRITABLE_DIRECTORIES =
(
  %w[
    etc/bash_completion.d lib/pkgconfig
    share/aclocal share/doc share/info share/locale share/man
    share/man/man1 share/man/man2 share/man/man3 share/man/man4
    share/man/man5 share/man/man6 share/man/man7 share/man/man8
    share/zsh share/zsh/site-functions
    var/log
  ].map { |dir| HOMEBREW_PREFIX/dir } + MUST_EXIST_SUBDIRECTORIES + [
    HOMEBREW_CACHE,
    HOMEBREW_CELLAR,
    HOMEBREW_LOCKS,
    HOMEBREW_LOGS,
    HOMEBREW_REPOSITORY,
    Language::Python.homebrew_site_packages,
  ]
).uniq.sort.freeze
SHARE_PATHS =

These paths relative to the keg’s share directory should always be real directories in the prefix, never symlinks.

%w[
  aclocal doc info java locale man
  man/man1 man/man2 man/man3 man/man4
  man/man5 man/man6 man/man7 man/man8
  man/cat1 man/cat2 man/cat3 man/cat4
  man/cat5 man/cat6 man/cat7 man/cat8
  applications gnome gnome/help icons
  mime-info pixmaps sounds postgresql
].freeze
PREFIX_PLACEHOLDER =
"@@HOMEBREW_PREFIX@@"
CELLAR_PLACEHOLDER =
"@@HOMEBREW_CELLAR@@"
REPOSITORY_PLACEHOLDER =
"@@HOMEBREW_REPOSITORY@@"
(remove_const :KEG_LINK_DIRECTORIES).freeze
FRAMEWORK_RX =

Matches framework references like XXX.framework/Versions/YYY/XXX and XXX.framework/XXX, both with or without a slash-delimited prefix.

%r{(?:^|/)(([^/]+)\.framework/(?:Versions/[^/]+/)?\2)$}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Keg

Returns a new instance of Keg



195
196
197
198
199
200
201
202
203
204
205
# File 'brew/Library/Homebrew/keg.rb', line 195

def initialize(path)
  path = path.resolved_path if path.to_s.start_with?("#{HOMEBREW_PREFIX}/opt/")
  raise "#{path} is not a valid keg" unless path.parent.parent.realpath == HOMEBREW_CELLAR.realpath
  raise "#{path} is not a directory" unless path.directory?

  @path = path
  @name = path.parent.basename.to_s
  @linked_keg_record = HOMEBREW_LINKED_KEGS/name
  @opt_record = HOMEBREW_PREFIX/"opt/#{name}"
  @require_relocation = false
end

Instance Attribute Details

#linked_keg_recordObject (readonly)

Returns the value of attribute linked_keg_record



186
187
188
# File 'brew/Library/Homebrew/keg.rb', line 186

def linked_keg_record
  @linked_keg_record
end

#nameObject (readonly)

Returns the value of attribute name



186
187
188
# File 'brew/Library/Homebrew/keg.rb', line 186

def name
  @name
end

#opt_recordObject (readonly)

Returns the value of attribute opt_record



186
187
188
# File 'brew/Library/Homebrew/keg.rb', line 186

def opt_record
  @opt_record
end

Class Method Details

.allObject



182
183
184
# File 'brew/Library/Homebrew/keg.rb', line 182

def self.all
  Formula.racks.flat_map(&:subdirs).map { |d| new(d) }
end

.bottle_dependenciesObject



193
194
195
# File 'brew/Library/Homebrew/keg_relocate.rb', line 193

def self.bottle_dependencies
  relocation_formulae
end

.file_linked_libraries(file, string) ⇒ Object



185
186
187
# File 'brew/Library/Homebrew/keg_relocate.rb', line 185

def self.file_linked_libraries(_file, _string)
  []
end

.find_some_installed_dependents(kegs) ⇒ Object

Given an array of kegs, this method will try to find some other kegs that depend on them. If it does, it returns:

  • some kegs in the passed array that have installed dependents
  • some installed dependents of those kegs.

If it doesn’t, it returns nil.

Note that nil will be returned if the only installed dependents in the passed kegs are other kegs in the array.

For efficiency, we don’t bother trying to get complete data.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'brew/Library/Homebrew/keg.rb', line 127

def self.find_some_installed_dependents(kegs)
  keg_names = kegs.select(&:optlinked?).map(&:name)
  keg_formulae = []
  kegs_by_source = kegs.group_by do |keg|
    begin
      # First, attempt to resolve the keg to a formula
      # to get up-to-date name and tap information.
      f = keg.to_formula
      keg_formulae << f
      [f.name, f.tap]
    rescue FormulaUnavailableError
      # If the formula for the keg can't be found,
      # fall back to the information in the tab.
      [keg.name, keg.tab.tap]
    end
  end

  all_required_kegs = Set.new
  all_dependents = []

  # Don't include dependencies of kegs that were in the given array.
  formulae_to_check = Formula.installed - keg_formulae

  formulae_to_check.each do |dependent|
    required = dependent.missing_dependencies(hide: keg_names)
    required_kegs = required.map do |f|
      f_kegs = kegs_by_source[[f.name, f.tap]]
      next unless f_kegs

      f_kegs.max_by(&:version)
    end.compact

    next if required_kegs.empty?

    all_required_kegs += required_kegs
    all_dependents << dependent.to_s
  end

  return if all_required_kegs.empty?
  return if all_dependents.empty?

  [all_required_kegs.to_a, all_dependents.sort]
end

.for(path) ⇒ Object

if path is a file in a keg then this will return the containing Keg object

Raises:



172
173
174
175
176
177
178
179
180
# File 'brew/Library/Homebrew/keg.rb', line 172

def self.for(path)
  path = path.realpath
  until path.root?
    return Keg.new(path) if path.parent.parent == HOMEBREW_CELLAR.realpath

    path = path.parent.realpath # realpath() prevents root? failing
  end
  raise NotAKegError, "#{path} is not inside a keg"
end

.relocation_formulaeObject



189
190
191
# File 'brew/Library/Homebrew/keg_relocate.rb', line 189

def self.relocation_formulae
  []
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



217
218
219
# File 'brew/Library/Homebrew/keg.rb', line 217

def ==(other)
  instance_of?(other.class) && path == other.path
end

#aliasesObject



527
528
529
# File 'brew/Library/Homebrew/keg.rb', line 527

def aliases
  tab.aliases || []
end

#appsObject



404
405
406
407
# File 'brew/Library/Homebrew/keg.rb', line 404

def apps
  app_prefix = optlinked? ? opt_record : path
  Pathname.glob("#{app_prefix}/{,libexec/}*.app")
end

#change_dylib_id(id, file) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'brew/Library/Homebrew/os/mac/keg.rb', line 4

def change_dylib_id(id, file)
  return if file.dylib_id == id

  @require_relocation = true
  puts "Changing dylib ID of #{file}\n  from #{file.dylib_id}\n    to #{id}" if ARGV.debug?
  MachO::Tools.change_dylib_id(file, id, strict: false)
rescue MachO::MachOError
  onoe <<~EOS
    Failed changing dylib ID of #{file}
      from #{file.dylib_id}
        to #{id}
  EOS
  raise
end

#change_install_name(old, new, file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'brew/Library/Homebrew/os/mac/keg.rb', line 19

def change_install_name(old, new, file)
  return if old == new

  @require_relocation = true
  puts "Changing install name in #{file}\n  from #{old}\n    to #{new}" if ARGV.debug?
  MachO::Tools.change_install_name(file, old, new, strict: false)
rescue MachO::MachOError
  onoe <<~EOS
    Failed changing install name in #{file}
      from #{old}
        to #{new}
  EOS
  raise
end

#change_rpath(file, old_prefix, new_prefix) ⇒ Object



18
19
20
21
22
23
24
25
26
27
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
# File 'brew/Library/Homebrew/extend/os/linux/keg_relocate.rb', line 18

def change_rpath(file, old_prefix, new_prefix)
  return if !file.elf? || !file.dynamic_elf?

  patchelf = DevelopmentTools.locate "patchelf"
  cmd_rpath = [patchelf, "--print-rpath", file]
  old_rpath = Utils.popen_read(*cmd_rpath, err: :out).strip

  # patchelf requires that the ELF file have a .dynstr section.
  # Skip ELF files that do not have a .dynstr section.
  return if ["cannot find section .dynstr", "strange: no string table"].include?(old_rpath)
  unless $CHILD_STATUS.success?
    raise ErrorDuringExecution.new(cmd_rpath, status: $CHILD_STATUS, output: [[:stderr, old_rpath]])
  end

  rpath = old_rpath
          .split(":")
          .map { |x| x.sub(old_prefix, new_prefix) }
          .select { |x| x.start_with?(new_prefix, "$ORIGIN") }

  lib_path = "#{new_prefix}/lib"
  rpath << lib_path unless rpath.include? lib_path
  new_rpath = rpath.join(":")
  cmd = [patchelf, "--force-rpath", "--set-rpath", new_rpath]

  if file.binary_executable?
    old_interpreter = Utils.safe_popen_read(patchelf, "--print-interpreter", file).strip
    new_interpreter = if File.readable? "#{new_prefix}/lib/ld.so"
      "#{new_prefix}/lib/ld.so"
    else
      old_interpreter.sub old_prefix, new_prefix
    end
    cmd << "--set-interpreter" << new_interpreter if old_interpreter != new_interpreter
  end

  return if old_rpath == new_rpath && old_interpreter == new_interpreter

  safe_system(*cmd, file)
end

#completion_installed?(shell) ⇒ Boolean

Returns:

  • (Boolean)


368
369
370
371
372
373
374
375
376
377
# File 'brew/Library/Homebrew/keg.rb', line 368

def completion_installed?(shell)
  dir = case shell
  when :bash then path/"etc/bash_completion.d"
  when :zsh
    dir = path/"share/zsh/site-functions"
    dir if dir.directory? && dir.children.any? { |f| f.basename.to_s.start_with?("_") }
  when :fish then path/"share/fish/vendor_completions.d"
  end
  dir&.directory? && !dir.children.empty?
end

#delete_pyc_files!Object



546
547
548
549
# File 'brew/Library/Homebrew/keg.rb', line 546

def delete_pyc_files!
  find { |pn| pn.delete if %w[.pyc .pyo].include?(pn.extname) }
  find { |pn| FileUtils.rm_rf pn if pn.basename.to_s == "__pycache__" }
end

#detect_cxx_stdlibs(options = {}) ⇒ Object

Detects the C++ dynamic libraries in place, scanning the dynamic links of the files within the keg. Note that this doesn’t attempt to distinguish between libstdc++ versions, for instance between Apple libstdc++ and GNU libstdc++



45
46
47
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 45

def detect_cxx_stdlibs(_options = {})
  []
end

#dylib_id_for(file) ⇒ Object



130
131
132
133
134
135
136
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 130

def dylib_id_for(file)
  # The new dylib ID should have the same basename as the old dylib ID, not
  # the basename of the file itself.
  basename = File.basename(file.dylib_id)
  relative_dirname = file.dirname.relative_path_from(path)
  (opt_record/relative_dirname/basename).to_s
end

#each_install_name_for(file, &block) ⇒ Object



123
124
125
126
127
128
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 123

def each_install_name_for(file, &block)
  dylibs = file.dynamically_linked_libraries
  dylibs.reject! { |fn| fn =~ /^@(loader|executable)_path/ }
  dylibs.reject! { |fn| fn =~ /^@rpath/ } unless ENV["HOMEBREW_RELOCATE_METAVARS"]
  dylibs.each(&block)
end

#each_unique_file_matching(string) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'brew/Library/Homebrew/keg_relocate.rb', line 103

def each_unique_file_matching(string)
  Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
    hardlinks = Set.new

    until io.eof?
      file = Pathname.new(io.readline.chomp)
      next if file.symlink?

      yield file if hardlinks.add? file.stat.ino
    end
  end
end

#elf_filesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'brew/Library/Homebrew/extend/os/linux/keg_relocate.rb', line 71

def elf_files
  hardlinks = Set.new
  elf_files = []
  path.find do |pn|
    next if pn.symlink? || pn.directory?
    next if !pn.dylib? && !pn.binary_executable?

    # If we've already processed a file, ignore its hardlinks (which have the
    # same dev ID and inode). This prevents relocations from being performed
    # on a binary more than once.
    next unless hardlinks.add? [pn.stat.dev, pn.stat.ino]

    elf_files << pn
  end
  elf_files
end

#elisp_installed?Boolean

Returns:

  • (Boolean)


409
410
411
412
413
# File 'brew/Library/Homebrew/keg.rb', line 409

def elisp_installed?
  return false unless (path/"share/emacs/site-lisp"/name).exist?

  (path/"share/emacs/site-lisp"/name).children.any? { |f| %w[.el .elc].include? f.extname }
end

#empty_installation?Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'brew/Library/Homebrew/keg.rb', line 222

def empty_installation?
  Pathname.glob("#{path}/*") do |file|
    return false if file.directory? && !file.children.reject(&:ds_store?).empty?

    basename = file.basename.to_s
    next if Metafiles.copy?(basename)
    next if %w[.DS_Store INSTALL_RECEIPT.json].include?(basename)

    return false
  end

  true
end

#expand_rpath(file, bad_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 90

def expand_rpath(file, bad_name)
  suffix = bad_name.sub(/^@rpath/, "")

  file.rpaths.each do |rpath|
    return rpath/suffix if (rpath/suffix).exist?
  end

  opoo "Could not find library #{bad_name} for #{file}"
  bad_name
end

#find_dylib(bad_name) ⇒ Object



150
151
152
153
154
155
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 150

def find_dylib(bad_name)
  return unless lib.directory?

  suffix = "/#{find_dylib_suffix_from(bad_name)}"
  lib.find { |pn| break pn if pn.to_s.end_with?(suffix) }
end

#find_dylib_suffix_from(bad_name) ⇒ Object



142
143
144
145
146
147
148
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 142

def find_dylib_suffix_from(bad_name)
  if (framework = bad_name.match(FRAMEWORK_RX))
    framework[1]
  else
    File.basename(bad_name)
  end
end

#fix_dynamic_linkageObject



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

def fix_dynamic_linkage
  symlink_files.each do |file|
    link = file.readlink
    # Don't fix relative symlinks
    next unless link.absolute?

    link_starts_cellar = link.to_s.start_with?(HOMEBREW_CELLAR.to_s)
    link_starts_prefix = link.to_s.start_with?(HOMEBREW_PREFIX.to_s)
    next if !link_starts_cellar && !link_starts_prefix

    new_src = link.relative_path_from(file.parent)
    file.unlink
    FileUtils.ln_s(new_src, file)
  end
end

#fixed_name(file, bad_name) ⇒ Object

If file is a dylib or bundle itself, look for the dylib named by bad_name relative to the lib directory, so that we can skip the more expensive recursive search if possible.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 104

def fixed_name(file, bad_name)
  if bad_name.start_with? PREFIX_PLACEHOLDER
    bad_name.sub(PREFIX_PLACEHOLDER, HOMEBREW_PREFIX)
  elsif bad_name.start_with? CELLAR_PLACEHOLDER
    bad_name.sub(CELLAR_PLACEHOLDER, HOMEBREW_CELLAR)
  elsif (file.dylib? || file.mach_o_bundle?) && (file.parent + bad_name).exist?
    "@loader_path/#{bad_name}"
  elsif file.mach_o_executable? && (lib + bad_name).exist?
    "#{lib}/#{bad_name}"
  elsif bad_name.start_with?("@rpath") && ENV["HOMEBREW_RELOCATE_METAVARS"]
    expand_rpath file, bad_name
  elsif (abs_name = find_dylib(bad_name)) && abs_name.exist?
    abs_name.to_s
  else
    opoo "Could not fix #{bad_name} in #{file}"
    bad_name
  end
end

#functions_installed?(shell) ⇒ Boolean

Returns:

  • (Boolean)


379
380
381
382
383
384
385
386
387
388
389
390
# File 'brew/Library/Homebrew/keg.rb', line 379

def functions_installed?(shell)
  case shell
  when :fish
    dir = path/"share/fish/vendor_functions.d"
    dir.directory? && !dir.children.empty?
  when :zsh
    # Check for non completion functions (i.e. files not started with an underscore),
    # since those can be checked separately
    dir = path/"share/zsh/site-functions"
    dir.directory? && dir.children.any? { |f| !f.basename.to_s.start_with?("_") }
  end
end

#generic_fix_dynamic_linkageObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'brew/Library/Homebrew/keg_relocate.rb', line 31

def fix_dynamic_linkage
  symlink_files.each do |file|
    link = file.readlink
    # Don't fix relative symlinks
    next unless link.absolute?

    link_starts_cellar = link.to_s.start_with?(HOMEBREW_CELLAR.to_s)
    link_starts_prefix = link.to_s.start_with?(HOMEBREW_PREFIX.to_s)
    next if !link_starts_cellar && !link_starts_prefix

    new_src = link.relative_path_from(file.parent)
    file.unlink
    FileUtils.ln_s(new_src, file)
  end
end

#generic_recursive_fgrep_argsObject



101
102
103
104
# File 'brew/Library/Homebrew/keg_relocate.rb', line 101

def recursive_fgrep_args
  # for GNU grep; overridden for BSD grep on OS X
  "-lr"
end

#inspectObject



213
214
215
# File 'brew/Library/Homebrew/keg.rb', line 213

def inspect
  "#<#{self.class.name}:#{path}>"
end

#libObject



116
117
118
# File 'brew/Library/Homebrew/keg_relocate.rb', line 116

def lib
  path/"lib"
end

#libtool_filesObject



165
166
167
168
169
170
171
172
173
174
# File 'brew/Library/Homebrew/keg_relocate.rb', line 165

def libtool_files
  libtool_files = []

  path.find do |pn|
    next if pn.symlink? || pn.directory? || ![".la", ".lai"].include?(pn.extname)

    libtool_files << pn
  end
  libtool_files
end


432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'brew/Library/Homebrew/keg.rb', line 432

def link(mode = OpenStruct.new)
  raise AlreadyLinkedError, self if linked_keg_record.directory?

  ObserverPathnameExtension.reset_counts!

  optlink(mode) unless mode.dry_run

  # yeah indeed, you have to force anything you need in the main tree into
  # these dirs REMEMBER that *NOT* everything needs to be in the main tree
  link_dir("etc", mode) { :mkpath }
  link_dir("bin", mode) { :skip_dir }
  link_dir("sbin", mode) { :skip_dir }
  link_dir("include", mode) { :link }

  link_dir("share", mode) do |relative_path|
    case relative_path.to_s
    when "locale/locale.alias" then :skip_file
    when INFOFILE_RX then :info
    when LOCALEDIR_RX then :mkpath
    when %r{^icons/.*/icon-theme\.cache$} then :skip_file
    # all icons subfolders should also mkpath
    when %r{^icons/} then :mkpath
    when /^zsh/ then :mkpath
    when /^fish/ then :mkpath
    # Lua, Lua51, Lua53 all need the same handling.
    when %r{^lua/} then :mkpath
    when %r{^guile/} then :mkpath
    when *SHARE_PATHS then :mkpath
    else :link
    end
  end

  link_dir("lib", mode) do |relative_path|
    case relative_path.to_s
    when "charset.alias" then :skip_file
    # pkg-config database gets explicitly created
    when "pkgconfig" then :mkpath
    # cmake database gets explicitly created
    when "cmake" then :mkpath
    # lib/language folders also get explicitly created
    when "dtrace" then :mkpath
    when /^gdk-pixbuf/ then :mkpath
    when "ghc" then :mkpath
    when /^gio/ then :mkpath
    when "lua" then :mkpath
    when /^mecab/ then :mkpath
    when /^node/ then :mkpath
    when /^ocaml/ then :mkpath
    when /^perl5/ then :mkpath
    when "php" then :mkpath
    when /^python[23]\.\d/ then :mkpath
    when /^R/ then :mkpath
    when /^ruby/ then :mkpath
    # Everything else is symlinked to the cellar
    else :link
    end
  end

  link_dir("Frameworks", mode) do |relative_path|
    # Frameworks contain symlinks pointing into a subdir, so we have to use
    # the :link strategy. However, for Foo.framework and
    # Foo.framework/Versions we have to use :mkpath so that multiple formulae
    # can link their versions into it and `brew [un]link` works.
    if relative_path.to_s =~ %r{[^/]*\.framework(/Versions)?$}
      :mkpath
    else
      :link
    end
  end

  make_relative_symlink(linked_keg_record, path, mode) unless mode.dry_run
rescue LinkError
  unlink
  raise
else
  ObserverPathnameExtension.n
end

#linked?Boolean

Returns:

  • (Boolean)


240
241
242
243
244
# File 'brew/Library/Homebrew/keg.rb', line 240

def linked?
  linked_keg_record.symlink? &&
    linked_keg_record.directory? &&
    path == linked_keg_record.resolved_path
end

#lockObject



358
359
360
361
362
363
364
365
366
# File 'brew/Library/Homebrew/keg.rb', line 358

def lock
  FormulaLock.new(name).with_lock do
    if oldname_opt_record
      FormulaLock.new(oldname_opt_record.basename.to_s).with_lock { yield }
    else
      yield
    end
  end
end

#mach_o_filesObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'brew/Library/Homebrew/extend/os/mac/keg_relocate.rb', line 157

def mach_o_files
  hardlinks = Set.new
  mach_o_files = []
  path.find do |pn|
    next if pn.symlink? || pn.directory?
    next unless pn.dylib? || pn.mach_o_bundle? || pn.mach_o_executable?
    # if we've already processed a file, ignore its hardlinks (which have the same dev ID and inode)
    # this prevents relocations from being performed on a binary more than once
    next unless hardlinks.add? [pn.stat.dev, pn.stat.ino]

    mach_o_files << pn
  end

  mach_o_files
end

#oldname_opt_recordObject



424
425
426
427
428
429
430
# File 'brew/Library/Homebrew/keg.rb', line 424

def oldname_opt_record
  @oldname_opt_record ||= if (opt_dir = HOMEBREW_PREFIX/"opt").directory?
    opt_dir.subdirs.find do |dir|
      dir.symlink? && dir != opt_record && path.parent == dir.resolved_path.parent
    end
  end
end


531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'brew/Library/Homebrew/keg.rb', line 531

def optlink(mode = OpenStruct.new)
  opt_record.delete if opt_record.symlink? || opt_record.exist?
  make_relative_symlink(opt_record, path, mode)
  aliases.each do |a|
    alias_opt_record = opt_record.parent/a
    alias_opt_record.delete if alias_opt_record.symlink? || alias_opt_record.exist?
    make_relative_symlink(alias_opt_record, path, mode)
  end

  return unless oldname_opt_record

  oldname_opt_record.delete
  make_relative_symlink(oldname_opt_record, path, mode)
end

#optlinked?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'brew/Library/Homebrew/keg.rb', line 251

def optlinked?
  opt_record.symlink? && path == opt_record.resolved_path
end

#plist_installed?Boolean

Returns:

  • (Boolean)


392
393
394
# File 'brew/Library/Homebrew/keg.rb', line 392

def plist_installed?
  !Dir["#{path}/*.plist"].empty?
end

#python_pth_files_installed?Boolean

Returns:

  • (Boolean)


400
401
402
# File 'brew/Library/Homebrew/keg.rb', line 400

def python_pth_files_installed?
  !Dir["#{path}/lib/python2.7/site-packages/*.pth"].empty?
end

#python_site_packages_installed?Boolean

Returns:

  • (Boolean)


396
397
398
# File 'brew/Library/Homebrew/keg.rb', line 396

def python_site_packages_installed?
  (path/"lib/python2.7/site-packages").directory?
end

#rackObject



207
208
209
# File 'brew/Library/Homebrew/keg.rb', line 207

def rack
  path.parent
end

#recursive_fgrep_argsObject



97
98
99
100
# File 'brew/Library/Homebrew/keg_relocate.rb', line 97

def recursive_fgrep_args
  # for GNU grep; overridden for BSD grep on OS X
  "-lr"
end

#relocate_dynamic_linkage(relocation) ⇒ Object



33
34
35
# File 'brew/Library/Homebrew/keg_relocate.rb', line 33

def relocate_dynamic_linkage(_relocation)
  []
end

#remove_linked_keg_recordObject



246
247
248
249
# File 'brew/Library/Homebrew/keg.rb', line 246

def remove_linked_keg_record
  linked_keg_record.unlink
  linked_keg_record.parent.rmdir_if_possible
end

#remove_old_aliasesObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'brew/Library/Homebrew/keg.rb', line 255

def remove_old_aliases
  opt = opt_record.parent

  tap = begin
    to_formula.tap
  rescue FormulaUnavailableError, TapFormulaAmbiguityError,
         TapFormulaWithOldnameAmbiguityError
    # If the formula can't be found, just ignore aliases for now.
    nil
  end

  if tap
    bad_tap_opt = opt/tap.user
    FileUtils.rm_rf bad_tap_opt if !bad_tap_opt.symlink? && bad_tap_opt.directory?
  end

  aliases.each do |a|
    # versioned aliases are handled below
    next if a =~ /.+@./

    alias_symlink = opt/a
    if alias_symlink.symlink? && alias_symlink.exist?
      alias_symlink.delete if alias_symlink.realpath == opt_record.realpath
    elsif alias_symlink.symlink? || alias_symlink.exist?
      alias_symlink.delete
    end
  end

  Pathname.glob("#{opt_record}@*").each do |a|
    a = a.basename.to_s
    next if aliases.include?(a)

    alias_symlink = opt/a
    if alias_symlink.symlink? && alias_symlink.exist?
      next if rack != alias_symlink.realpath.parent
    end

    alias_symlink.delete
  end
end

#remove_oldname_opt_recordObject



510
511
512
513
514
515
516
517
# File 'brew/Library/Homebrew/keg.rb', line 510

def remove_oldname_opt_record
  return unless oldname_opt_record
  return unless oldname_opt_record.resolved_path == path

  @oldname_opt_record.unlink
  @oldname_opt_record.parent.rmdir_if_possible
  @oldname_opt_record = nil
end

#remove_opt_recordObject



296
297
298
299
# File 'brew/Library/Homebrew/keg.rb', line 296

def remove_opt_record
  opt_record.unlink
  opt_record.parent.rmdir_if_possible
end

#replace_locations_with_placeholdersObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'brew/Library/Homebrew/keg_relocate.rb', line 37

def replace_locations_with_placeholders
  relocation = Relocation.new(
    old_prefix:     HOMEBREW_PREFIX.to_s,
    old_cellar:     HOMEBREW_CELLAR.to_s,
    old_repository: HOMEBREW_REPOSITORY.to_s,
    new_prefix:     PREFIX_PLACEHOLDER,
    new_cellar:     CELLAR_PLACEHOLDER,
    new_repository: REPOSITORY_PLACEHOLDER,
  )
  relocate_dynamic_linkage(relocation)
  replace_text_in_files(relocation)
end

#replace_placeholders_with_locations(files, skip_linkage: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'brew/Library/Homebrew/keg_relocate.rb', line 50

def replace_placeholders_with_locations(files, skip_linkage: false)
  relocation = Relocation.new(
    old_prefix:     PREFIX_PLACEHOLDER,
    old_cellar:     CELLAR_PLACEHOLDER,
    old_repository: REPOSITORY_PLACEHOLDER,
    new_prefix:     HOMEBREW_PREFIX.to_s,
    new_cellar:     HOMEBREW_CELLAR.to_s,
    new_repository: HOMEBREW_REPOSITORY.to_s,
  )
  relocate_dynamic_linkage(relocation) unless skip_linkage
  replace_text_in_files(relocation, files: files)
end

#replace_text_in_files(relocation, files: nil) ⇒ Object



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
# File 'brew/Library/Homebrew/keg_relocate.rb', line 63

def replace_text_in_files(relocation, files: nil)
  files ||= text_files | libtool_files

  changed_files = []
  files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest|
    s = first.open("rb", &:read)

    replacements = {
      relocation.old_prefix     => relocation.new_prefix,
      relocation.old_cellar     => relocation.new_cellar,
      relocation.old_repository => relocation.new_repository,
    }
    changed = s.gsub!(Regexp.union(replacements.keys.sort_by(&:length).reverse), replacements)
    next unless changed

    changed_files += [first, *rest].map { |file| file.relative_path_from(path) }

    begin
      first.atomic_write(s)
    rescue SystemCallError
      first.ensure_writable do
        first.open("wb") { |f| f.write(s) }
      end
    else
      rest.each { |file| FileUtils.ln(first, file, force: true) }
    end
  end
  changed_files
end

#require_relocation?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'brew/Library/Homebrew/keg.rb', line 236

def require_relocation?
  @require_relocation
end

#runtime_dependenciesObject



523
524
525
# File 'brew/Library/Homebrew/keg.rb', line 523

def runtime_dependencies
  tab.runtime_dependencies
end


176
177
178
179
180
181
182
183
# File 'brew/Library/Homebrew/keg_relocate.rb', line 176

def symlink_files
  symlink_files = []
  path.find do |pn|
    symlink_files << pn if pn.symlink?
  end

  symlink_files
end

#tabObject



519
520
521
# File 'brew/Library/Homebrew/keg.rb', line 519

def tab
  Tab.for_keg(self)
end

#text_filesObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'brew/Library/Homebrew/keg_relocate.rb', line 120

def text_files
  text_files = []
  return text_files unless which("file") && which("xargs")

  # file has known issues with reading files on other locales. Has
  # been fixed upstream for some time, but a sufficiently new enough
  # file with that fix is only available in macOS Sierra.
  # https://bugs.gw.com/view.php?id=292
  with_custom_locale("C") do
    files = Set.new path.find.reject { |pn|
      next true if pn.symlink?
      next true if pn.directory?
      next false if pn.basename.to_s == "orig-prefix.txt" # for python virtualenvs
      next true if pn == self/".brew/#{name}.rb"
      next true if Metafiles::EXTENSIONS.include?(pn.extname)

      if pn.text_executable?
        text_files << pn
        next true
      end
      false
    }
    output, _status = Open3.capture2("xargs -0 file --no-dereference --print0",
                                     stdin_data: files.to_a.join("\0"))
    # `file` output sometimes contains data from the file, which may include
    # invalid UTF-8 entities, so tell Ruby this is just a bytestring
    output.force_encoding(Encoding::ASCII_8BIT)
    output.each_line do |line|
      path, info = line.split("\0", 2)
      # `file` sometimes prints more than one line of output per file;
      # subsequent lines do not contain a null-byte separator, so `info`
      # will be `nil` for those lines
      next unless info
      next unless info.include?("text")

      path = Pathname.new(path)
      next unless files.include?(path)

      text_files << path
    end
  end

  text_files
end

#to_formulaObject



420
421
422
# File 'brew/Library/Homebrew/keg.rb', line 420

def to_formula
  Formulary.from_keg(self)
end

#uninstallObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'brew/Library/Homebrew/keg.rb', line 301

def uninstall
  CacheStoreDatabase.use(:linkage) do |db|
    break unless db.created?

    LinkageCacheStore.new(path, db).delete!
  end

  path.rmtree
  path.parent.rmdir_if_possible
  remove_opt_record if optlinked?
  remove_old_aliases
  remove_oldname_opt_record
rescue Errno::EACCES, Errno::ENOTEMPTY
  odie <<~EOS
    Could not remove #{name} keg! Do so manually:
      sudo rm -rf #{path}
  EOS
end


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'brew/Library/Homebrew/keg.rb', line 320

def unlink(mode = OpenStruct.new)
  ObserverPathnameExtension.reset_counts!

  dirs = []

  KEG_LINK_DIRECTORIES.map { |d| path/d }.each do |dir|
    next unless dir.exist?

    dir.find do |src|
      dst = HOMEBREW_PREFIX + src.relative_path_from(path)
      dst.extend(ObserverPathnameExtension)

      dirs << dst if dst.directory? && !dst.symlink?

      # check whether the file to be unlinked is from the current keg first
      next unless dst.symlink? && src == dst.resolved_path

      if mode.dry_run
        puts dst
        Find.prune if src.directory?
        next
      end

      dst.uninstall_info if dst.to_s =~ INFOFILE_RX
      dst.unlink
      remove_old_aliases
      Find.prune if src.directory?
    end
  end

  unless mode.dry_run
    remove_linked_keg_record if linked?
    dirs.reverse_each(&:rmdir_if_possible)
  end

  ObserverPathnameExtension.n
end

#versionObject



415
416
417
418
# File 'brew/Library/Homebrew/keg.rb', line 415

def version
  require "pkg_version"
  PkgVersion.parse(path.basename.to_s)
end