Class: Vanagon::Component::Source::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/vanagon/component/source/local.rb

Direct Known Subclasses

Http

Constant Summary collapse

ARCHIVE_EXTENSIONS =

Extensions for files we intend to unpack during the build

{
  "7z" => %w[.7z],
  "bzip2" => %w[.bz2 .bz],
  "cpio" => %w[.cpio],
  "gzip" => %w[.gz .z],
  "rar" => %w[.rar],
  "tar" => %w[.tar],
  "tbz2" => %w[.tar.bz2 .tbz2 .tbz],
  "tgz" => %w[.tar.gz .tgz],
  "txz" => %w[.tar.xz .txz],
  "xz" => %w[.xz],
  "zip" => %w[.zip],
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, workdir:, **options) ⇒ Local

Constructor for the File source type

Parameters:

  • path (String)

    path of the local file to copy

  • workdir (String)

    working directory to copy <path> to



46
47
48
49
# File 'lib/vanagon/component/source/local.rb', line 46

def initialize(path, workdir:, **options)
  @url = ::Pathname.new(mangle(path))
  @workdir = ::Pathname.new(workdir)
end

Instance Attribute Details

#cleanupString

Return the correct incantation to cleanup the source archive and source directory for a given source

Returns:

  • (String)

    command to cleanup the source

Raises:

  • (RuntimeError)

    an exception is raised if there is no known extraction method for @extension



120
121
122
# File 'lib/vanagon/component/source/local.rb', line 120

def cleanup
  @cleanup
end

#extensionObject

rubocop:disable Lint/DuplicateMethods



70
71
72
# File 'lib/vanagon/component/source/local.rb', line 70

def extension
  @extension
end

#fileObject

rubocop:disable Lint/DuplicateMethods



66
67
68
# File 'lib/vanagon/component/source/local.rb', line 66

def file
  @file
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/vanagon/component/source/local.rb', line 7

def url
  @url
end

#workdirObject

Returns the value of attribute workdir.



7
8
9
# File 'lib/vanagon/component/source/local.rb', line 7

def workdir
  @workdir
end

Class Method Details

.archive_extensionsObject



37
38
39
# File 'lib/vanagon/component/source/local.rb', line 37

def archive_extensions
  ARCHIVE_EXTENSIONS.values.flatten
end

.mangle(path) ⇒ Object

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.



33
34
35
# File 'lib/vanagon/component/source/local.rb', line 33

def mangle(path)
  path.gsub(%r{^file://}, '')
end

.valid_file?(target_file) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/vanagon/component/source/local.rb', line 25

def valid_file?(target_file)
  File.exist?(mangle(target_file.to_s))
end

Instance Method Details

#archive?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/vanagon/component/source/local.rb', line 137

def archive?
  archive_extensions.include?(extension)
end

#archive_extensionsObject



133
134
135
# File 'lib/vanagon/component/source/local.rb', line 133

def archive_extensions
  self.class.archive_extensions
end

#copyObject Also known as: fetch

Moves file from source to workdir

Raises:

  • (RuntimeError, Vanagon::Error)

    an exception is raised if the URI scheme cannot be handled



59
60
61
62
63
# File 'lib/vanagon/component/source/local.rb', line 59

def copy
  warn "Copying file '#{url.basename}' to workdir"

  FileUtils.cp_r(url, file)
end

#decompressorObject



141
142
143
# File 'lib/vanagon/component/source/local.rb', line 141

def decompressor
  @decompressor ||= ARCHIVE_EXTENSIONS.select { |k, v| v.include? extension }.keys.first
end

#dirnameString

The dirname to reference when building from the source

Returns:

  • (String)

    the directory that should be traversed into to build this source

Raises:

  • (RuntimeError)

    if the @extension for the @file isn’t currently handled by the method



149
150
151
152
153
154
155
156
157
# File 'lib/vanagon/component/source/local.rb', line 149

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

#extnameString

Returns the extension for @file

Returns:

  • (String)

    the extension of @file



127
128
129
130
131
# File 'lib/vanagon/component/source/local.rb', line 127

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") ⇒ String?

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

Parameters:

  • tar (String) (defaults to: "tar")

    the tar command to use

Returns:

  • (String, nil)

    command to extract the source

Raises:

  • (RuntimeError)

    an exception is raised if there is no known extraction method for @extension



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vanagon/component/source/local.rb', line 79

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"
    %(#{tar} xf "#{file}")
  when "tbz2"
    %(bunzip2 -c "#{file}" | #{tar} xf -)
  when "tgz"
    %(gunzip -c "#{file}" | #{tar} xf -)
  when "txz"
    %(unxz -d "#{file}" | #{tar} xvf -)
  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

#mangle(path) ⇒ Object

Wrapper around the class method ‘.mangle’



160
161
162
# File 'lib/vanagon/component/source/local.rb', line 160

def mangle(path)
  self.class.mangle(path)
end

#verifyObject

Local files need no checksum so this is a noop



52
53
54
# File 'lib/vanagon/component/source/local.rb', line 52

def verify
  # nothing to do here, so just return
end