From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'order' option to alphabetically order file. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Mon, 06 May 2024 10:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 70800@debbugs.gnu.org Cc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by submit@debbugs.gnu.org id=B.171499269330945 (code B ref -1); Mon, 06 May 2024 10:52:02 +0000 Received: (at submit) by debbugs.gnu.org; 6 May 2024 10:51:33 +0000 Received: from localhost ([127.0.0.1]:37201 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3vwS-000833-W5 for submit@debbugs.gnu.org; Mon, 06 May 2024 06:51:33 -0400 Received: from lists.gnu.org ([2001:470:142::17]:56194) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3vwO-00082u-9B for submit@debbugs.gnu.org; Mon, 06 May 2024 06:51:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s3vvu-00062k-PJ for guix-patches@gnu.org; Mon, 06 May 2024 06:50:58 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117] helo=email.rimm.ee) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1s3vvl-0004tT-De for guix-patches@gnu.org; Mon, 06 May 2024 06:50:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1714992642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=WVdFGj1eni+Ricg5xwFr4yv4nH/HcOyKDOWKEnt+Hf0=; b=haHERx8BijRNpzaAdaFi0bo5eazRdQp+8g0bFMyp/00nCbacvLgzUYEZYL8IwJ7AqYMniT WtqQLdYIkWoZgQDhmg1hQWL/YJHXsviicF9xoZZRNpSUi2KUdfLAreHWO4j2t30xh42nWC AU2SeqyVeuohWF57MP/Wqq5s77i4cBR6dS1L9NKTTfHzDDjOmBPK7U2+U++B0n5GhNxvg7 fX+fpvRb+48zfvNlaV8O0Mzjb+K9lDD/ZFakLNVLJKFeJI9b3KxFWxhUfqxLruOVC89Z9q 8P0C1bWSATfBqdV1Pw7WsS76UpwS76h+KraP8r6wRQjEJImzNIEq3hfm4QYMUw== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 8d314ab5 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for ; Mon, 6 May 2024 10:50:42 +0000 (UTC) From: Herman Rimm Date: Mon, 6 May 2024 12:50:34 +0200 Message-ID: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=81.205.150.117; envelope-from=herman@rimm.ee; helo=email.rimm.ee X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_PBL=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.9 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.1 (/) * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'order' option. (guix-style): Alphabetically order packages in files. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- Hi, I managed to create a procedure which alphabetically sorts top-level package definitions. Sort is not stable as I understand it, so versions of packages get swapped. It works well enough in small package modules, and should not be a problem once package versions are used in sorting. Cheers, Herman doc/guix.texi | 6 +++++ guix/scripts/style.scm | 52 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 1c1e0164e7..6316f6bfc9 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15097,6 +15097,12 @@ Invoking guix style guix style -f /etc/config.scm @end example +@item --order +@itemx -o +Place the top-level package definitions in the given files in alphabetical +order. This option only has an effect in combination with +@option{--whole-file}. + @item --styling=@var{rule} @itemx -S @var{rule} Apply @var{rule}, one of the following styling rules: diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 211980dc1c..ace28c1bca 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021-2023 Ludovic Courtès +;;; Copyright © 2024 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,7 @@ (define-module (guix scripts style) #:autoload (gnu packages) (specification->package fold-packages) + #:use-module (guix combinators) #:use-module (guix scripts) #:use-module ((guix scripts build) #:select (%standard-build-options)) #:use-module (guix ui) @@ -494,11 +496,43 @@ (define (package-location name1 name2))))))) + (reverse (concatenate lst)))) + +(define* (format-whole-file file order? #:rest rest) + "Reformat all of FILE. When ORDER? is true, top-level package definitions + are put in alphabetical order." (with-fluids ((%default-port-encoding "UTF-8")) - (let ((lst (call-with-input-file file read-with-comments/sequence - #:guess-encoding #t))) + (let* ((lst (call-with-input-file file read-with-comments/sequence + #:guess-encoding #t)) + (lst (if order? + (order-packages lst) + lst))) (with-atomic-file-output file (lambda (port) (apply pretty-print-with-comments/splice port lst @@ -526,6 +560,9 @@ (define %options (option '(#\f "whole-file") #f #f (lambda (opt name arg result) (alist-cons 'whole-file? #t result))) + (option '(#\o "order") #f #f + (lambda (opt name arg result) + (alist-cons 'order? #t result))) (option '(#\S "styling") #t #f (lambda (opt name arg result) (alist-cons 'styling-procedure @@ -569,7 +606,7 @@ (define (show-help) (display (G_ " -S, --styling=RULE apply RULE, a styling rule")) (display (G_ " - -l, --list-stylings display the list of available style rules")) + -l, --list-stylings display the list of available style rules")) (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) @@ -584,6 +621,8 @@ (define (show-help) (newline) (display (G_ " -f, --whole-file format the entire contents of the given file(s)")) + (display (G_ " + -o, --order place the contents in alphabetical order as well")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -627,7 +666,8 @@ (define-command (guix-style . args) (warning (G_ "'--styling' option has no effect in whole-file mode~%"))) (when (null? files) (warning (G_ "no files specified, nothing to do~%"))) - (for-each format-whole-file files)) + (for-each format-whole-file + files (map (const (assoc-ref opts 'order?)) files))) (let ((packages (filter-map (match-lambda (('argument . spec) (specification->package spec)) base-commit: ef8ab6ab66c4d629699d175938ef1912941d4dce -- 2.41.0 From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'order' option to alphabetically order file. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 25 May 2024 14:16:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Herman Rimm Cc: 70800@debbugs.gnu.org, Josselin Poiret , Simon Tournier , Mathieu Othacehe , Tobias Geerinckx-Rice , Ricardo Wurmus , Christopher Baines Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.17166465322376 (code B ref 70800); Sat, 25 May 2024 14:16:01 +0000 Received: (at 70800) by debbugs.gnu.org; 25 May 2024 14:15:32 +0000 Received: from localhost ([127.0.0.1]:43685 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sAsBH-0000cF-Vv for submit@debbugs.gnu.org; Sat, 25 May 2024 10:15:32 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59610) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sAsBF-0000bp-US for 70800@debbugs.gnu.org; Sat, 25 May 2024 10:15:30 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sAsB0-0003Li-9c; Sat, 25 May 2024 10:15:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=U2oFtY6DngiY94kSAXJaOcKuy9uhaDTCjb7BYYfLe7E=; b=F2Sq3Bpv3SdkNBR+Sd49 8UbVGbniRsGeBeHfqhaKU23Sy0WL24JMeC2YxjbDby37SPlqPyQorC+ciQ11q6ERv0H6qYbzQtj6b tq2o9l92y0Njiv0Mat+XxqN/miSZkRgeEBJ6upkOPXvSveAQc00BltdPmV7xyWbXc5TafMVp9lC42 6xSzz9ZCdfi7K2rYc2Fkorc7aJAoJCmwYLkCXBmd5hYMP9/8DNlxaklMk4mFimkLwFDQ35v/k8nyr UZsp4dWqWkXmiTo9az2SMwjQDSW5eITFbEaVwDRUf8EHRXN1boAQWXn7W+/ymZPOrj6SHFX+kY4cm 5QS/64prQEbYlg==; From: Ludovic =?UTF-8?Q?Court=C3=A8s?= In-Reply-To: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> (Herman Rimm's message of "Mon, 6 May 2024 12:50:34 +0200") References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: Septidi 7 Prairial an 232 de la =?UTF-8?Q?R=C3=A9volution,?= jour du Fromental X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu Date: Sat, 25 May 2024 16:14:57 +0200 Message-ID: <875xv20yhq.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Herman Rimm skribis: > * guix/scripts/style.scm (show-help): Describe option. > (order-packages): Add procedure. > (format-whole-file): Add 'order?' argument. > (%options): Add 'order' option. > (guix-style): Alphabetically order packages in files. > * doc/guix.texi (Invoking guix style): Document option. > > Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 Yay! > I managed to create a procedure which alphabetically sorts top-level > package definitions. Sort is not stable as I understand it, so versions > of packages get swapped. It works well enough in small package modules, > and should not be a problem once package versions are used in sorting. Maybe use =E2=80=98stable-sort=E2=80=99 instead of =E2=80=98sort=E2=80=99? Overall LGTM; some suggestions below: > +(define (order-packages lst) > + "Place top-level package definitions in LST in alphabetical order." =E2=80=9CReturn LST, a list of top-level expressions and blanks, with top-l= evel package definitions in alphabetical order.=E2=80=9D > + ;; Group define-public with preceding blanks and defines. > + (let* ((lst (identity I=E2=80=99d drop =E2=80=98identity=E2=80=99. > + (package-name (lambda (pkg) > + (match pkg > + ((('define-public _ expr) _ ...) > + (match expr > + ((or ('package _ ('name name) _ ...) > + ('package ('name name) _ ...)) > + name) > + (_ #f))) > + (_ #f)))) Nitpick: I=E2=80=99d make this an inner =E2=80=98define=E2=80=99 in =E2=80= =98order-packages=E2=80=99, right below the docstring. > + (lst (sort lst (lambda (lst1 lst2) > + (let ((name1 (package-name lst1)) > + (name2 (package-name lst2))) > + (and name1 name2 (string> name1 name2))))))) > + (reverse (concatenate lst)))) Maybe replace =E2=80=98string>=E2=80=99 by =E2=80=98string + (option '(#\o "order") #f #f > + (lambda (opt name arg result) > + (alist-cons 'order? #t result))) I=E2=80=99d avoid =E2=80=98-o=E2=80=99 for this because it=E2=80=99s usuall= y synonymous with =E2=80=98output=E2=80=99. But maybe make it =E2=80=98-A=E2=80=99/=E2=80=98--alphabetical-sort=E2=80= =99? > (display (G_ " > - -l, --list-stylings display the list of available style rules")) > + -l, --list-stylings display the list of available style rules")) Oops. :-) > + (for-each format-whole-file > + files (map (const (assoc-ref opts 'order?)) files)= )) I=E2=80=99d go with something less inventive here: (for-each (cute format-whole-file <> (assoc-ref opts 'order?)) files) Could you send an updated patch? Thank you! Ludo=E2=80=99. From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'order' option to alphabetically order file. Resent-From: Simon Tournier Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 31 May 2024 16:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Ludovic =?UTF-8?Q?Court=C3=A8s?= , Herman Rimm Cc: 70800@debbugs.gnu.org, Josselin Poiret , Mathieu Othacehe , Tobias Geerinckx-Rice , Ricardo Wurmus , Christopher Baines Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.17171734293925 (code B ref 70800); Fri, 31 May 2024 16:38:02 +0000 Received: (at 70800) by debbugs.gnu.org; 31 May 2024 16:37:09 +0000 Received: from localhost ([127.0.0.1]:55245 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sD5Fd-00011E-73 for submit@debbugs.gnu.org; Fri, 31 May 2024 12:37:09 -0400 Received: from mail-lj1-f173.google.com ([209.85.208.173]:38344) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sD5Fa-00010g-RY for 70800@debbugs.gnu.org; Fri, 31 May 2024 12:37:07 -0400 Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2ea9814d3caso730861fa.1 for <70800@debbugs.gnu.org>; Fri, 31 May 2024 09:36:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717173350; x=1717778150; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=1xuZbnWSHkTjLnfStzbHZZkaLTmjpRjPiUB9XEC5TaU=; b=hQyvLoO8271dN2tKx8NyNv3XLMrVbKWDcXIG78sUzHpUC4fxCs0nkO8ai3FfQq7idX 8foG+LXaj32c6M1l0HHIXyV1uGVXWuhDF4SxQHVHIj2TAfmvE/7n0FvUCzjd2XTACqE+ IMNApHF7VF3C+v34y06/3uYKXKv2tgf0g5IV3n4oroEDOwz93tYDtHvAo6e7MTb3iTOo mqPd2IzBb9eIVIlOFl5jOQj6XM2AgT/vX2M/mCdcEdZm700oXQM9n6ZsMcwkQi4ED597 ioePxpX+BZ+9NomsSU7b5+lBbLSinoh2ANbxLubidK2XTT12nFQhKpEsmdmcwBrtaadH y9BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717173350; x=1717778150; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1xuZbnWSHkTjLnfStzbHZZkaLTmjpRjPiUB9XEC5TaU=; b=LcWUEdtw7SKIoZ5W9Lnv1nYvpXZLXjBSJ3HdSBmd849PlNdk/blf2oIIn6F/F73TcT 8TxnYJmV8mkpRO0EknlCA0BnTOCGUrwKi2w23aMPM9VN7t9EK8yUvCiyNYNyfoB5wVfs 8Q55kHIullTtckDA9gX4RycqfgAXZYNqFv2is++q8i8eW/tcFYZd4o+78PPUWWH+MwLr cSj+kHnpgINSP6illBJdhmSN0IGXErU+bGwQAR1FEpTqmJUz8MXjpF7aDKI26suxLbUl H9ETlOXZYEVesysqaDGm5rdiZyzQ0+m8Xhrxq6Y40UFJZpu7XPD7ZMdCRMDtena+K1Qp eOWw== X-Gm-Message-State: AOJu0YwvQlwWHHIqtQWiJ2AnL0gzCGb2i6JxckJhkCyYWjSopT1yV/FA Oy8U8vH27ne0n8e3nGuUTZqPtGZqO6RhVjKXpnNZJvXWFPmNAnLE X-Google-Smtp-Source: AGHT+IH4q34iRXc62mA97L1EpQktk+KGD4tyu6Ub59nREbu8Eu1YSPbmyjVV/EV7vzQwpR2OvBJwkA== X-Received: by 2002:a05:651c:21a:b0:2ea:814b:f57a with SMTP id 38308e7fff4ca-2ea950a0cedmr15444191fa.1.1717173349373; Fri, 31 May 2024 09:35:49 -0700 (PDT) Received: from lili ([81.185.168.12]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42132471e01sm16637515e9.13.2024.05.31.09.35.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 09:35:48 -0700 (PDT) From: Simon Tournier In-Reply-To: <875xv20yhq.fsf@gnu.org> References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> <875xv20yhq.fsf@gnu.org> Date: Fri, 31 May 2024 17:36:09 +0200 Message-ID: <87ed9i3seu.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Hi, On Sat, 25 May 2024 at 16:14, Ludovic Court=C3=A8s wrote: >> I managed to create a procedure which alphabetically sorts top-level >> package definitions. Sort is not stable as I understand it, so versions >> of packages get swapped. It works well enough in small package modules, >> and should not be a problem once package versions are used in sorting. > > Maybe use =E2=80=98stable-sort=E2=80=99 instead of =E2=80=98sort=E2=80=99? [...] >> + (lst (sort lst (lambda (lst1 lst2) >> + (let ((name1 (package-name lst1)) >> + (name2 (package-name lst2))) >> + (and name1 name2 (string> name1 name2))))))) >> + (reverse (concatenate lst)))) > > Maybe replace =E2=80=98string>=E2=80=99 by =E2=80=98string8--- (sort! lst (lambda (lst1 lst2) (let ((name1 (package-name lst1)) (name2 (package-name lst2))) (and name1 name2 (or (string8--- with =E2=80=99version-compare=E2=80=99 from (guix utils). Well, something = like that. :-) Cheers, simon From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'order' option to alphabetically order file. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 26 Jun 2024 21:33:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Herman Rimm Cc: 70800@debbugs.gnu.org, Josselin Poiret , Simon Tournier , Mathieu Othacehe , Tobias Geerinckx-Rice , Ricardo Wurmus , Christopher Baines Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.17194375447278 (code B ref 70800); Wed, 26 Jun 2024 21:33:01 +0000 Received: (at 70800) by debbugs.gnu.org; 26 Jun 2024 21:32:24 +0000 Received: from localhost ([127.0.0.1]:40479 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sMaFc-0001tH-0f for submit@debbugs.gnu.org; Wed, 26 Jun 2024 17:32:24 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57854) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sMaFZ-0001t3-CH for 70800@debbugs.gnu.org; Wed, 26 Jun 2024 17:32:22 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sMaFO-0006BW-5H; Wed, 26 Jun 2024 17:32:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=dWOk9ABYrMCWEhIsmcX3KcyKKq5Zp2NdwPVhy6t7d8o=; b=d2HTX/FBS2fNYclmcWLf 2qSnWGKxIpIM92t3XWlOf8j9BPOGlzEzowMbTUBOOqXU48bTVyDtGtYyZ60DeULFhE0SbR8+Zatsj ToVEnGrzgVtj0QyeN0GalsCOu9cIuTCbx6Rkkt9aq2+pm/aVKC0B9+9rRMcdszj3zlEDQqGGPGVhd 3ZcRNWLtwZrNtgtxEe29VixUg7NEFPYJrTLgA1DemCo+9UB+PY3aBN6uyrjP3MpN4AQPnxwG3T7RA jb4teYHFEH5uTKMmG6hzDa8wmXUIQb+QmmPhlt7bUoKD4WKNEcAaVtX+xKkj2dFKkR9Pu5Ussvi5E Q/V0aRC/6g/gkw==; From: Ludovic =?UTF-8?Q?Court=C3=A8s?= In-Reply-To: <875xv20yhq.fsf@gnu.org> ("Ludovic =?UTF-8?Q?Court=C3=A8s?="'s message of "Sat, 25 May 2024 16:14:57 +0200") References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> <875xv20yhq.fsf@gnu.org> Date: Wed, 26 Jun 2024 23:32:05 +0200 Message-ID: <87y16r9yqy.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Hi Herman, Did you have a chance to look into this? Let us know if anything looks unclear or questionable to you. Thanks, Ludo=E2=80=99. Ludovic Court=C3=A8s skribis: > Herman Rimm skribis: > >> * guix/scripts/style.scm (show-help): Describe option. >> (order-packages): Add procedure. >> (format-whole-file): Add 'order?' argument. >> (%options): Add 'order' option. >> (guix-style): Alphabetically order packages in files. >> * doc/guix.texi (Invoking guix style): Document option. >> >> Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 > > Yay! > >> I managed to create a procedure which alphabetically sorts top-level >> package definitions. Sort is not stable as I understand it, so versions >> of packages get swapped. It works well enough in small package modules, >> and should not be a problem once package versions are used in sorting. > > Maybe use =E2=80=98stable-sort=E2=80=99 instead of =E2=80=98sort=E2=80=99? > > Overall LGTM; some suggestions below: > >> +(define (order-packages lst) >> + "Place top-level package definitions in LST in alphabetical order." > > =E2=80=9CReturn LST, a list of top-level expressions and blanks, with top= -level > package definitions in alphabetical order.=E2=80=9D > >> + ;; Group define-public with preceding blanks and defines. >> + (let* ((lst (identity > > I=E2=80=99d drop =E2=80=98identity=E2=80=99. > >> + (package-name (lambda (pkg) >> + (match pkg >> + ((('define-public _ expr) _ ...) >> + (match expr >> + ((or ('package _ ('name name) _ ...) >> + ('package ('name name) _ ...)) >> + name) >> + (_ #f))) >> + (_ #f)))) > > Nitpick: I=E2=80=99d make this an inner =E2=80=98define=E2=80=99 in =E2= =80=98order-packages=E2=80=99, right > below the docstring. > >> + (lst (sort lst (lambda (lst1 lst2) >> + (let ((name1 (package-name lst1)) >> + (name2 (package-name lst2))) >> + (and name1 name2 (string> name1 name2))))))) >> + (reverse (concatenate lst)))) > > Maybe replace =E2=80=98string>=E2=80=99 by =E2=80=98string >> + (option '(#\o "order") #f #f >> + (lambda (opt name arg result) >> + (alist-cons 'order? #t result))) > > I=E2=80=99d avoid =E2=80=98-o=E2=80=99 for this because it=E2=80=99s usua= lly synonymous with =E2=80=98output=E2=80=99. > > But maybe make it =E2=80=98-A=E2=80=99/=E2=80=98--alphabetical-sort=E2=80= =99? > >> (display (G_ " >> - -l, --list-stylings display the list of available style rules")) >> + -l, --list-stylings display the list of available style rules")) > > Oops. :-) > >> + (for-each format-whole-file >> + files (map (const (assoc-ref opts 'order?)) files= ))) > > I=E2=80=99d go with something less inventive here: > > (for-each (cute format-whole-file <> (assoc-ref opts 'order?)) > files) > > Could you send an updated patch? > > Thank you! > > Ludo=E2=80=99. From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH v2] scripts: style: Add 'alphabetical-sort' option. References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> In-Reply-To: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 04 Jul 2024 15:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 70800@debbugs.gnu.org Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.172010854422286 (code B ref 70800); Thu, 04 Jul 2024 15:56:02 +0000 Received: (at 70800) by debbugs.gnu.org; 4 Jul 2024 15:55:44 +0000 Received: from localhost ([127.0.0.1]:42808 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sPOoB-0005nN-Rw for submit@debbugs.gnu.org; Thu, 04 Jul 2024 11:55:44 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:42489 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sPOo6-0005mw-Gp for 70800@debbugs.gnu.org; Thu, 04 Jul 2024 11:55:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1720108526; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bGqr3gQ6FDVFNhb4WmFAuKtR4M5eI5TMoW2vT1u8dtw=; b=krQAJ9w0TCRIHebe2F2f+4grB4zYdUZpW6h4c36/t94VFADyhmEta3D7sJzCoI0KTXNnVh JaLfxaO+nZn4yItHSWr5bOw7p2URLm0GrwSjwlXCQcvHJ8SgLydETlETUDTuoC7XZpfVrz W0gp3akWx7v0yxVkA6IKdH0HyQg7ENFn9lTw/RTVzY/Ny2UQ2Hw0JGnzTLQlb5aacPEHsc tjfbf4pxPJyP9H9scuhN+NbHDhXw62DlEM7PGFr5PHN1jQzdRGxwXSOSXQEpESyd5tpmag kSA8HhAOWjH4zi1jbtUlg77kQNO6hMWcGT2OAhjfYughfSgauVtTTHhR5tivBA== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 541905ae (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for <70800@debbugs.gnu.org>; Thu, 4 Jul 2024 15:55:26 +0000 (UTC) From: Herman Rimm Date: Thu, 4 Jul 2024 17:55:06 +0200 Message-ID: <20240704155507.20383-1-herman@rimm.ee> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 3.6 (+++) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetic [...] Content analysis details: (3.6 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 TVD_RCVD_IP Message was received from an IP address 3.6 RCVD_IN_PBL RBL: Received via a relay in Spamhaus PBL [81.205.150.117 listed in zen.spamhaus.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 T_SPF_HELO_TEMPERROR SPF: test of HELO record failed (temperror) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 2.6 (++) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetic [...] Content analysis details: (2.6 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 3.6 RCVD_IN_PBL RBL: Received via a relay in Spamhaus PBL [81.205.150.117 listed in zen.spamhaus.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -1.0 MAILING_LIST_MULTI Multiple indicators imply a widely-seen list manager * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetically order packages in files. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- Hi, I should have mentioned it back then, but in May and again today I tried to not use a reverse. If you can do a sort without it, please amend the patch. This revision takes the other feedback into account. Cheers, Herman doc/guix.texi | 7 ++++ guix/scripts/style.scm | 73 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 6770c9d664..3e598f812b 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15190,6 +15190,13 @@ configuration (you need write permissions for the file): guix style -f /etc/config.scm @end example +@item --alphabetical-sort +@itemx -A +Place the top-level package definitions in the given files in +alphabetical order. Package definitions with matching names are placed +with versions in descending order. This option only has an effect in +combination with @option{--whole-file}. + @item --styling=@var{rule} @itemx -S @var{rule} Apply @var{rule}, one of the following styling rules: diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 0727ac1480..5f4ee4a492 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021-2024 Ludovic Courtès +;;; Copyright © 2024 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,7 @@ (define-module (guix scripts style) #:autoload (gnu packages) (specification->package fold-packages) + #:use-module (guix combinators) #:use-module (guix scripts) #:use-module ((guix scripts build) #:select (%standard-build-options)) #:use-module (guix ui) @@ -494,11 +496,62 @@ (define (package-location? lst1 lst2) + (let ((name1 (package-name lst1)) + (name2 (package-name lst2)) + (version1 (package-version lst1)) + (version2 (package-version lst2))) + (and name1 name2 (or (string>? name1 name2) + (and (string=? name1 name2) + version1 + version2 + (version>? version2 version1)))))) + + ;; Group define-public with preceding blanks and defines. + (let ((lst (fold2 (lambda (expr tail head) + (let ((head (cons expr head))) + (match expr + ((? blank?) + (values tail head)) + (('define _ ...) + (values tail head)) + (_ (values (cons head tail) '()))))) + '() '() lst))) + (reverse (concatenate (sort! lst package>?))))) + +(define* (format-whole-file file order? #:rest rest) + "Reformat all of FILE. When ORDER? is true, top-level package definitions +are put in alphabetical order." (with-fluids ((%default-port-encoding "UTF-8")) - (let ((lst (call-with-input-file file read-with-comments/sequence - #:guess-encoding #t))) + (let* ((lst (call-with-input-file file read-with-comments/sequence + #:guess-encoding #t)) + (lst (if order? + (order-packages lst) + lst))) (with-atomic-file-output file (lambda (port) (apply pretty-print-with-comments/splice port lst @@ -526,6 +579,9 @@ (define %options (option '(#\f "whole-file") #f #f (lambda (opt name arg result) (alist-cons 'whole-file? #t result))) + (option '(#\A "--alphabetical-sort") #f #f + (lambda (opt name arg result) + (alist-cons 'order? #t result))) (option '(#\S "styling") #t #f (lambda (opt name arg result) (alist-cons 'styling-procedure @@ -569,7 +625,7 @@ (define (show-help) (display (G_ " -S, --styling=RULE apply RULE, a styling rule")) (display (G_ " - -l, --list-stylings display the list of available style rules")) + -l, --list-stylings display the list of available style rules")) (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) @@ -584,6 +640,9 @@ (define (show-help) (newline) (display (G_ " -f, --whole-file format the entire contents of the given file(s)")) + (display (G_ " + -A, --alphabetical-sort + place the contents in alphabetical order as well")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -627,7 +686,9 @@ (define (parse-options) (warning (G_ "'--styling' option has no effect in whole-file mode~%"))) (when (null? files) (warning (G_ "no files specified, nothing to do~%"))) - (for-each format-whole-file files)) + (for-each + (cute format-whole-file <> (assoc-ref opts 'order?)) + files)) (let ((packages (filter-map (match-lambda (('argument . spec) (specification->package spec)) -- 2.45.1 From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH v2] scripts: style: Add 'alphabetical-sort' option. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 Jul 2024 15:14:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Herman Rimm Cc: 70800@debbugs.gnu.org Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.172131558720547 (code B ref 70800); Thu, 18 Jul 2024 15:14:02 +0000 Received: (at 70800) by debbugs.gnu.org; 18 Jul 2024 15:13:07 +0000 Received: from localhost ([127.0.0.1]:37850 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sUSod-0005LK-3p for submit@debbugs.gnu.org; Thu, 18 Jul 2024 11:13:07 -0400 Received: from eggs.gnu.org ([209.51.188.92]:46636) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sUSob-0005Kc-JP for 70800@debbugs.gnu.org; Thu, 18 Jul 2024 11:13:06 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sUSoP-0004TK-UP; Thu, 18 Jul 2024 11:12:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=lFDAKIkNCCqSMy/HvJ/3ueFKeZmx2oRPbLgB//YNlWY=; b=RoRUZzfoKidPfO4khjb9 1j5xHFyI5DTNeZiZKc8A+RhqopOyOAlqZdwsm9USDv2U2zfeS1azaRyQaLJpJW31Ah5kJPklk9Cbs BsAzivsdLn6+yipPJodUxS+/z1bRMgNsjbljQcrG4af6isziRV+mFwqMqTE6RB3U3cjWWIbLsQgTN 6w84S9A9WJJj0uj+KqgmOwZwQa8RoPyeTqw95U34/2JAkbRViAlU+oiwSFUcVQItD1AoI8h8LfM3O hIdLz1OKeaMp51z9TDedI3mNx+AsmIX6m6io+LEC5p2mlXXdJyDHuSuhlsk8zxMDBmzkBtENGUk5t RfpdD3JCO7oy/A==; From: Ludovic =?UTF-8?Q?Court=C3=A8s?= In-Reply-To: <20240704155507.20383-1-herman@rimm.ee> (Herman Rimm's message of "Thu, 4 Jul 2024 17:55:06 +0200") References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> <20240704155507.20383-1-herman@rimm.ee> Date: Thu, 18 Jul 2024 17:12:51 +0200 Message-ID: <87wmlivimk.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Hi Herman, Herman Rimm skribis: > * guix/scripts/style.scm (show-help): Describe option. > (order-packages): Add procedure. > (format-whole-file): Add 'order?' argument. > (%options): Add 'alphabetical-sort' option. > (guix-style): Alphabetically order packages in files. > * doc/guix.texi (Invoking guix style): Document option. > > Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 > --- > Hi, > > I should have mentioned it back then, but in May and again today I tried > to not use a reverse. If you can do a sort without it, please amend the > patch. This revision takes the other feedback into account. This looks great to me, thanks! I realize I forgot to ask you to add tests to =E2=80=98tests/guix-style.sh=E2=80=99=E2=80=94my bad. Would you be willing and able to submit an updated version with tests? If not (which I=E2=80=99d understand), I can allocate time to do it. Thanks, Ludo=E2=80=99. From unknown Fri Aug 15 20:52:03 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'alphabetical-sort' option. References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> In-Reply-To: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 02 Sep 2024 19:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 70800@debbugs.gnu.org Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.17253035976621 (code B ref 70800); Mon, 02 Sep 2024 19:00:02 +0000 Received: (at 70800) by debbugs.gnu.org; 2 Sep 2024 18:59:57 +0000 Received: from localhost ([127.0.0.1]:52378 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slCHN-0001ig-8L for submit@debbugs.gnu.org; Mon, 02 Sep 2024 14:59:57 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:50065 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slCHK-0001iL-Jw for 70800@debbugs.gnu.org; Mon, 02 Sep 2024 14:59:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1725303525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=L7KreTjyUqEe6j666ZmOAiPeRLX1t+ufR4IP+vPElYE=; b=MutxtmFA+2+t0DoYy/+UXloLuo/otzXCzg3rNQH6ALwH+qse5azeUrwFSsq4UeB8SndFRf 4XtLamGui3/XkTbao0DT0lNAmQOx11BgyQ9lQhjlCj7EL6ZzN3fWBbzhbOkoc92NKHl2dC 8IZ5EmzTTKP5MeLs5S43lR9nnNAQk/MQitFXKUqT963mecKO3irjCw7IMrq6P/w0ZaUmyM TaZGO34n50PUry6nHOlCU8I3ochqfr98TTVBx50LtfdXi/mHqfkaJhrCUkALC4QY3bs1Y1 g8vZ6LdM0+I2/rraPBXVgaS8EKfxUO8LU6Tn1LbyLobegOT6bm98idhyW5W/8Q== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 10b1b737 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for <70800@debbugs.gnu.org>; Mon, 2 Sep 2024 18:58:45 +0000 (UTC) From: Herman Rimm Date: Mon, 2 Sep 2024 20:58:05 +0200 Message-ID: <20240902185808.12842-1-herman@rimm.ee> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 3.6 (+++) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetic [...] Content analysis details: (3.6 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 TVD_RCVD_IP Message was received from an IP address 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 3.6 RCVD_IN_PBL RBL: Received via a relay in Spamhaus PBL [81.205.150.117 listed in zen.spamhaus.org] -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 2.6 (++) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetic [...] Content analysis details: (2.6 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 3.6 RCVD_IN_PBL RBL: Received via a relay in Spamhaus PBL [81.205.150.117 listed in zen.spamhaus.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -1.0 MAILING_LIST_MULTI Multiple indicators imply a widely-seen list manager * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetically order packages in files. * tests/guix-style.sh: Test alphabetical ordering. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- doc/guix.texi | 7 ++++ guix/scripts/style.scm | 73 ++++++++++++++++++++++++++++++++++++++---- tests/guix-style.sh | 26 +++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index f7dbe246a9..6f6f952f96 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15216,6 +15216,13 @@ configuration (you need write permissions for the file): guix style -f /etc/config.scm @end example +@item --alphabetical-sort +@itemx -A +Place the top-level package definitions in the given files in +alphabetical order. Package definitions with matching names are placed +with versions in descending order. This option only has an effect in +combination with @option{--whole-file}. + @item --styling=@var{rule} @itemx -S @var{rule} Apply @var{rule}, one of the following styling rules: diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 1802b854d0..73e3feadee 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021-2024 Ludovic Courtès +;;; Copyright © 2024 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,7 @@ (define-module (guix scripts style) #:autoload (gnu packages) (specification->package fold-packages) + #:use-module (guix combinators) #:use-module (guix scripts) #:use-module ((guix scripts build) #:select (%standard-build-options)) #:use-module (guix ui) @@ -485,11 +487,62 @@ (define* (format-package-definition package ;;; Whole-file formatting. ;;; -(define* (format-whole-file file #:rest rest) - "Reformat all of FILE." +(define (order-packages lst) + "Return LST, a list of top-level expressions and blanks, with +top-level package definitions in alphabetical order. Packages which +share a name are placed with versions in descending order." + (define (package-name pkg) + (match pkg + ((('define-public _ expr) _ ...) + (match expr + ((or ('package _ ('name name) _ ...) + ('package ('name name) _ ...)) + name) + (_ #f))) + (_ #f))) + + (define (package-version pkg) + (match pkg + ((('define-public _ expr) _ ...) + (match expr + ((or ('package _ _ ('version version) _ ...) + ('package _ ('version version) _ ...)) + version) + (_ #f))) + (_ #f))) + + (define (package>? lst1 lst2) + (let ((name1 (package-name lst1)) + (name2 (package-name lst2)) + (version1 (package-version lst1)) + (version2 (package-version lst2))) + (and name1 name2 (or (string>? name1 name2) + (and (string=? name1 name2) + version1 + version2 + (version>? version2 version1)))))) + + ;; Group define-public with preceding blanks and defines. + (let ((lst (fold2 (lambda (expr tail head) + (let ((head (cons expr head))) + (match expr + ((? blank?) + (values tail head)) + (('define _ ...) + (values tail head)) + (_ (values (cons head tail) '()))))) + '() '() lst))) + (reverse (concatenate (sort! lst package>?))))) + +(define* (format-whole-file file order? #:rest rest) + "Reformat all of FILE. When ORDER? is true, top-level package definitions +are put in alphabetical order." (with-fluids ((%default-port-encoding "UTF-8")) - (let ((lst (call-with-input-file file read-with-comments/sequence - #:guess-encoding #t))) + (let* ((lst (call-with-input-file file read-with-comments/sequence + #:guess-encoding #t)) + (lst (if order? + (order-packages lst) + lst))) (with-atomic-file-output file (lambda (port) (apply pretty-print-with-comments/splice port lst @@ -517,6 +570,9 @@ (define %options (option '(#\f "whole-file") #f #f (lambda (opt name arg result) (alist-cons 'whole-file? #t result))) + (option '(#\A "--alphabetical-sort") #f #f + (lambda (opt name arg result) + (alist-cons 'order? #t result))) (option '(#\S "styling") #t #f (lambda (opt name arg result) (alist-cons 'styling-procedure @@ -560,7 +616,7 @@ (define (show-help) (display (G_ " -S, --styling=RULE apply RULE, a styling rule")) (display (G_ " - -l, --list-stylings display the list of available style rules")) + -l, --list-stylings display the list of available style rules")) (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) @@ -575,6 +631,9 @@ (define (show-help) (newline) (display (G_ " -f, --whole-file format the entire contents of the given file(s)")) + (display (G_ " + -A, --alphabetical-sort + place the contents in alphabetical order as well")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -618,7 +677,9 @@ (define (parse-options) (warning (G_ "'--styling' option has no effect in whole-file mode~%"))) (when (null? files) (warning (G_ "no files specified, nothing to do~%"))) - (for-each format-whole-file files)) + (for-each + (cute format-whole-file <> (assoc-ref opts 'order?)) + files)) (let ((packages (filter-map (match-lambda (('argument . spec) (specification->package spec)) diff --git a/tests/guix-style.sh b/tests/guix-style.sh index 2de879d5e3..9333139435 100644 --- a/tests/guix-style.sh +++ b/tests/guix-style.sh @@ -58,6 +58,24 @@ cat > "$tmpfile" < Subject: bug#70800: closed (Re: [bug#70800] [PATCH] scripts: style: Add 'alphabetical-sort' option.) Message-ID: References: <87r0a1hxr7.fsf@gnu.org> <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> X-Gnu-PR-Message: they-closed 70800 X-Gnu-PR-Package: guix-patches X-Gnu-PR-Keywords: patch Reply-To: 70800@debbugs.gnu.org Date: Tue, 03 Sep 2024 09:54:02 +0000 Content-Type: multipart/mixed; boundary="----------=_1725357242-24323-1" This is a multi-part message in MIME format... ------------=_1725357242-24323-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #70800: [PATCH] scripts: style: Add 'order' option to alphabetically order = file. which was filed against the guix-patches package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 70800@debbugs.gnu.org. --=20 70800: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D70800 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1725357242-24323-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 70800-done) by debbugs.gnu.org; 3 Sep 2024 09:53:45 +0000 Received: from localhost ([127.0.0.1]:58998 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slQEK-0006Jh-VZ for submit@debbugs.gnu.org; Tue, 03 Sep 2024 05:53:45 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54126) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slQEJ-0006JS-17 for 70800-done@debbugs.gnu.org; Tue, 03 Sep 2024 05:53:43 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1slQB5-0006cQ-9a; Tue, 03 Sep 2024 05:50:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:References:In-Reply-To:Subject:To: From; bh=dX5m4OcKQmWFPSukIqC4aCKhFkvKZkM0yw6uiPCSW9E=; b=OR9uXHbp+yj+vS8e0JfX cjCOD6RHelqqg2qJi8wwePYmTu8QQXmT2cgW+RdbObXACj7Tt8BAoWLaz733gvS+nyzUbHkooZlNa a8N6GojSOGr9xOPdGa9fxYUnxhfWsn3NZGt6TFfalZrrXZtmDr1H+CDKpheLUGZvMGxvY7pWwLOxF +BRpeLqLZJcu7vw7a8x+WP1zPx2KdrHn46uThTKxE/LZNciDgDyVF4INOdYRnZdTJjCPhfvsQga29 Cfvd4I12wHEzROrIf4Eu5Jl7GFSTDH2f1ZkV8rbn6pwhb9fch4f4hmxBF1DHSD+sBi9ZsXfENrl0t tCw/TD6MuW2N1w==; From: =?utf-8?Q?Ludovic_Court=C3=A8s?= To: Herman Rimm Subject: Re: [bug#70800] [PATCH] scripts: style: Add 'alphabetical-sort' option. In-Reply-To: <20240902185808.12842-1-herman@rimm.ee> (Herman Rimm's message of "Mon, 2 Sep 2024 20:58:05 +0200") References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> <20240902185808.12842-1-herman@rimm.ee> Date: Tue, 03 Sep 2024 11:50:20 +0200 Message-ID: <87r0a1hxr7.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 70800-done Cc: 70800-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Hi Herman, Herman Rimm skribis: > * guix/scripts/style.scm (show-help): Describe option. > (order-packages): Add procedure. > (format-whole-file): Add 'order?' argument. > (%options): Add 'alphabetical-sort' option. > (guix-style): Alphabetically order packages in files. > * tests/guix-style.sh: Test alphabetical ordering. > * doc/guix.texi (Invoking guix style): Document option. > > Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 Finally applied, thank you! Ludo=E2=80=99. ------------=_1725357242-24323-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 6 May 2024 10:51:33 +0000 Received: from localhost ([127.0.0.1]:37201 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3vwS-000833-W5 for submit@debbugs.gnu.org; Mon, 06 May 2024 06:51:33 -0400 Received: from lists.gnu.org ([2001:470:142::17]:56194) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3vwO-00082u-9B for submit@debbugs.gnu.org; Mon, 06 May 2024 06:51:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s3vvu-00062k-PJ for guix-patches@gnu.org; Mon, 06 May 2024 06:50:58 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117] helo=email.rimm.ee) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1s3vvl-0004tT-De for guix-patches@gnu.org; Mon, 06 May 2024 06:50:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1714992642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=WVdFGj1eni+Ricg5xwFr4yv4nH/HcOyKDOWKEnt+Hf0=; b=haHERx8BijRNpzaAdaFi0bo5eazRdQp+8g0bFMyp/00nCbacvLgzUYEZYL8IwJ7AqYMniT WtqQLdYIkWoZgQDhmg1hQWL/YJHXsviicF9xoZZRNpSUi2KUdfLAreHWO4j2t30xh42nWC AU2SeqyVeuohWF57MP/Wqq5s77i4cBR6dS1L9NKTTfHzDDjOmBPK7U2+U++B0n5GhNxvg7 fX+fpvRb+48zfvNlaV8O0Mzjb+K9lDD/ZFakLNVLJKFeJI9b3KxFWxhUfqxLruOVC89Z9q 8P0C1bWSATfBqdV1Pw7WsS76UpwS76h+KraP8r6wRQjEJImzNIEq3hfm4QYMUw== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 8d314ab5 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for ; Mon, 6 May 2024 10:50:42 +0000 (UTC) From: Herman Rimm To: guix-patches@gnu.org Subject: [PATCH] scripts: style: Add 'order' option to alphabetically order file. Date: Mon, 6 May 2024 12:50:34 +0200 Message-ID: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Debbugs-Cc: Christopher Baines , Josselin Poiret , Ludovic Courtès , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=81.205.150.117; envelope-from=herman@rimm.ee; helo=email.rimm.ee X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_PBL=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.9 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.1 (/) * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'order' option. (guix-style): Alphabetically order packages in files. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- Hi, I managed to create a procedure which alphabetically sorts top-level package definitions. Sort is not stable as I understand it, so versions of packages get swapped. It works well enough in small package modules, and should not be a problem once package versions are used in sorting. Cheers, Herman doc/guix.texi | 6 +++++ guix/scripts/style.scm | 52 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 1c1e0164e7..6316f6bfc9 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15097,6 +15097,12 @@ Invoking guix style guix style -f /etc/config.scm @end example +@item --order +@itemx -o +Place the top-level package definitions in the given files in alphabetical +order. This option only has an effect in combination with +@option{--whole-file}. + @item --styling=@var{rule} @itemx -S @var{rule} Apply @var{rule}, one of the following styling rules: diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 211980dc1c..ace28c1bca 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021-2023 Ludovic Courtès +;;; Copyright © 2024 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,7 @@ (define-module (guix scripts style) #:autoload (gnu packages) (specification->package fold-packages) + #:use-module (guix combinators) #:use-module (guix scripts) #:use-module ((guix scripts build) #:select (%standard-build-options)) #:use-module (guix ui) @@ -494,11 +496,43 @@ (define (package-location name1 name2))))))) + (reverse (concatenate lst)))) + +(define* (format-whole-file file order? #:rest rest) + "Reformat all of FILE. When ORDER? is true, top-level package definitions + are put in alphabetical order." (with-fluids ((%default-port-encoding "UTF-8")) - (let ((lst (call-with-input-file file read-with-comments/sequence - #:guess-encoding #t))) + (let* ((lst (call-with-input-file file read-with-comments/sequence + #:guess-encoding #t)) + (lst (if order? + (order-packages lst) + lst))) (with-atomic-file-output file (lambda (port) (apply pretty-print-with-comments/splice port lst @@ -526,6 +560,9 @@ (define %options (option '(#\f "whole-file") #f #f (lambda (opt name arg result) (alist-cons 'whole-file? #t result))) + (option '(#\o "order") #f #f + (lambda (opt name arg result) + (alist-cons 'order? #t result))) (option '(#\S "styling") #t #f (lambda (opt name arg result) (alist-cons 'styling-procedure @@ -569,7 +606,7 @@ (define (show-help) (display (G_ " -S, --styling=RULE apply RULE, a styling rule")) (display (G_ " - -l, --list-stylings display the list of available style rules")) + -l, --list-stylings display the list of available style rules")) (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) @@ -584,6 +621,8 @@ (define (show-help) (newline) (display (G_ " -f, --whole-file format the entire contents of the given file(s)")) + (display (G_ " + -o, --order place the contents in alphabetical order as well")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -627,7 +666,8 @@ (define-command (guix-style . args) (warning (G_ "'--styling' option has no effect in whole-file mode~%"))) (when (null? files) (warning (G_ "no files specified, nothing to do~%"))) - (for-each format-whole-file files)) + (for-each format-whole-file + files (map (const (assoc-ref opts 'order?)) files))) (let ((packages (filter-map (match-lambda (('argument . spec) (specification->package spec)) base-commit: ef8ab6ab66c4d629699d175938ef1912941d4dce -- 2.41.0 ------------=_1725357242-24323-1--