Class: Packages::PackageExtractor

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/packages/src/lib/packages/package_extractor.rb

Overview

Extracts the RPM package contents to a directory.

Examples:

Extracting a package into a temporary directory

extractor = PackageExtractor("./my_package-0.1-0.noarch.rpm")
Dir.mktmpdir do |tmpdir|
  extractor.extract(tmpdir)
  # do something with the content in tmpdir...
end

Defined Under Namespace

Classes: ExtractionFailed

Constant Summary

EXTRACT_CMD =

Command to extract an RPM, the contents is extracted into the current working directory.

"rpm2cpio %<source>s | cpio --quiet --sparse -dimu --no-absolute-filenames".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_path) ⇒ PackageExtractor

Constructor



42
43
44
# File 'library/packages/src/lib/packages/package_extractor.rb', line 42

def initialize(package_path)
  @package_path = package_path
end

Instance Attribute Details

#package_pathString (readonly)

Path to the package to extract.



34
35
36
# File 'library/packages/src/lib/packages/package_extractor.rb', line 34

def package_path
  @package_path
end

Instance Method Details

#extract(dir) ⇒ Object

Extracts the RPM contents to the given directory.

It is responsibility of the caller to remove the extracted content when it is not needed anymore.

Raises:

  • ExtractionFailed



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'library/packages/src/lib/packages/package_extractor.rb', line 58

def extract(dir)
  Dir.chdir(dir) do
    cmd = format(EXTRACT_CMD, source: Shellwords.escape(package_path))
    log.info("Extracting package #{package_path} to #{dir}...")

    # we need a shell to process the pipe,
    # the "allowed_exitstatus" option forces Cheetah to return the exit code
    ret = Yast::Execute.locally("sh", "-c", cmd, allowed_exitstatus: 0..255)
    log.info("Extraction result: #{ret}")

    raise ExtractionFailed unless ret.zero?
  end
end