From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:470:142:3::10]:42349) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jHrq2-0003QI-0r for guix-patches@gnu.org; Fri, 27 Mar 2020 12:28:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jHrpz-00078h-TY for guix-patches@gnu.org; Fri, 27 Mar 2020 12:28:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55551) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jHrpz-00078a-QD for guix-patches@gnu.org; Fri, 27 Mar 2020 12:28:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jHrpz-0000XD-NS for guix-patches@gnu.org; Fri, 27 Mar 2020 12:28:03 -0400 Subject: [bug#39258] [PATCH v3 3/3] guix: Use package metadata cache for package search. Resent-Message-ID: From: Arun Isaac Date: Fri, 27 Mar 2020 21:56:54 +0530 Message-Id: <20200327162654.18785-4-arunisaac@systemreboot.net> In-Reply-To: <20200327162654.18785-1-arunisaac@systemreboot.net> References: <20200327162654.18785-1-arunisaac@systemreboot.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: 39258@debbugs.gnu.org Cc: Arun Isaac , mail@ambrevar.xyz, ludo@gnu.org, zimon.toutoune@gmail.com * guix/scripts/package.scm (process-query): Call search-packages and display-package-search-results instead of find-packages-by-description and display-search-results respectively. * guix/ui.scm (package-metadata->recutils): New function. (%package-metrics): Use package-metadata record field accessors. (package-relevance): Rename argument package to package-metadata. (display-package-search-results): New function. --- guix/scripts/package.scm | 5 +- guix/ui.scm | 132 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 125 insertions(+), 12 deletions(-) diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm index 110d4f2977..c11f92f5a2 100644 --- a/guix/scripts/package.scm +++ b/guix/scripts/package.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2016 Benz Schenk ;;; Copyright © 2016 Chris Marusich ;;; Copyright © 2019 Tobias Geerinckx-Rice +;;; Copyright © 2020 Arun Isaac ;;; ;;; This file is part of GNU Guix. ;;; @@ -770,9 +771,9 @@ processed, #f otherwise." (_ #f)) opts)) (regexps (map (cut make-regexp* <> regexp/icase) patterns)) - (matches (find-packages-by-description regexps))) + (matches (search-packages (current-profile) regexps))) (leave-on-EPIPE - (display-search-results matches (current-output-port))) + (display-package-search-results matches (current-output-port))) #t)) (('show requested-name) diff --git a/guix/ui.scm b/guix/ui.scm index 1e24fe5dca..934699f065 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -14,6 +14,7 @@ ;;; Copyright © 2019 Chris Marusich ;;; Copyright © 2019 Tobias Geerinckx-Rice ;;; Copyright © 2019 Simon Tournier +;;; Copyright © 2020 Arun Isaac ;;; ;;; This file is part of GNU Guix. ;;; @@ -112,6 +113,7 @@ package-synopsis-string string->recutils package->recutils + package-metadata->recutils package-specification->name+version+output supports-hyperlinks? @@ -122,6 +124,7 @@ relevance package-relevance display-search-results + display-package-search-results with-profile-lock string->generations @@ -1484,6 +1487,75 @@ HYPERLINKS? is true, emit hyperlink escape sequences when appropriate." extra-fields) (newline port)) +(define* (package-metadata->recutils p port #:optional (width (%text-width)) + #:key + (hyperlinks? (supports-hyperlinks? port)) + (extra-fields '())) + "Write to PORT a `recutils' record of object P, arranging +to fit within WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to +emit. When HYPERLINKS? is true, emit hyperlink escape sequences when +appropriate." + (define width* + ;; The available number of columns once we've taken into account space for + ;; the initial "+ " prefix. + (if (> width 2) (- width 2) width)) + + ;; Note: Don't i18n field names so that people can post-process it. + (format port "name: ~a~%" (package-metadata-name p)) + (format port "version: ~a~%" (package-metadata-version p)) + (format port "outputs: ~a~%" (string-join (package-metadata-outputs p))) + (format port "systems: ~a~%" + (string-join (package-metadata-supported-systems p))) + (format port "dependencies: ~a~%" + (string-join (package-metadata-dependencies p) " ")) + (format port "location: ~a~%" + (or (and=> (package-metadata-location p) + (if hyperlinks? location->hyperlink location->string)) + (G_ "unknown"))) + + ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in + ;; field identifiers. + (format port "homepage: ~a~%" (package-metadata-home-page p)) + + ;; TODO: Print license + ;; (format port "license: ~a~%" + ;; (match (package-metadata-license p) + ;; (((? license? licenses) ...) + ;; (string-join (map license-name licenses) + ;; ", ")) + ;; ((? license? license) + ;; (let ((text (license-name license)) + ;; (uri (license-uri license))) + ;; (if (and hyperlinks? uri (string-prefix? "http" uri)) + ;; (hyperlink uri text) + ;; text))) + ;; (x + ;; (G_ "unknown")))) + (format port "synopsis: ~a~%" + (string-map (match-lambda + (#\newline #\space) + (chr chr)) + (or (and=> (package-metadata-synopsis p) P_) + ""))) + (format port "~a~%" + (string->recutils + (string-trim-right + (parameterize ((%text-width width*)) + (texi->plain-text + (string-append "description: " + (or (and=> (package-metadata-description p) P_) + "")))) + #\newline))) + (for-each (match-lambda + ((field . value) + (let ((field (symbol->string field))) + (format port "~a: ~a~%" + field + (fill-paragraph (object->string value) width* + (string-length field)))))) + extra-fields) + (newline port)) + ;;; ;;; Searching. @@ -1528,34 +1600,74 @@ score, the more relevant OBJ is to REGEXPS." (define %package-metrics ;; Metrics used to compute the "relevance score" of a package against a set ;; of regexps. - `((,package-name . 4) + `((,package-metadata-name . 4) ;; Match against uncommon outputs. - (,(lambda (package) + (,(lambda (package-metadata) (filter (lambda (output) (not (member output ;; Some common outpus shared by many packages. '("out" "doc" "debug" "lib" "include" "bin")))) - (package-outputs package))) + (package-metadata-outputs package-metadata))) . 1) ;; Match regexps on the raw Texinfo since formatting it is quite expensive ;; and doesn't have much of an effect on search results. - (,(lambda (package) - (and=> (package-synopsis package) P_)) . 3) - (,(lambda (package) - (and=> (package-description package) P_)) . 2) + (,(lambda (package-metadata) + (and=> (package-metadata-synopsis package-metadata) P_)) . 3) + (,(lambda (package-metadata) + (and=> (package-metadata-description package-metadata) P_)) . 2) (,(lambda (type) - (match (and=> (package-location type) location-file) + (match (and=> (package-metadata-location type) location-file) ((? string? file) (basename file ".scm")) (#f ""))) . 1))) -(define (package-relevance package regexps) +(define (package-relevance package-metadata regexps) "Return a score denoting the relevance of PACKAGE for REGEXPS. A score of zero means that PACKAGE does not match any of REGEXPS." - (relevance package regexps %package-metrics)) + (relevance package-metadata regexps %package-metrics)) + +(define* (display-package-search-results matches port + #:key + (command "guix search")) + "Display MATCHES, a list of /score pairs. If PORT is a +terminal, print at most a full screen of results." + (define first-line + (port-line port)) + + (define max-rows + (and first-line (isatty? port) + (terminal-rows port))) + + (define (line-count str) + (string-count str #\newline)) + + (let loop ((matches matches)) + (match matches + (((package-metadata . score) rest ...) + (let* ((links? (supports-hyperlinks? port)) + (text (call-with-output-string + (lambda (port) + (package-metadata->recutils package-metadata port + #:hyperlinks? links? + #:extra-fields + `((relevance . ,score))))))) + (if (and (not (getenv "INSIDE_EMACS")) + max-rows + (> (port-line port) first-line) ;print at least one result + (> (+ 4 (line-count text) (port-line port)) + max-rows)) + (unless (null? rest) + (display-hint (format #f (G_ "Run @code{~a ... | less} \ +to view all the results.") + command))) + (begin + (display text port) + (loop rest))))) + (() + #t)))) (define* (display-search-results matches port #:key -- 2.25.1