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

Constant Summary collapse

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

Parameters:

  • package_path (String)

    the path to the package to extract



39
40
41
# File 'library/packages/src/lib/packages/package_extractor.rb', line 39

def initialize(package_path)
  @package_path = package_path
end

Instance Attribute Details

#package_pathString (readonly)

Path to the package to extract.

Returns:

  • (String)

    package path



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.

Parameters:

  • dir (String)

    Directory where the RPM contents will be extracted to

Raises:

  • ExtractionFailed



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'library/packages/src/lib/packages/package_extractor.rb', line 55

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 Y2Packager::PackageExtractionError unless ret.zero?
  end
end