From patchwork Wed Oct 6 15:20:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hartmut Goebel X-Patchwork-Id: 33638 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 6F10D27BBE3; Wed, 6 Oct 2021 16:36:22 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id C91EA27BBE1 for ; Wed, 6 Oct 2021 16:36:21 +0100 (BST) Received: from localhost ([::1]:37304 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mY8xw-0004fh-Rg for patchwork@mira.cbaines.net; Wed, 06 Oct 2021 11:36:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47398) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mY8j8-0002fl-VA for guix-patches@gnu.org; Wed, 06 Oct 2021 11:21:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:33627) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mY8j8-0008A4-Nw for guix-patches@gnu.org; Wed, 06 Oct 2021 11:21:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mY8j8-00051y-J9 for guix-patches@gnu.org; Wed, 06 Oct 2021 11:21:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#42180] [PATCH v2 01/23] guix: Add extracting-download. References: In-Reply-To: Resent-From: Hartmut Goebel Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 06 Oct 2021 15:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 42180 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 42180@debbugs.gnu.org X-Debbugs-Original-To: 42180@debbugs.gnu.org, guix-patches@gnu.org Received: via spool by 42180-submit@debbugs.gnu.org id=B42180.163353363419138 (code B ref 42180); Wed, 06 Oct 2021 15:21:02 +0000 Received: (at 42180) by debbugs.gnu.org; 6 Oct 2021 15:20:34 +0000 Received: from localhost ([127.0.0.1]:45141 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mY8ic-0004yR-Ky for submit@debbugs.gnu.org; Wed, 06 Oct 2021 11:20:34 -0400 Received: from mail-out.m-online.net ([212.18.0.9]:36967) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mY8iZ-0004yB-QF for 42180@debbugs.gnu.org; Wed, 06 Oct 2021 11:20:28 -0400 Received: from frontend02.mail.m-online.net (unknown [192.168.8.183]) by mail-out.m-online.net (Postfix) with ESMTP id 4HPdSB3PcDz1qwyD; Wed, 6 Oct 2021 17:20:26 +0200 (CEST) Received: from localhost (dynscan2.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 4HPdSB1flJz1qqkD; Wed, 6 Oct 2021 17:20:26 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan2.mail.m-online.net [192.168.6.68]) (amavisd-new, port 10024) with ESMTP id e5QwXXbKqggV; Wed, 6 Oct 2021 17:20:23 +0200 (CEST) Received: from hermia.goebel-consult.de (ppp-188-174-52-121.dynamic.mnet-online.de [188.174.52.121]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Wed, 6 Oct 2021 17:20:23 +0200 (CEST) Received: from thisbe.goebel-consult.de (hermia.goebel-consult.de [192.168.110.7]) by hermia.goebel-consult.de (Postfix) with ESMTP id C52566008B; Wed, 6 Oct 2021 17:20:31 +0200 (CEST) From: Hartmut Goebel Date: Wed, 6 Oct 2021 17:20:19 +0200 Message-Id: <626e4718c45c95a7278460f132bd38e08835e9f4.1633533541.git.h.goebel@crazy-compilers.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/extracting-download.scm: New file * Makefile.am (MODULES): Add it. --- Makefile.am | 1 + guix/extracting-download.scm | 179 +++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 guix/extracting-download.scm diff --git a/Makefile.am b/Makefile.am index b66789fa0b..f2b6c8e8da 100644 --- a/Makefile.am +++ b/Makefile.am @@ -96,6 +96,7 @@ MODULES = \ guix/discovery.scm \ guix/android-repo-download.scm \ guix/bzr-download.scm \ + guix/extracting-download.scm \ guix/git-download.scm \ guix/hg-download.scm \ guix/swh.scm \ diff --git a/guix/extracting-download.scm b/guix/extracting-download.scm new file mode 100644 index 0000000000..4b7dcc7e83 --- /dev/null +++ b/guix/extracting-download.scm @@ -0,0 +1,179 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès +;;; Copyright © 2017 Mathieu Lirzin +;;; Copyright © 2017 Christopher Baines +;;; Copyright © 2020 Jakub Kądziołka +;;; Copyright © 2020 Hartmut Goebel +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix extracting-download) + #:use-module (ice-9 match) + #:use-module (ice-9 popen) + #:use-module ((guix build download) #:prefix build:) + #:use-module ((guix build utils) #:hide (delete)) + #:use-module (guix gexp) + #:use-module (guix modules) + #:use-module (guix monads) + #:use-module (guix packages) ;; for %current-system + #:use-module (guix store) + #:use-module (guix utils) + #:use-module (srfi srfi-26) + #:export (http-fetch/extract + download-to-store/extract)) + +;;; +;;; Produce fixed-output derivations with data extracted from n archive +;;; fetched over HTTP or FTP. +;;; +;;; This is meant to be used for package repositories where the actual source +;;; archive is packed into another archive, eventually carrying meta-data. +;;; Using this derivation saves both storing the outer archive and extracting +;;; the actual one at build time. The hash is calculated on the actual +;;; archive to ease validating the stored file. +;;; + +(define* (http-fetch/extract url filename-to-extract hash-algo hash + #:optional name + #:key (system (%current-system)) (guile (default-guile))) + "Return a fixed-output derivation that fetches an archive at URL, and +extracts FILE_TO_EXTRACT from the archive. The FILE_TO_EXTRACT is expected to +have hash HASH of type HASH-ALGO (a symbol). By default, the file name is the +base name of URL; optionally, NAME can specify a different file name." + (define file-name + (match url + ((head _ ...) + (basename head)) + (_ + (basename url)))) + + (define guile-zlib + (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib)) + + (define guile-json + (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4)) + + (define gnutls + (module-ref (resolve-interface '(gnu packages tls)) 'gnutls)) + + (define inputs + `(("tar" ,(module-ref (resolve-interface '(gnu packages base)) + 'tar)))) + + (define config.scm + (scheme-file "config.scm" + #~(begin + (define-module (guix config) + #:export (%system)) + + (define %system + #$(%current-system))))) + + (define modules + (cons `((guix config) => ,config.scm) + (delete '(guix config) + (source-module-closure '((guix build download) + (guix build utils) + (guix utils) + (web uri)))))) + + (define build + (with-imported-modules modules + (with-extensions (list guile-json gnutls ;for (guix swh) + guile-zlib) + #~(begin + (use-modules (guix build download) + (guix build utils) + (guix utils) + (web uri) + (ice-9 match) + (ice-9 popen)) + ;; The code below expects tar to be in $PATH. + (set-path-environment-variable "PATH" '("bin") + (match '#+inputs + (((names dirs outputs ...) ...) + dirs))) + + (setvbuf (current-output-port) 'line) + (setvbuf (current-error-port) 'line) + + (call-with-temporary-directory + (lambda (directory) + ;; TODO: Support different archive types, based on content-type + ;; or archive name extention. + (let* ((file-to-extract (getenv "extract filename")) + (port (http-fetch (string->uri (getenv "download url")) + #:verify-certificate? #f)) + (tar (open-pipe* OPEN_WRITE "tar" "-C" directory + "-xf" "-" file-to-extract))) + (dump-port port tar) + (close-port port) + (let ((status (close-pipe tar))) + (unless (zero? status) + (error "tar extraction failure" status))) + (copy-file (string-append directory "/" + (getenv "extract filename")) + #$output)))))))) + + (mlet %store-monad ((guile (package->derivation guile system))) + (gexp->derivation (or name file-name) build + + ;; Use environment variables and a fixed script name so + ;; there's only one script in store for all the + ;; downloads. + #:script-name "extract-download" + #:env-vars + `(("download url" . ,url) + ("extract filename" . ,filename-to-extract)) + #:leaked-env-vars '("http_proxy" "https_proxy" + "LC_ALL" "LC_MESSAGES" "LANG" + "COLUMNS") + #:system system + #:local-build? #t ; don't offload download + #:hash-algo hash-algo + #:hash hash + #:guile-for-build guile))) + + +(define* (download-to-store/extract store url filename-to-extract + #:optional (name (basename url)) + #:key (log (current-error-port)) + (verify-certificate? #t)) + "Download an archive from URL, and extracts FILE_TO_EXTRACT from the archive +to STORE, either under NAME or URL's basename if omitted. Write progress +reports to LOG. VERIFY-CERTIFICATE? determines whether or not to validate +HTTPS server certificates." + (call-with-temporary-output-file + (lambda (temp port) + (let ((result + (parameterize ((current-output-port log)) + (build:url-fetch url temp + ;;#:mirrors %mirrors + #:verify-certificate? + verify-certificate?)))) + (close port) + (and result + (call-with-temporary-output-file + (lambda (contents port) + (let ((tar (open-pipe* OPEN_READ + "tar" ;"--auto-compress" + "-xf" temp "--to-stdout" filename-to-extract))) + (dump-port tar port) + (close-port port) + (let ((status (close-pipe tar))) + (unless (zero? status) + (error "tar extraction failure" status))) + (add-to-store store name #f "sha256" contents)))))))))