Refactoring the search for compress files (bz2, gz, ...)

This commit is contained in:
Christoph Haas 2015-04-24 11:18:21 +02:00
parent 15d56a5301
commit f1059c520c

View file

@ -42,26 +42,38 @@ module DebImporter
# TODO: Avoid code duplication (bz2, gz, ...)
def i18n(component, language)
url = "#{@dist_url}/#{component}/i18n/Translation-en"
file = find_and_open_compressed_url(url)
return get_paragraphs(file)
end
# Look for the file or URL in various compressed formats
# (e.g. bz2, gz) and fall back to plain text format.
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', '']
url = "#{@dist_url}/#{component}/i18n/Translation-en#{suffix}"
Rails.logger.debug "Looking for translations file: #{url}"
begin
Rails.logger.debug "Try opening URL: #{url}"
begin
url = "#{base_url}#{suffix}"
Rails.logger.debug "Checking if file at #{url} is available"
file = open(url)
rescue OpenURI::HTTPError => e
Rails.logger.info "URL #{url} lead to #{e}. skipping."
Rails.logger.debug "Loading #{url} lead to error #{e}. skipping."
next
end
if suffix == '.gz'
file = Zlib::GzipReader.new(file)
elsif suffix == '.bz2'
# 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 get_paragraphs(file)
return file
rescue Errno::ENOENT
Rails.logger.debug "URL could not be opened. Skipping."
Rails.logger.error "URL #{url} could not be opened. Skipping."
end
end
end
@ -70,36 +82,10 @@ module DebImporter
# and architecture (e.g. "amd64")
def packages(component, architecture)
# create path like "main/binary-amd64/Packages"
packages_path = "/#{component}/binary-#{architecture}/Packages"
# Check if gzip or uncompressed files exist
# (there is no working bzip2 library for Ruby 2.x at the time - 11/2014)
for suffix in ['.bz2', '.gz', '']
packages_path_with_suffix = packages_path+suffix
Rails.logger.debug "Looking for Packages file: #{packages_path_with_suffix}"
url = @dist_url + packages_path_with_suffix
begin
Rails.logger.debug "Try opening URL: #{url}"
begin
file = open(url)
rescue OpenURI::HTTPError => e
Rails.logger.info "URL #{url} lead to #{e}. skipping."
next
end
if suffix == '.gz'
file = Zlib::GzipReader.new(file)
elsif suffix == '.bz2'
file = Bzip2::Reader.new(file)
end
Rails.logger.debug "File containing packages is: #{file}"
#paragraphs = get_paragraphs(file)
#return paragraphs
#byebug
return get_paragraphs(file)
rescue Errno::ENOENT
Rails.logger.debug "URL could not be opened. Skipping."
end
end
return nil
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 # def packages
end # class Release