module Bundler

def requires_sudo?

def requires_sudo?
  return @requires_sudo if defined?(@requires_sudo_ran)
  sudo_present = which "sudo" if settings.allow_sudo?
  if sudo_present
    # the bundle path and subdirectories need to be writable for Rubygems
    # to be able to unpack and install gems without exploding
    path = bundle_path
    path = path.parent until path.exist?
    # bins are written to a different location on OS X
    bin_dir = Pathname.new(Bundler.system_bindir)
    bin_dir = bin_dir.parent until bin_dir.exist?
    # if any directory is not writable, we need sudo
    files = [path, bin_dir] | Dir[path.join("build_info/*").to_s] | Dir[path.join("*").to_s]
    sudo_needed = files.any? {|f| !File.writable?(f) }
  end
  @requires_sudo_ran = true
  @requires_sudo = settings.allow_sudo? && sudo_present && sudo_needed
end