From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58284) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dnoIh-0007xA-Er for guix-patches@gnu.org; Fri, 01 Sep 2017 11:56:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dnoIc-0000jz-HR for guix-patches@gnu.org; Fri, 01 Sep 2017 11:56:07 -0400 Received: from debbugs.gnu.org ([208.118.235.43]:59961) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dnoIc-0000jV-D4 for guix-patches@gnu.org; Fri, 01 Sep 2017 11:56:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dnoIc-0008S4-3K for guix-patches@gnu.org; Fri, 01 Sep 2017 11:56:02 -0400 Subject: [bug#28251] [PATCH 1/3] packages: Add package->code. Resent-Message-ID: From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) References: <20170827155820.28812-1-rekado@elephly.net> <20170827160046.29049-1-rekado@elephly.net> Date: Fri, 01 Sep 2017 17:55:27 +0200 In-Reply-To: <20170827160046.29049-1-rekado@elephly.net> (Ricardo Wurmus's message of "Sun, 27 Aug 2017 18:00:44 +0200") Message-ID: <87ziae4dn4.fsf@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: Ricardo Wurmus Cc: 28251@debbugs.gnu.org Ricardo Wurmus skribis: > * guix/packages.scm (package->code): New procedure. We=E2=80=99ll need tests for this. :-) I would move it to (guix import utils) or a new (guix import print) module or similar (which will also allow us to use =E2=80=98factorize-uri= =E2=80=99). In my mind, eventually all importers will produce a object directly, and so this will be a core part of the import machinery. Since it=E2=80=99s not a crucial component, I would prefer to have it out of (guix packages) though. > +;; FIXME: the quasiquoted arguments field may contain embedded package > +;; objects, e.g. in #:disallowed-references; they will just be printed w= ith > +;; their usual # representation, not as variable names. Not sure how to solve that; maybe we can ignore for now. > +(define (package->code package) > + "Return an S-expression representing the source code that produces PAC= KAGE > +when evaluated." Like you wrote, it would be nice to also return a spec of modules in scope, like: ((gnu packages r) ((guix licenses) #:prefix license:)) WDYT? That way, we can eventually change =E2=80=98guix import=E2=80=99 to systema= tically print both the =E2=80=98define-module=E2=80=99 clause and the package definition. > + ;; 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)))) > + I think we should compare values with =E2=80=98eq?=E2=80=99 (usually we=E2= =80=99re concerned with pointer identity of records), and also use =E2=80=98module-for-each=E2= =80=99 + =E2=80=98let/ec=E2=80=99 to avoid building a list for nothing, like: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> (match (let/ec return (module-for-each (lambda (sym var) (if (eq? + (variable-ref var)) (return sym) #f)) the-scm-module)) ((? symbol? sym) sym) (_ #f)) $17 =3D + --8<---------------cut here---------------end--------------->8--- > + ;; Print either license variable name or the code for a license object > + (define (print-license lic) Nitpick: I=E2=80=99d rename all the =E2=80=98print-*=E2=80=99 procedures to= =E2=80=98*->code=E2=80=99. > + (match package > + (($ name version source build-system If we move this to (guix import =E2=80=A6), we can no longer match on the record, but that=E2=80=99s not necessarily a bad thing anyway. :-) > + ,@(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))))) =E2=80=98match=E2=80=99! :-) This looks pretty cool already! Thanks, Ludo=E2=80=99.