Refactoring the search for compress files (bz2, gz, ...)
This commit is contained in:
parent
15d56a5301
commit
f1059c520c
1 changed files with 25 additions and 39 deletions
|
|
@ -42,26 +42,38 @@ module DebImporter
|
||||||
|
|
||||||
# TODO: Avoid code duplication (bz2, gz, ...)
|
# TODO: Avoid code duplication (bz2, gz, ...)
|
||||||
def i18n(component, language)
|
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', '']
|
for suffix in ['.bz2', '.gz', '']
|
||||||
url = "#{@dist_url}/#{component}/i18n/Translation-en#{suffix}"
|
|
||||||
Rails.logger.debug "Looking for translations file: #{url}"
|
|
||||||
begin
|
begin
|
||||||
Rails.logger.debug "Try opening URL: #{url}"
|
|
||||||
begin
|
begin
|
||||||
|
url = "#{base_url}#{suffix}"
|
||||||
|
Rails.logger.debug "Checking if file at #{url} is available"
|
||||||
file = open(url)
|
file = open(url)
|
||||||
rescue OpenURI::HTTPError => e
|
rescue OpenURI::HTTPError => e
|
||||||
Rails.logger.info "URL #{url} lead to #{e}. skipping."
|
Rails.logger.debug "Loading #{url} lead to error #{e}. skipping."
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
if suffix == '.gz'
|
|
||||||
file = Zlib::GzipReader.new(file)
|
# Decompress file depending on its filename suffix
|
||||||
elsif suffix == '.bz2'
|
case suffix
|
||||||
|
when '.bz2'
|
||||||
file = Bzip2::Reader.new(file)
|
file = Bzip2::Reader.new(file)
|
||||||
|
when '.gz'
|
||||||
|
file = Zlib::GzipReader.new(file)
|
||||||
end
|
end
|
||||||
Rails.logger.debug "File containing translations is: #{file}"
|
Rails.logger.debug "File containing translations is: #{file}"
|
||||||
return get_paragraphs(file)
|
|
||||||
|
return file
|
||||||
rescue Errno::ENOENT
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -70,36 +82,10 @@ module DebImporter
|
||||||
# and architecture (e.g. "amd64")
|
# and architecture (e.g. "amd64")
|
||||||
def packages(component, architecture)
|
def packages(component, architecture)
|
||||||
# create path like "main/binary-amd64/Packages"
|
# create path like "main/binary-amd64/Packages"
|
||||||
packages_path = "/#{component}/binary-#{architecture}/Packages"
|
packages_path = "#{@dist_url}/#{component}/binary-#{architecture}/Packages"
|
||||||
# Check if gzip or uncompressed files exist
|
Rails.logger.debug "Loading packages from #{packages_path}"
|
||||||
# (there is no working bzip2 library for Ruby 2.x at the time - 11/2014)
|
file = find_and_open_compressed_url(packages_path)
|
||||||
for suffix in ['.bz2', '.gz', '']
|
return get_paragraphs(file)
|
||||||
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
|
|
||||||
end # def packages
|
end # def packages
|
||||||
|
|
||||||
end # class Release
|
end # class Release
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue