Class: Keg

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

Defined Under Namespace

Classes: AlreadyLinkedError, ConflictError, DirectoryNotWritableError, LinkError, Relocation

Constant Summary

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]+(@.+)?)?}
INFOFILE_RX =
%r{info/([^.].*?\.info|dir)$}
TOP_LEVEL_DIRECTORIES =
%w[bin etc include lib sbin share var Frameworks].freeze
ALL_TOP_LEVEL_DIRECTORIES =
(TOP_LEVEL_DIRECTORIES + %w[lib/pkgconfig share/locale share/man opt]).freeze
PRUNEABLE_DIRECTORIES =
%w[bin etc include lib sbin share opt Frameworks LinkedKegs var/homebrew/linked].map do |dir|
  case dir
  when "LinkedKegs"
    HOMEBREW_LIBRARY/dir
  else
    HOMEBREW_PREFIX/dir
  end
end
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@@".freeze
CELLAR_PLACEHOLDER =
"@@HOMEBREW_CELLAR@@".freeze
REPOSITORY_PLACEHOLDER =
"@@HOMEBREW_REPOSITORY@@".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)$}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Keg

Returns a new instance of Keg



183
184
185
186
187
188
189
190
191
192
# File 'Library/Homebrew/keg.rb', line 183

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



174
175
176
# File 'Library/Homebrew/keg.rb', line 174

def linked_keg_record
  @linked_keg_record
end

#nameObject (readonly)

Returns the value of attribute name



174
175
176
# File 'Library/Homebrew/keg.rb', line 174

def name
  @name
end

#opt_recordObject (readonly)

Returns the value of attribute opt_record



174
175
176
# File 'Library/Homebrew/keg.rb', line 174

def opt_record
  @opt_record
end

Class Method Details

.allObject



170
171
172
# File 'Library/Homebrew/keg.rb', line 170

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

.file_linked_libraries(file, string) ⇒ Object



174
175
176
# File 'Library/Homebrew/keg_relocate.rb', line 174

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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
# File 'Library/Homebrew/keg.rb', line 103

def self.find_some_installed_dependents(kegs)
  # First, check in the tabs of installed Formulae.
  kegs.each do |keg|
    # Don't include dependencies of kegs that were in the given array.
    dependents = keg.installed_dependents - kegs
    dependents.map! { |d| "#{d.name} #{d.version}" }
    return [keg], dependents if dependents.any?
  end

  # Some kegs won't have modern Tabs with the dependencies listed.
  # In this case, fall back to Formula#missing_dependencies.

  # Find formulae that didn't have dependencies saved in all of their kegs,
  # so need them to be calculated now.
  #
  # This happens after the initial dependency check because it's sloooow.
  remaining_formulae = Formula.installed.select do |f|
    installed_kegs = f.installed_kegs

    # Don't include dependencies of kegs that were in the given array.
    next false if (installed_kegs - kegs).empty?

    installed_kegs.any? { |k| Tab.for_keg(k).runtime_dependencies.nil? }
  end

  keg_names = kegs.map(&:name)
  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
      [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, Tab.for_keg(keg).tap]
    end
  end

  remaining_formulae.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.sort_by(&:version).last
    end.compact

    next unless required_kegs.any?

    return required_kegs, [dependent.to_s]
  end

  nil
end

.for(path) ⇒ Object

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

Raises:



161
162
163
164
165
166
167
168
# File 'Library/Homebrew/keg.rb', line 161

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

Instance Method Details

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



204
205
206
# File 'Library/Homebrew/keg.rb', line 204

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

#aliasesObject



469
470
471
472
473
# File 'Library/Homebrew/keg.rb', line 469

def aliases
  Formulary.from_rack(rack).aliases
rescue FormulaUnavailableError
  []
end

#appsObject



337
338
339
340
# File 'Library/Homebrew/keg.rb', line 337

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

#change_dylib_id(id, file) ⇒ Object



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

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.undent
    Failed changing dylib ID of #{file}
      from #{file.dylib_id}
        to #{id}
  EOS
  raise
end

#change_install_name(old, new, file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'Library/Homebrew/os/mac/keg.rb', line 16

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.undent
    Failed changing install name in #{file}
      from #{old}
        to #{new}
  EOS
  raise
end

#completion_installed?(shell) ⇒ Boolean

Returns:

  • (Boolean)


301
302
303
304
305
306
307
308
309
310
# File 'Library/Homebrew/keg.rb', line 301

def completion_installed?(shell)
  dir = case shell
  when :bash then path.join("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 && dir.directory? && !dir.children.empty?
end

#delete_pyc_files!Object



489
490
491
# File 'Library/Homebrew/keg.rb', line 489

def delete_pyc_files!
  find { |pn| pn.delete if %w[.pyc .pyo].include?(pn.extname) }
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++



47
48
49
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 47

def detect_cxx_stdlibs(_options = {})
  []
end

#dylib_id_for(file) ⇒ Object



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

def dylib_id_for(file)
  return file.dylib_id if filename_contains_metavariable?(file.dylib_id)

  # 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.join(relative_dirname, basename).to_s
end

#each_install_name_for(file, &block) ⇒ Object



85
86
87
88
89
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 85

def each_install_name_for(file, &block)
  dylibs = file.dynamically_linked_libraries
  dylibs.reject! { |fn| filename_contains_metavariable?(fn) }
  dylibs.each(&block)
end

#each_unique_file_matching(string) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'Library/Homebrew/keg_relocate.rb', line 99

def each_unique_file_matching(string)
  Utils.popen_read("/usr/bin/fgrep", "-lr", 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

#elisp_installed?Boolean

Returns:

  • (Boolean)


342
343
344
345
# File 'Library/Homebrew/keg.rb', line 342

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)


209
210
211
212
213
214
215
216
217
218
219
# File 'Library/Homebrew/keg.rb', line 209

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

#filename_contains_metavariable?(fn) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 81

def filename_contains_metavariable?(fn)
  fn =~ /^@(loader_|executable_|r)path/
end

#find_dylib(bad_name) ⇒ Object



113
114
115
116
117
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 113

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



105
106
107
108
109
110
111
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 105

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



14
15
16
17
18
19
20
21
22
23
# File 'Library/Homebrew/keg_relocate.rb', line 14

def fix_dynamic_linkage
  symlink_files.each do |file|
    link = file.readlink
    # Don't fix relative symlinks
    next unless link.absolute?
    if link.to_s.start_with?(HOMEBREW_CELLAR.to_s) || link.to_s.start_with?(HOMEBREW_PREFIX.to_s)
      FileUtils.ln_sf(link.relative_path_from(file.parent), file)
    end
  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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 64

def fixed_name(file, bad_name)
  if bad_name.start_with? PREFIX_PLACEHOLDER
    bad_name.sub(PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s)
  elsif bad_name.start_with? CELLAR_PLACEHOLDER
    bad_name.sub(CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s)
  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 (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)


312
313
314
315
316
317
318
319
320
321
322
323
# File 'Library/Homebrew/keg.rb', line 312

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



24
25
26
27
28
29
30
31
32
33
# File 'Library/Homebrew/keg_relocate.rb', line 24

def fix_dynamic_linkage
  symlink_files.each do |file|
    link = file.readlink
    # Don't fix relative symlinks
    next unless link.absolute?
    if link.to_s.start_with?(HOMEBREW_CELLAR.to_s) || link.to_s.start_with?(HOMEBREW_PREFIX.to_s)
      FileUtils.ln_sf(link.relative_path_from(file.parent), file)
    end
  end
end

#inspectObject



200
201
202
# File 'Library/Homebrew/keg.rb', line 200

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

#installed_dependentsObject



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'Library/Homebrew/keg.rb', line 356

def installed_dependents
  return [] unless optlinked?
  tap = Tab.for_keg(self).source["tap"]
  Keg.all.select do |keg|
    tab = Tab.for_keg(keg)
    next if tab.runtime_dependencies.nil?
    tab.runtime_dependencies.any? do |dep|
      # Resolve formula rather than directly comparing names
      # in case of conflicts between formulae from different taps.
      begin
        dep_formula = Formulary.factory(dep["full_name"])
        dep_formula == to_formula
      rescue FormulaUnavailableError
        next "#{tap}/#{name}" == dep["full_name"]
      end
    end
  end
end

#libObject



111
112
113
# File 'Library/Homebrew/keg_relocate.rb', line 111

def lib
  path.join("lib")
end

#libtool_filesObject



155
156
157
158
159
160
161
162
163
# File 'Library/Homebrew/keg_relocate.rb', line 155

def libtool_files
  libtool_files = []

  path.find do |pn|
    next if pn.symlink? || pn.directory? || pn.extname != ".la"
    libtool_files << pn
  end
  libtool_files
end


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
# File 'Library/Homebrew/keg.rb', line 383

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)


225
226
227
228
229
# File 'Library/Homebrew/keg.rb', line 225

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

#lockObject



291
292
293
294
295
296
297
298
299
# File 'Library/Homebrew/keg.rb', line 291

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'Library/Homebrew/extend/os/mac/keg_relocate.rb', line 119

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



375
376
377
378
379
380
381
# File 'Library/Homebrew/keg.rb', line 375

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


475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'Library/Homebrew/keg.rb', line 475

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)


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

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

#plist_installed?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'Library/Homebrew/keg.rb', line 325

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

#python_pth_files_installed?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'Library/Homebrew/keg.rb', line 333

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

#python_site_packages_installed?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'Library/Homebrew/keg.rb', line 329

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

#rackObject



194
195
196
# File 'Library/Homebrew/keg.rb', line 194

def rack
  path.parent
end

#relocate_dynamic_linkage(relocation) ⇒ Object



26
27
28
# File 'Library/Homebrew/keg_relocate.rb', line 26

def relocate_dynamic_linkage(_relocation)
  []
end

#remove_linked_keg_recordObject



231
232
233
234
# File 'Library/Homebrew/keg.rb', line 231

def remove_linked_keg_record
  linked_keg_record.unlink
  linked_keg_record.parent.rmdir_if_possible
end

#remove_oldname_opt_recordObject



461
462
463
464
465
466
467
# File 'Library/Homebrew/keg.rb', line 461

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



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

def remove_opt_record
  opt_record.unlink
  aliases.each do |a|
    next if !opt_record.symlink? && !opt_record.exist?
    (opt_record.parent/a).delete
  end
  opt_record.parent.rmdir_if_possible
end

#replace_locations_with_placeholdersObject



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

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'Library/Homebrew/keg_relocate.rb', line 43

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



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

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,
    }

    # Order matters here since `HOMEBREW_CELLAR` and `HOMEBREW_REPOSITORY` are
    # children of `HOMEBREW_PREFIX` by default.
    regexp = Regexp.union(
      relocation.old_cellar,
      relocation.old_repository,
      relocation.old_prefix,
    )

    changed = s.gsub!(regexp, 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)


221
222
223
# File 'Library/Homebrew/keg.rb', line 221

def require_relocation?
  @require_relocation
end


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

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

  symlink_files
end

#text_filesObject



115
116
117
118
119
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
# File 'Library/Homebrew/keg_relocate.rb', line 115

def text_files
  text_files = []
  return text_files unless File.exist?("/usr/bin/file")

  # 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 true if Metafiles::EXTENSIONS.include?(pn.extname)
      if pn.text_executable?
        text_files << pn
        next true
      end
      false
    }
    output, _status = Open3.capture2("/usr/bin/xargs -0 /usr/bin/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



352
353
354
# File 'Library/Homebrew/keg.rb', line 352

def to_formula
  Formulary.from_keg(self)
end

#uninstallObject



249
250
251
252
253
254
# File 'Library/Homebrew/keg.rb', line 249

def uninstall
  path.rmtree
  path.parent.rmdir_if_possible
  remove_opt_record if optlinked?
  remove_oldname_opt_record
end


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
# File 'Library/Homebrew/keg.rb', line 256

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

  dirs = []

  TOP_LEVEL_DIRECTORIES.map { |d| path.join(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
      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



347
348
349
350
# File 'Library/Homebrew/keg.rb', line 347

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