summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2020-12-09 18:28:42 +0100
committerEugen Wissner <belka@caraus.de>2020-12-09 18:28:42 +0100
commit2910a89d6c59b997a3896f05c6ad7fb65c26f813 (patch)
tree3d3edebdf32b0c972e8449b82be2c10742395d6c /lib
parentbe64bc6dbb9d0f717f7cbdd83d9371de979d4c63 (diff)
downloadslackbuilder-2910a89d6c59b997a3896f05c6ad7fb65c26f813.tar.gz
Add autoupdater
Diffstat (limited to 'lib')
-rw-r--r--lib/download.rb145
-rw-r--r--lib/package.rb38
2 files changed, 183 insertions, 0 deletions
diff --git a/lib/download.rb b/lib/download.rb
new file mode 100644
index 0000000..49bb0c0
--- /dev/null
+++ b/lib/download.rb
@@ -0,0 +1,145 @@
+# 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 '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)
+ "https://download.dlackware.com/hosted-sources#{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} "\
+ "caraus.de:/srv/httpd/dlackware/download/hosted-sources#{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('/')..-1]
+
+ 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('/')..-1]
+
+ 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 -m "#{message}")
+ # sh "git -C slackbuilds push origin #{branch}"
+end
diff --git a/lib/package.rb b/lib/package.rb
new file mode 100644
index 0000000..7bc81ec
--- /dev/null
+++ b/lib/package.rb
@@ -0,0 +1,38 @@
+# 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
+
+class Package
+ attr_reader :path
+ attr_reader :version
+ attr_reader :homepage
+ attr_reader :requires
+
+ def initialize(path, version:, homepage:, requires: [])
+ @path = path
+ @version = version
+ @homepage = homepage
+ @requires = requires
+ end
+
+ def name
+ File.basename @path
+ end
+end
+
+def info_template(package, download, md5sum)
+ <<~INFO_FILE
+ PRGNAM="#{package.name}"
+ VERSION="#{package.version}"
+ HOMEPAGE="#{package.homepage}"
+ DOWNLOAD="#{download * " \\\n "}"
+ MD5SUM="#{md5sum * " \\\n "}"
+ DOWNLOAD_x86_64=""
+ MD5SUM_x86_64=""
+ REQUIRES="#{package.requires * ' '}"
+ MAINTAINER="Eugene Wissner"
+ EMAIL="belka@caraus.de"
+ INFO_FILE
+end