class Vanagon::Component::Source::Local

Constants

ARCHIVE_EXTENSIONS

Extensions for files we intend to unpack during the build

Attributes

cleanup[RW]
extension[RW]
file[RW]
url[RW]
workdir[RW]

Public Class Methods

archive_extensions() click to toggle source
# File lib/vanagon/component/source/local.rb, line 38
def archive_extensions
  ARCHIVE_EXTENSIONS.values.flatten
end
mangle(path) click to toggle source

If a scheme is specified as “file://”, this will return strip off the scheme and delimiters – we need to do this because once upon a time we allowed specifying files with no strong specifications for where they should be located.

# File lib/vanagon/component/source/local.rb, line 34
def mangle(path)
  path.gsub(%r{^file://}, '')
end
new(path, workdir:, **options) click to toggle source

Constructor for the File source type

@param path [String] path of the local file to copy @param workdir [String] working directory to copy <path> to

# File lib/vanagon/component/source/local.rb, line 47
def initialize(path, workdir:, **options)
  @url = ::Pathname.new(mangle(path))
  @workdir = ::Pathname.new(workdir)
end
valid_file?(target_file) click to toggle source
# File lib/vanagon/component/source/local.rb, line 26
def valid_file?(target_file)
  File.exist?(mangle(target_file.to_s))
end

Public Instance Methods

archive?() click to toggle source
# File lib/vanagon/component/source/local.rb, line 136
def archive?
  archive_extensions.include?(extension)
end
archive_extensions() click to toggle source
# File lib/vanagon/component/source/local.rb, line 132
def archive_extensions
  self.class.archive_extensions
end
copy() click to toggle source

Moves file from source to workdir

@raise [RuntimeError, Vanagon::Error] an exception is raised if the URI scheme cannot be handled

# File lib/vanagon/component/source/local.rb, line 60
def copy
  VanagonLogger.info "Copying file '#{url.basename}' to workdir"

  FileUtils.cp_r(url, file)
end
Also aliased as: fetch
decompressor() click to toggle source
# File lib/vanagon/component/source/local.rb, line 140
def decompressor
  @decompressor ||= ARCHIVE_EXTENSIONS.select { |k, v| v.include? extension }.keys.first
end
dirname() click to toggle source

The dirname to reference when building from the source

@return [String] the directory that should be traversed into to build this source @raise [RuntimeError] if the @extension for the @file isn’t currently handled by the method

# File lib/vanagon/component/source/local.rb, line 148
def dirname
  # We are not treating file as a Pathname since other sources can inherit from this class
  # which could cause file to be a URI instead of a string.
  if archive? || File.directory?(file)
    File.basename(file, extension)
  else
    './'
  end
end
extname() click to toggle source

Returns the extension for @file

@return [String] the extension of @file

# File lib/vanagon/component/source/local.rb, line 126
def extname
  extension_match = file.to_s.match %r{#{Regexp.union(archive_extensions)}\Z}
  return extension_match.to_s if extension_match
  File.extname(file)
end
extract(tar = "tar") click to toggle source

Gets the command to extract the archive given if needed (uses @extension)

@param tar [String] the tar command to use @return [String, nil] command to extract the source @raise [RuntimeError] an exception is raised if there is no known extraction method for @extension

# File lib/vanagon/component/source/local.rb, line 80
def extract(tar = "tar") # rubocop:disable Metrics/AbcSize
  # Extension does not appear to be an archive, so "extract" is a no-op
  return ': nothing to extract' unless archive_extensions.include?(extension)

  case decompressor
  when "7z"
    %(7z x "#{file}")
  when "bzip2"
    %(bunzip2 "#{file}")
  when "cpio"
    %(
      mkdir "#{file.basename}" &&
      pushd "#{file.basename}" 2>&1 > /dev/null &&
      cpio -idv < "#{file}" &&
      popd 2>&1 > /dev/null
    ).undent
  when "gzip"
    %(gunzip "#{file}")
  when "rar"
    %(unrar x "#{file}")
  when "tar", "txz"
    %(#{tar} xf "#{file}")
  when "tbz2"
    %(bunzip2 -c "#{file}" | #{tar} xf -)
  when "tgz"
    %(gunzip -c "#{file}" | #{tar} xf -)
  when "xz"
    %(unxz "#{file}")
  when "zip"
    "unzip -d '#{File.basename(file, '.zip')}' '#{file}' || 7za x -r -tzip -o'#{File.basename(file, '.zip')}' '#{file}'"
  else
    raise Vanagon::Error, "Don't know how to decompress #{extension} archives"
  end
end
fetch()
Alias for: copy
mangle(path) click to toggle source

Wrapper around the class method ‘.mangle’

# File lib/vanagon/component/source/local.rb, line 159
def mangle(path)
  self.class.mangle(path)
end
verify() click to toggle source

Local files need no checksum so this is a noop

# File lib/vanagon/component/source/local.rb, line 53
def verify
  # nothing to do here, so just return
end