<commit_before>require 'thread'
require 'fspath'

class ImageOptim
  class BinNotFoundError < StandardError; end
  class BinResolver
    attr_reader :dir
    def initialize
      @bins = {}
      @lock = Mutex.new
    end

    def resolve!(bin)
      bin = bin.to_sym
      resolving(bin) do
        @bins[bin] = resolve?(bin)
      end
      @bins[bin] or raise BinNotFoundError, "`#{bin}` not found"
    end

    VENDOR_PATH = File.expand_path('../../../vendor', __FILE__)

    def env_path
      [dir, ENV['PATH'], VENDOR_PATH].compact.join(':')
    end

  private

    def resolving(bin)
      unless @bins.include?(bin)
        @lock.synchronize do
          unless @bins.include?(bin)
            yield
          end
        end
      end
    end

    def resolve?(bin)
      if path = ENV["#{bin}_bin".upcase]
        unless @dir
          @dir = FSPath.temp_dir
          at_exit{ FileUtils.remove_entry_secure @dir }
        end
        symlink = @dir / bin
        symlink.make_symlink(File.expand_path(path))
      end
      accessible?(bin)
    end

    def accessible?(bin)
      capture_output("which #{bin.to_s.shellescape}") != ''
    end

    def capture_output(command)
      `env PATH=#{env_path.shellescape} #{command}`
    end
  end
end
<commit_msg>renamed var bin => name<commit_after>require 'thread'
require 'fspath'

class ImageOptim
  class BinNotFoundError < StandardError; end
  class BinResolver
    attr_reader :dir
    def initialize
      @bins = {}
      @lock = Mutex.new
    end

    def resolve!(name)
      name = name.to_sym
      resolving(name) do
        @bins[name] = resolve?(name)
      end
      @bins[name] or raise BinNotFoundError, "`#{name}` not found"
    end

    VENDOR_PATH = File.expand_path('../../../vendor', __FILE__)

    def env_path
      [dir, ENV['PATH'], VENDOR_PATH].compact.join(':')
    end

  private

    def resolving(name)
      unless @bins.include?(name)
        @lock.synchronize do
          unless @bins.include?(name)
            yield
          end
        end
      end
    end

    def resolve?(name)
      if path = ENV["#{name}_bin".upcase]
        unless @dir
          @dir = FSPath.temp_dir
          at_exit{ FileUtils.remove_entry_secure @dir }
        end
        symlink = @dir / name
        symlink.make_symlink(File.expand_path(path))
      end
      accessible?(name)
    end

    def accessible?(name)
      capture_output("which #{name.to_s.shellescape}") != ''
    end

    def capture_output(command)
      `env PATH=#{env_path.shellescape} #{command}`
    end
  end
end

{'fraction_non_alphanumeric': 0.11530970003895598, 'fraction_numerical': 0.0, 'mean_word_length': 2.1432068543451654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '4376687', 'n_tokens_mistral': 838, 'n_tokens_neox': 829, 'n_words': 189}