slackbuilder/lib/download.rb

146 lines
3.7 KiB
Ruby

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# frozen_string_literal: true
require_relative '../config/config'
require_relative 'package'
require 'net/http'
MY_BRANCH = 'user/belka/updates'
def write_download(target, response)
checksum = Digest::MD5.new
File.open target, 'w' do |io|
response.read_body do |chunk|
print '.'
io << chunk
checksum << chunk
end
end
checksum
end
def redirect_download(location, target)
puts 'redirecting...'
new_location = URI location
download new_location, target
end
def start_download(uri, target, http)
request = Net::HTTP::Get.new uri
http.request request do |response|
case response
when Net::HTTPRedirection
return redirect_download response['location'], target
else
return write_download target, response
end
end
end
def download(uri, target)
print "Downloading #{uri} "
checksum = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
checksum = start_download uri, target, http
end
puts
checksum
end
def hosted_sources(absolute_url)
CONFIG[:download_url] + absolute_url
end
def remote_file_exists?(url)
uri = URI hosted_sources(url)
request = Net::HTTP.new uri.host, uri.port
request.use_ssl = true
response = request.request_head uri.path
response.code.to_i == 200
end
def upload_command(local_path, remote_path)
['scp', "slackbuilds/#{local_path}", CONFIG[:remote_path] + remote_path]
end
def clone_and_archive(repo, name_version, tarball)
_, _, version = name_version.rpartition '-'
rm_rf name_version
sh "git clone #{repo} #{name_version}"
sh "git -C #{name_version} checkout v#{version}"
sh "git -C #{name_version} submodule update --init --recursive"
sh "tar Jcvf slackbuilds/#{tarball} #{name_version}"
rm_rf name_version
end
def clone(repo, tarball)
name_version = File.basename tarball, '.tar.xz'
remote_path = tarball[tarball.index('/')..]
if remote_file_exists?(remote_path)
uri = URI hosted_sources(remote_path)
return download(uri, "slackbuilds/#{tarball}").hexdigest
end
clone_and_archive repo, name_version, tarball
sh(*upload_command(tarball, remote_path))
Digest::MD5.hexdigest File.read("slackbuilds/#{tarball}")
end
def download_and_deploy(uri, tarball)
remote_path = tarball[tarball.index('/')..]
if remote_file_exists?(remote_path)
uri = URI hosted_sources(remote_path)
return download(uri, "slackbuilds/#{tarball}").hexdigest
end
checksum = download uri, "slackbuilds/#{tarball}"
sh(*upload_command(tarball, remote_path))
checksum.hexdigest
end
def write_info(package, download:, md5sum:)
File.open "slackbuilds/#{package.path}/#{package.name}.info", 'w' do |file|
file.write info_template(package, download, md5sum)
end
end
def update_slackbuild_version(package_path, version)
name = package_path.split('/').last
slackbuild_filename = "slackbuilds/#{package_path}/#{name}.SlackBuild"
slackbuild_contents = File.read(slackbuild_filename)
.gsub(/^VERSION=\${VERSION:-.+/, "VERSION=${VERSION:-#{version}}")
File.open(slackbuild_filename, 'w') { |file| file.puts slackbuild_contents }
end
def commit(package_path, version)
message = "#{package_path}: Updated for version #{version}"
unless system('git', '-C', 'slackbuilds', 'checkout', MY_BRANCH,
err: '/dev/null')
sh "git -C slackbuilds checkout -b #{MY_BRANCH} master"
end
sh "git -C slackbuilds add #{package_path}"
sh %(git -C slackbuilds commit -S -m "#{message}")
# sh "git -C slackbuilds push origin #{branch}"
end