<commit_before>class Keg
  PREFIX_PLACEHOLDER = "@@HOMEBREW_PREFIX@@".freeze
  CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@".freeze

  def fix_install_names options={}
    mach_o_files.each do |file|
      file.ensure_writable do
        change_rpath(file)
        change_dylib_id(dylib_id_for(file, options), file) if file.dylib?

        each_install_name_for(file) do |bad_name|
          # Don't fix absolute paths unless they are rooted in the build directory
          next if bad_name.start_with? '/' and not bad_name.start_with? HOMEBREW_TEMP.to_s

          new_name = fixed_name(file, bad_name)
          change_install_name(bad_name, new_name, file) unless new_name == bad_name
        end
      end
    end
  end

  def relocate_install_names old_prefix, new_prefix, old_cellar, new_cellar, options={}
    mach_o_files.each do |file|
      file.ensure_writable do
        if file.dylib?
          id = dylib_id_for(file, options).sub(old_prefix, new_prefix)
          change_dylib_id(id, file)
        end

        each_install_name_for(file) do |old_name|
          if old_name.start_with? old_cellar
            new_name = old_name.sub(old_cellar, new_cellar)
          elsif old_name.start_with? old_prefix
            new_name = old_name.sub(old_prefix, new_prefix)
          end

          change_install_name(old_name, new_name, file) if new_name
        end
      end
    end

    files = pkgconfig_files | libtool_files | script_files

    files.group_by { |f| f.stat.ino }.each_value do |first, *rest|
      s = first.open("rb", &:read)
      changed = s.gsub!(old_cellar, new_cellar)
      changed = s.gsub!(old_prefix, new_prefix) || changed

      begin
        first.atomic_write(s)
      rescue Errno::EACCES
        first.ensure_writable do
          first.open("wb") { |f| f.write(s) }
        end
      else
        rest.each { |file| FileUtils.ln(first, file, :force => true) }
      end if changed
    end
  end

  def change_rpath(file)
    return unless OS.linux?
    patchelf = Formula["patchelf"]
    return unless patchelf.installed?
    glibc = Formula["glibc"]
    cmd = "#{patchelf.bin}/patchelf --set-rpath #{HOMEBREW_PREFIX}/lib"
    cmd << " --set-interpreter #{glibc.opt_lib}/ld-linux-x86-64.so.2" if glibc.installed?
    cmd << " #{file}"
    puts "Setting RPATH of #{file}\n#{cmd}" if ARGV.debug?
    system cmd
  end

  def change_dylib_id(id, file)
    puts "Changing dylib ID in #{file} to #{id}" if ARGV.debug?
    install_name_tool("-id", id, file)
  end

  def change_install_name(old, new, file)
    puts "Changing install name in #{file} from #{old} to #{new}" if ARGV.debug?
    install_name_tool("-change", old, new, file)
  end
<commit_msg>Linuxbrew: Use safe_system for patchelf<commit_after>class Keg
  PREFIX_PLACEHOLDER = "@@HOMEBREW_PREFIX@@".freeze
  CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@".freeze

  def fix_install_names options={}
    mach_o_files.each do |file|
      file.ensure_writable do
        change_rpath(file)
        change_dylib_id(dylib_id_for(file, options), file) if file.dylib?

        each_install_name_for(file) do |bad_name|
          # Don't fix absolute paths unless they are rooted in the build directory
          next if bad_name.start_with? '/' and not bad_name.start_with? HOMEBREW_TEMP.to_s

          new_name = fixed_name(file, bad_name)
          change_install_name(bad_name, new_name, file) unless new_name == bad_name
        end
      end
    end
  end

  def relocate_install_names old_prefix, new_prefix, old_cellar, new_cellar, options={}
    mach_o_files.each do |file|
      file.ensure_writable do
        if file.dylib?
          id = dylib_id_for(file, options).sub(old_prefix, new_prefix)
          change_dylib_id(id, file)
        end

        each_install_name_for(file) do |old_name|
          if old_name.start_with? old_cellar
            new_name = old_name.sub(old_cellar, new_cellar)
          elsif old_name.start_with? old_prefix
            new_name = old_name.sub(old_prefix, new_prefix)
          end

          change_install_name(old_name, new_name, file) if new_name
        end
      end
    end

    files = pkgconfig_files | libtool_files | script_files

    files.group_by { |f| f.stat.ino }.each_value do |first, *rest|
      s = first.open("rb", &:read)
      changed = s.gsub!(old_cellar, new_cellar)
      changed = s.gsub!(old_prefix, new_prefix) || changed

      begin
        first.atomic_write(s)
      rescue Errno::EACCES
        first.ensure_writable do
          first.open("wb") { |f| f.write(s) }
        end
      else
        rest.each { |file| FileUtils.ln(first, file, :force => true) }
      end if changed
    end
  end

  def change_rpath(file)
    return unless OS.linux?
    patchelf = Formula["patchelf"]
    return unless patchelf.installed?
    glibc = Formula["glibc"]
    cmd = "#{patchelf.opt_bin}/patchelf --set-rpath #{HOMEBREW_PREFIX}/lib"
    cmd << " --set-interpreter #{glibc.opt_lib}/ld-linux-x86-64.so.2" if glibc.installed?
    cmd << " #{file}"
    puts "Setting RPATH of #{file}" if ARGV.debug?
    safe_system cmd
  end

  def change_dylib_id(id, file)
    puts "Changing dylib ID in #{file} to #{id}" if ARGV.debug?
    install_name_tool("-id", id, file)
  end

  def change_install_name(old, new, file)
    puts "Changing install name in #{file} from #{old} to #{new}" if ARGV.debug?
    install_name_tool("-change", old, new, file)
  end

{'fraction_non_alphanumeric': 0.10585627193792721, 'fraction_numerical': 0.0018474043968224644, 'mean_word_length': 2.8019662921348316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6707088', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1911, 'n_words': 432}