Subject: [PATCH] utils: Add find-unordered-packages procedure.

From: Herman Rimm <[email-masked]>
Date: Sun, 21 Apr 2024 19:59:49 +0200

* guix/utils.scm (find-unordered-packages): Add and export procedure.
* tests/utils.scm ("find-unordered-packages"): Add test.

Change-Id: I26ea0fca7d428b192711f75ff3cf5e5a4416b1b6
---
Hello,

I added a procedure which returns the line numbers where packages break
from alphabetical order, e.g.:

  scheme@(guix-user)> (lset-xor eqv? (find-unordered-packages
  "gnu/packages/crates-io.scm"))
  $1 = (626 1153 2693 2715 2840 4074 4139 4187 6099 6243 6382 6896 7677
  9465 10346 11424 12089 12552 12676 12887 16147 16364 16897 18195 18260
  19912 21335 22489 22792 23898 24344 24801 25443 25492 26591 27832
  27944 31206 32365 32502 32609 32770 34281 34913 36192 36320 36621
  36718 36828 37511 38169 39000 39360 39684 40921 41153 41459 41501
  42121 42803 42910 44318 44585 46435 47350 47456 47709 48043 49624
  49905 51084 51216 51560 53586 54473 57062 58925 59048 59134 59584
  59664 59702 59914 62399 66006 66266 66391 68003 68200 68244 70292
  70321 71660 71686 71761 72643 73746 73880 74434 74459 77309 77646
  78136 79569 81722 81896 83802 85617 87167 89037 92212)

It could be extended to also expect package versions in decreasing
order. Maybe I can use it to sort packages automatically later. 

Cheers,
Herman
 guix/utils.scm  | 21 +++++++++++++++++++++
 tests/utils.scm | 19 +++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/guix/utils.scm b/guix/utils.scm
index d8ce6ed886..f690618306 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -148,6 +148,7 @@ (define-module (guix utils)
             delete-expression
             insert-expression
             find-definition-insertion-location
+            find-unordered-packages
 
             filtered-port
             decompressed-port
@@ -531,6 +532,26 @@ (define (find-definition-insertion-location file term)
            (and (not (eof-object? syntax))
                 (syntax-source syntax))))))))
 
+(define (find-unordered-packages file)
+  "Return the line numbers of top-level package definitions whose name
+alphabetically preceeds the previous name."
+  (call-with-input-file file
+    (lambda (port)
+      (let loop ((lst '())
+                 (previous-name ""))
+        (match (read-syntax port)
+          ((? eof-object?)
+           (reverse! lst))
+          (exp
+            (match (syntax->datum exp)
+              (('define-public _ (or ('package _ ('name name) _ ...)
+                                     ('package ('name name) _ ...)))
+               (loop (if (string< name previous-name)
+                         (cons (assoc-ref (syntax-source exp) 'line) lst)
+                         lst)
+                     name))
+              (_ (loop lst previous-name)))))))))
+
 
 ;;;
 ;;; Keyword arguments.
diff --git a/tests/utils.scm b/tests/utils.scm
index 462e43e2b1..8956ea5420 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -303,6 +303,25 @@ (define-public package-2\n  'package)\n"
            (find-definition-insertion-location temp-file term))
          (list 'package 'package-1 'package-2))))
 
+(test-equal "find-unordered-packages"
+  (list 7)
+  (begin
+    (call-with-output-file temp-file
+      (lambda (port)
+        (display "
+(define-public rust-addr2line-0.19
+  (package
+    (inherit rust-addr2line-0.21)
+    (name \"rust-addr2line\")
+    (version \"0.19.0\")))
+
+(define-public rust-addchain-0.2
+  (package
+    (name \"rust-addchain\")
+    (version \"0.2.0\")))
+" port)))
+    (find-unordered-packages temp-file)))
+
 (test-equal "string-distance"
   '(0 1 1 5 5)
   (list

base-commit: a1d711c92e119f6b5b8e99a620cdba92a4ca3bfb
-- 
2.41.0