# Class: DebImporter::Release

Inherits:
  Object
  - Object
  - DebImporter::Release
  show all

Defined in:
  lib/deb_importer.rb

## Overview

This module imports information about packages of a Linux distribution that uses the DEB package format like Debian, Ubuntu or Mint.

It starts by loading the Release file of a release to get information about available components and architectures.

Next it loads the Packages lists (prefers bz2, falls back to .gz or even the uncompressed version).

## Instance Attribute Summary collapse

- #architectures ⇒ Object readonly

  Returns the value of attribute architectures.

- #codename ⇒ Object readonly

  Returns the value of attribute codename.

- #components ⇒ Object readonly

  Returns the value of attribute components.

- #description ⇒ Object readonly

  Returns the value of attribute description.

- #files ⇒ Object readonly

  Returns the value of attribute files.

- #origin ⇒ Object readonly

  Returns the value of attribute origin.

- #version ⇒ Object readonly

  Returns the value of attribute version.

## Instance Method Summary collapse

- #find_and_open_compressed_url(base_url) ⇒ Object

  Look for the file or URL in various compressed formats (e.g. bz2, gz) and fall back to plain text format.

- #i18n(component, language) ⇒ Object

  Get package information from translation (i18n) files.

- #initialize(dist_url) ⇒ Release constructor

  Load and parse a Release file of an APT repository.

- #packages(component, architecture) ⇒ Object

  Try to load the Packages file for a certain component (e.g. “main”) and architecture (e.g. “amd64”).

## Constructor Details

### #initialize(dist_url) ⇒ `Release`

Load and parse a Release file of an APT repository

    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    # File 'lib/deb_importer.rb', line 20

    def initialize(dist_url)
      @dist_url = dist_url
      release_url = dist_url + "/Release"
      Rails.logger.debug "Loading Release file from #{release_url}"
      open(release_url) do |release_data|
        fields = get_fields(release_data)
        @architectures = fields[:Architectures]
        @components = fields[:Components]
        @description = fields[:Description]
        @codename = fields[:Codename]
        @origin = fields[:Origin]
        @version = fields[:Version]

        # TODO: check sizes and checksums
      end # open
    end

## Instance Attribute Details

### #architectures ⇒ `Object` (readonly)

Returns the value of attribute architectures.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def architectures
      @architectures
    end

### #codename ⇒ `Object` (readonly)

Returns the value of attribute codename.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def codename
      @codename
    end

### #components ⇒ `Object` (readonly)

Returns the value of attribute components.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def components
      @components
    end

### #description ⇒ `Object` (readonly)

Returns the value of attribute description.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def description
      @description
    end

### #files ⇒ `Object` (readonly)

Returns the value of attribute files.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def files
      @files
    end

### #origin ⇒ `Object` (readonly)

Returns the value of attribute origin.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def origin
      @origin
    end

### #version ⇒ `Object` (readonly)

Returns the value of attribute version.

    17
    18
    19
    # File 'lib/deb_importer.rb', line 17

    def version
      @version
    end

## Instance Method Details

### #find_and_open_compressed_url(base_url) ⇒ `Object`

Look for the file or URL in various compressed formats (e.g. bz2, gz) and fall back to plain text format.

    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    # File 'lib/deb_importer.rb', line 51

    def find_and_open_compressed_url(base_url)
      Rails.logger.debug "Looking for files at URL #{base_url} with different compressions"
      for suffix in ['.bz2', '.gz', '']
        begin
          begin
            url = "#{base_url}#{suffix}"
            Rails.logger.debug "Checking if file at #{url} is available"
            file = open(url)
          rescue OpenURI::HTTPError => e
            Rails.logger.debug "Loading #{url} lead to error #{e}. skipping."
            next
          end

          # Decompress file depending on its filename suffix
          case suffix
          when '.bz2'
            file = Bzip2::Reader.new(file)
          when '.gz'
            file = Zlib::GzipReader.new(file)
          end
          Rails.logger.debug "File containing translations is: #{file}"

          return file
        rescue Errno::ENOENT
          Rails.logger.debug "URL #{url} could not be opened. Skipping."
        end
      end

      Rails.logger.error "No file found at #{url} and various compression extensions."
      return nil
    end

### #i18n(component, language) ⇒ `Object`

Get package information from translation (i18n) files. Returns an enumerator of packages.

    39
    40
    41
    42
    43
    44
    45
    46
    47
    # File 'lib/deb_importer.rb', line 39

    def i18n(component, language)
      url = "#{@dist_url}/#{component}/i18n/Translation-en"
      file = find_and_open_compressed_url(url)
      if file
        return get_paragraphs(file)
      else
        return []
      end
    end

### #packages(component, architecture) ⇒ `Object`

Try to load the Packages file for a certain component (e.g. “main”) and architecture (e.g. “amd64”)

    85
    86
    87
    88
    89
    90
    91
    # File 'lib/deb_importer.rb', line 85

    def packages(component, architecture)
      # create path like "main/binary-amd64/Packages"
      packages_path = "#{@dist_url}/#{component}/binary-#{architecture}/Packages"
      Rails.logger.debug "Loading packages from #{packages_path}"
      file = find_and_open_compressed_url(packages_path)
      return get_paragraphs(file)
    end