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



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

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



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

def cleanup
  @cleanup
end

#extensionObject

rubocop:disable Lint/DuplicateMethods



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

def extension
  @extension
end

#fileObject

rubocop:disable Lint/DuplicateMethods



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

def file
  @file
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#workdirObject

Returns the value of attribute workdir.



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

def workdir
  @workdir
end

Class Method Details

.archive_extensionsObject



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

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.



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

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

.valid_file?(target_file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#archive?Boolean

Returns:

  • (Boolean)


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

def archive?
  archive_extensions.include?(extension)
end

#archive_extensionsObject



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

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



60
61
62
63
64
# 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

#decompressorObject



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

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



148
149
150
151
152
153
154
155
156
# 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

#extnameString

Returns the extension for @file

Returns:

  • (String)

    the extension of @file



126
127
128
129
130
# 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") ⇒ 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



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
# 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

#mangle(path) ⇒ Object

Wrapper around the class method ‘.mangle’



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

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

#verifyObject

Local files need no checksum so this is a noop



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

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