all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: 28251@debbugs.gnu.org
Cc: Ricardo Wurmus <rekado@elephly.net>
Subject: [bug#28251] [PATCH 1/3] packages: Add package->code.
Date: Sun, 27 Aug 2017 18:00:44 +0200	[thread overview]
Message-ID: <20170827160046.29049-1-rekado@elephly.net> (raw)
In-Reply-To: <20170827155820.28812-1-rekado@elephly.net>

* guix/packages.scm (package->code): New procedure.
---
 guix/packages.scm | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 131 insertions(+)

diff --git a/guix/packages.scm b/guix/packages.scm
index f619d9b37..d25920010 100644
--- a/guix/packages.scm
+++ b/guix/packages.scm
@@ -4,6 +4,7 @@
 ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
 ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
 ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
+;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -31,6 +32,7 @@
   #:use-module (guix derivations)
   #:use-module (guix memoization)
   #:use-module (guix build-system)
+  #:use-module (guix licenses)
   #:use-module (guix search-paths)
   #:use-module (guix sets)
   #:use-module (ice-9 match)
@@ -84,6 +86,7 @@
             package-maintainers
             package-properties
             package-location
+            package->code
             hidden-package
             hidden-package?
             package-superseded
@@ -306,6 +309,134 @@ name of its URI."
                                                        package)
                                                       16)))))
 
+;; FIXME: the quasiquoted arguments field may contain embedded package
+;; objects, e.g. in #:disallowed-references; they will just be printed with
+;; their usual #<package ...> representation, not as variable names.
+(define (package->code package)
+  "Return an S-expression representing the source code that produces PACKAGE
+when evaluated."
+  ;; The module in which the package PKG is defined
+  (define (package-module-name pkg)
+    (map string->symbol
+         (string-split (string-drop-right
+                        (location-file (package-location pkg)) 4)
+                       #\/)))
+
+  ;; Return the first candidate variable name that is bound to VAL.
+  ;; TODO: avoid '%pkg-config
+  (define (variable-name val mod)
+    (let ((candidates (filter identity
+                              (module-map
+                               (lambda (sym var)
+                                 (if (equal? val (variable-ref var)) sym #f))
+                               (resolve-interface mod)))))
+      (if (null? candidates) #f (car candidates))))
+
+  ;; Print either license variable name or the code for a license object
+  (define (print-license lic)
+    (let ((var (variable-name lic '(guix licenses))))
+      (or var
+          `(license
+            (name ,(license-name lic))
+            (uri ,(license-uri lic))
+            (comment ,(license-comment lic))))))
+
+  (define (print-search-path-specification spec)
+    `(search-path-specification
+      (variable ,(search-path-specification-variable spec))
+      (files (list ,@(search-path-specification-files spec)))
+      (separator ,(search-path-specification-separator spec))
+      (file-type (quote ,(search-path-specification-file-type spec)))
+      (file-pattern ,(search-path-specification-file-pattern spec))))
+
+  (define (print-source source version)
+    ;; FIXME: we cannot use factorize-uri because (guix import utils)
+    ;; cannot be imported in this module.
+    (let ((factorize-uri (lambda (uri version)
+                           (list uri))))
+      (match source
+        (($ <origin> uri method sha256 file-name patches)
+         `(origin
+            (uri (string-append ,@(factorize-uri uri version)))
+            (method ,(procedure-name method))
+            (sha256
+             (base32
+              ,(format #f "~a" (bytevector->nix-base32-string sha256))))
+            ;; FIXME: in order to be able to throw away the directory prefix,
+            ;; we just assume that the patch files can be found with
+            ;; "search-patches".
+            ,@(let ((ps (force patches)))
+                (if (null? ps) '()
+                    `((patches (search-patches ,@(map basename ps)))))))))))
+
+  (define (print-package-lists lsts)
+    (list 'quasiquote
+          (map (match-lambda
+                 ((label pkg)
+                  (let ((mod (package-module-name pkg)))
+                    (list label
+                          ;; FIXME: using '@ certainly isn't pretty, but it
+                          ;; avoids having to import the individual package
+                          ;; modules.
+                          (list 'unquote
+                                (list '@ mod (variable-name pkg mod)))))))
+               lsts)))
+
+  (match package
+    (($ <package> name version source build-system
+                  arguments inputs propagated-inputs native-inputs
+                  self-native-input?
+                  outputs
+                  native-search-paths
+                  search-paths
+                  replacement
+                  synopsis description license
+                  home-page supported-systems maintainers
+                  properties location)
+     `(package
+        (name ,name)
+        (version ,version)
+        (source ,(print-source source version))
+        ,@(if (null? properties)  '()
+              `((properties ,properties)))
+        ,@(let ((rep (replacement)))
+            (if rep
+                `((replacement ,rep))
+                '()))
+        (build-system ,(symbol-append (build-system-name build-system)
+                                      '-build-system))
+        ,@(let ((args (arguments)))
+            (if (null? args) '()
+                `((arguments ,(list 'quasiquote (arguments))))))
+        ,@(if (equal? outputs '("out")) '()
+              `((outputs (list ,@outputs))))
+        ,@(let ((pkgs (native-inputs)))
+            (if (null? pkgs) '()
+                `((native-inputs ,(print-package-lists pkgs)))))
+        ,@(let ((pkgs (inputs)))
+            (if (null? pkgs) '()
+                `((inputs ,(print-package-lists pkgs)))))
+        ,@(let ((pkgs (propagated-inputs)))
+            (if (null? pkgs) '()
+                `((propagated-inputs ,(print-package-lists pkgs)))))
+        ,@(if (lset= string=? supported-systems %supported-systems)
+              '()
+              `((supported-systems (list ,@supported-systems))))
+        ,@(let ((paths (map print-search-path-specification native-search-paths)))
+            (if (null? paths) '()
+                `((native-search-paths
+                   (list ,@paths)))))
+        ,@(let ((paths (map print-search-path-specification search-paths)))
+            (if (null? paths) '()
+                `((search-paths
+                   (list ,@paths)))))
+        (home-page ,home-page)
+        (synopsis ,synopsis)
+        (description ,description)
+        (license ,(if (list? license)
+                      `(list ,@(map print-license license))
+                      (print-license license)))))))
+
 (define (package-upstream-name package)
   "Return the upstream name of PACKAGE, which could be different from the name
 it has in Guix."
-- 
2.14.1

  reply	other threads:[~2017-08-27 16:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-27 15:58 [bug#28251] [PATCH 0/3] Add generic JSON importer Ricardo Wurmus
2017-08-27 16:00 ` Ricardo Wurmus [this message]
2017-08-27 16:00   ` [bug#28251] [PATCH 2/3] import: Add generic data to package converter Ricardo Wurmus
2017-09-04 13:04     ` Ludovic Courtès
2017-08-27 16:00   ` [bug#28251] [PATCH 3/3] import: Add JSON importer Ricardo Wurmus
2017-09-04 13:04     ` Ludovic Courtès
2017-09-01 15:55   ` [bug#28251] [PATCH 1/3] packages: Add package->code Ludovic Courtès
2017-09-28 11:19     ` bug#28251: " Ricardo Wurmus
2017-08-28 12:27 ` [bug#28251] [PATCH 0/3] Add generic JSON importer Ricardo Wurmus
2017-09-01 15:35 ` Ludovic Courtès
2017-09-28 11:23   ` Ricardo Wurmus
2017-09-28 19:58     ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170827160046.29049-1-rekado@elephly.net \
    --to=rekado@elephly.net \
    --cc=28251@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.