Subject: [PATCH 1/3] packages: Add package->code.

From: Ricardo Wurmus <[email-masked]>
Date: Sun, 27 Aug 2017 18:00:44 +0200

* 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 <[email-masked]>
 ;;; Copyright © 2016 Alex Kost <[email-masked]>
 ;;; Copyright © 2017 Efraim Flashner <[email-masked]>
+;;; Copyright © 2017 Ricardo Wurmus <[email-masked]>
 ;;;
 ;;; 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