<commit_before>class Hbc::Pkg
  def self.all_matching(regexp, command)
    command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map { |package_id|
      new(package_id.chomp, command)
    }
  end

  attr_reader :package_id

  def initialize(package_id, command = Hbc::SystemCommand)
    @package_id = package_id
    @command = command
  end

  def uninstall
    odebug "Deleting pkg files"
    pkgutil_bom_files.each_slice(500) do |file_slice|
      @command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
    end
    odebug "Deleting pkg symlinks and special files"
    pkgutil_bom_specials.each_slice(500) do |file_slice|
      @command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
    end
    odebug "Deleting pkg directories"
    _deepest_path_first(pkgutil_bom_dirs).each do |dir|
      next unless dir.exist? && !MacOS.undeletable?(dir)
      _with_full_permissions(dir) do
        _clean_broken_symlinks(dir)
        _clean_ds_store(dir)
        _rmdir(dir)
      end
    end
    forget
  end

  def forget
    odebug "Unregistering pkg receipt (aka forgetting)"
    @command.run!("/usr/sbin/pkgutil", args: ["--forget", package_id], sudo: true)
  end

  def pkgutil_bom_files
    @command.run!("/usr/sbin/pkgutil", args: ["--only-files", "--files", package_id])
            .stdout
            .split("\n")
            .map { |path| root.join(path) }
  end

  def pkgutil_bom_dirs
    @command.run!("/usr/sbin/pkgutil", args: ["--only-dirs", "--files", package_id])
            .stdout
            .split("\n")
            .map { |path| root.join(path) }
  end

  def pkgutil_bom_all
    @command.run!("/usr/sbin/pkgutil", args: ["--files", package_id])
            .stdout
            .split("\n")
            .map { |path| root.join(path) }
  end

  def pkgutil_bom_specials
    pkgutil_bom_all - pkgutil_bom_files - pkgutil_bom_dirs
  end

  def root
    @root ||= Pathname(info.fetch("volume")).join(info.fetch("install-location"))
  end

  def info
    @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id])
            .plist
  end

  def _rmdir(path)
    @command.run!("/bin/rmdir", args: ["--", path], sudo: true) if path.children.empty?
  end

  def _with_full_permissions(path)
    original_mode = (path.stat.mode % 0o1000).to_s(8)
    # TODO: similarly read and restore macOS flags (cf man chflags)
    @command.run!("/bin/chmod", args: ["--", "777", path], sudo: true)
    yield
  ensure
    if path.exist? # block may have removed dir
      @command.run!("/bin/chmod", args: ["--", original_mode, path], sudo: true)<commit_msg>Refactor `pkgutil_bom`. (#23828)<commit_after>class Hbc::Pkg
  def self.all_matching(regexp, command)
    command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map { |package_id|
      new(package_id.chomp, command)
    }
  end

  attr_reader :package_id

  def initialize(package_id, command = Hbc::SystemCommand)
    @package_id = package_id
    @command = command
  end

  def uninstall
    odebug "Deleting pkg files"
    pkgutil_bom_files.each_slice(500) do |file_slice|
      @command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
    end
    odebug "Deleting pkg symlinks and special files"
    pkgutil_bom_specials.each_slice(500) do |file_slice|
      @command.run("/bin/rm", args: file_slice.unshift("-f", "--"), sudo: true)
    end
    odebug "Deleting pkg directories"
    _deepest_path_first(pkgutil_bom_dirs).each do |dir|
      next unless dir.exist? && !MacOS.undeletable?(dir)
      _with_full_permissions(dir) do
        _clean_broken_symlinks(dir)
        _clean_ds_store(dir)
        _rmdir(dir)
      end
    end
    forget
  end

  def forget
    odebug "Unregistering pkg receipt (aka forgetting)"
    @command.run!("/usr/sbin/pkgutil", args: ["--forget", package_id], sudo: true)
  end

  def pkgutil_bom(*type)
    @command.run!("/usr/sbin/pkgutil", args: [*type, "--files", package_id].compact)
            .stdout
            .split("\n")
            .map { |path| root.join(path) }
  end

  def pkgutil_bom_files
    @pkgutil_bom_files ||= pkgutil_bom("--only-files")
  end

  def pkgutil_bom_dirs
    @pkgutil_bom_dirs ||= pkgutil_bom("--only-dirs")
  end

  def pkgutil_bom_all
    @pkgutil_bom_all ||= pkgutil_bom
  end

  def pkgutil_bom_specials
    pkgutil_bom_all - pkgutil_bom_files - pkgutil_bom_dirs
  end

  def root
    @root ||= Pathname(info.fetch("volume")).join(info.fetch("install-location"))
  end

  def info
    @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id])
            .plist
  end

  def _rmdir(path)
    @command.run!("/bin/rmdir", args: ["--", path], sudo: true) if path.children.empty?
  end

  def _with_full_permissions(path)
    original_mode = (path.stat.mode % 0o1000).to_s(8)
    # TODO: similarly read and restore macOS flags (cf man chflags)
    @command.run!("/bin/chmod", args: ["--", "777", path], sudo: true)
    yield
  ensure
    if path.exist? # block may have removed dir
      @command.run!("/bin/chmod", args: ["--", original_mode, path], sudo: true)
{'fraction_non_alphanumeric': 0.1517160686427457, 'fraction_numerical': 0.006825273010920437, 'mean_word_length': 2.9976617303195634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1623617', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1834, 'n_words': 375}