module Pkg::Util::File

Public Class Methods

directories(dir) click to toggle source

Returns an array of all the directories at the top level of #{dir}

# File lib/packaging/util/file.rb, line 28
def directories(dir)
  if File.directory?(dir)
    Dir.chdir(dir) do
      Dir.glob("*").select { |entry| File.directory?(entry) }
    end
  end
end
directory?(file) click to toggle source
# File lib/packaging/util/file.rb, line 12
def directory?(file)
  ::File.directory?(file)
end
empty_dir?(dir) click to toggle source
# File lib/packaging/util/file.rb, line 22
def empty_dir?(dir)
  File.exist?(dir) and File.directory?(dir) and Dir["#{dir}/**/*"].empty?
end
erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) click to toggle source
# File lib/packaging/util/file.rb, line 66
def erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding })
  outfile ||= File.join(mktemp, File.basename(erbfile).sub(File.extname(erbfile), ""))
  output = erb_string(erbfile, opts[:binding])
  File.open(outfile, 'w') { |f| f.write output }
  puts "Generated: #{outfile}"
  FileUtils.rm_rf erbfile if remove_orig
  outfile
end
erb_string(erbfile, b = binding) click to toggle source
# File lib/packaging/util/file.rb, line 60
def erb_string(erbfile, b = binding)
  template = File.read(erbfile)
  message  = ERB.new(template, nil, "-")
  message.result(b)
end
exist?(file) click to toggle source
# File lib/packaging/util/file.rb, line 7
def exist?(file)
  ::File.exist?(file)
end
Also aliased as: exists?
exists?(file)
Alias for: exist?
file_exists?(file, args = { :required => false }) click to toggle source
# File lib/packaging/util/file.rb, line 42
def file_exists?(file, args = { :required => false })
  exists = File.exist? file
  if !exists and args[:required]
    fail "Required file #{file} could not be found"
  end
  exists
end
file_writable?(file, args = { :required => false }) click to toggle source
# File lib/packaging/util/file.rb, line 50
def file_writable?(file, args = { :required => false })
  writable = File.writable? file
  if !writable and args[:required]
    fail "File #{file} is not writable"
  end
  writable
end
files_with_ext(dir, ext) click to toggle source

Returns an array of all files with #{ext} inside #{dir}

# File lib/packaging/util/file.rb, line 37
def files_with_ext(dir, ext)
  Dir.glob("#{dir}/**/*#{ext}")
end
get_temp()
Alias for: mktemp
install_files_into_dir(file_patterns, workdir) click to toggle source
# File lib/packaging/util/file.rb, line 87
def install_files_into_dir(file_patterns, workdir)
  install = []
  # We need to add our list of file patterns from the configuration; this
  # used to be a list of "things to copy recursively", which would install
  # editor backup files and other nasty things.
  #
  # This handles that case correctly, with a deprecation warning, to augment
  # our FileList with the right things to put in place.
  #
  # Eventually, when all our projects are migrated to the new standard, we
  # can drop this in favour of just pushing the patterns directly into the
  # FileList and eliminate many lines of code and comment.
  Dir.chdir(Pkg::Config.project_root) do
    file_patterns.each do |pattern|
      if File.directory?(pattern) and !Pkg::Util::File.empty_dir?(pattern)
        install << Dir[pattern + "/**/*"]
      else
        install << Dir[pattern]
      end
    end
    install.flatten!

    # Transfer all the files and symlinks into the working directory...
    install = install.select { |x| File.file?(x) or File.symlink?(x) or Pkg::Util::File.empty_dir?(x) }

    install.each do |file|
      if Pkg::Util::File.empty_dir?(file)
        FileUtils.mkpath(File.join(workdir, file), :verbose => false)
      else
        FileUtils.mkpath(File.dirname(File.join(workdir, file)), :verbose => false)
        FileUtils.cp(file, File.join(workdir, file), :verbose => false, :preserve => true)
      end
    end
  end
  Pkg::Util::Version.versionbump(workdir) if Pkg::Config.update_version_file
end
mktemp() click to toggle source
# File lib/packaging/util/file.rb, line 16
def mktemp
  mktemp = Pkg::Util::Tool.find_tool('mktemp', :required => true)
  stdout, _, _ = Pkg::Util::Execution.capture3("#{mktemp} -d -t pkgXXXXXX")
  stdout.strip
end
Also aliased as: get_temp
untar_into(source, target = nil, options = "") click to toggle source
# File lib/packaging/util/file.rb, line 75
def untar_into(source, target = nil, options = "")
  tar = Pkg::Util::Tool.find_tool('tar', :required => true)
  # We only accept a writable directory as a target
  if target and !target.empty? and file_writable?(target) and File.directory?(target)
    target_opts = "-C #{target}"
  end
  if file_exists?(source, :required => true)
    stdout, _, _ = Pkg::Util::Execution.capture3(%Q(#{tar} #{options} #{target_opts} -xf #{source}))
    stdout
  end
end