Subject: [PATCH v2 12/23] DRAFT import: juliahub: Add functions to parse the git repo for a git tag.

From: Simon Tournier <[email-masked]>
Date: Mon, 18 Sep 2023 20:03:19 +0200

From: Nicolas Graves via Guix-patches via <[email-masked]>

Signed-off-by: Simon Tournier <[email-masked]>
---
 guix/import/juliahub.scm | 62 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 56 insertions(+), 6 deletions(-)

diff --git a/guix/import/juliahub.scm b/guix/import/juliahub.scm
index fc25ba1d4220..5327e923251d 100644
--- a/guix/import/juliahub.scm
+++ b/guix/import/juliahub.scm
@@ -26,6 +26,7 @@ (define-module (guix import juliahub)
   #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-11)
   #:use-module (guix http-client)
+  #:use-module (guix git)
   #:use-module (guix import utils)
   #:use-module (guix import json)
   #:use-module (guix base32)
@@ -38,6 +39,48 @@ (define-module (guix import juliahub)
             %juliahub-updater
             juliahub-recursive-import))
 
+;; Juliahub may be more up-to-date than the General registry or the actual git
+;; tag (it seems around 6 hours pass between the time a commit is supplied to
+;; JuliaRegistrator as a release, and the time Julia TagBot Github Action makes
+;; the git tag). We have no simple way to get the commit of the latest-version.
+;; Thus the best simple thing we can do is get the latest-git-version, and
+;; import this version instead. We do this by parsing Package.toml in the General
+;; registry, and then getting the refs of the git repo supplied by this
+;; file. Parsing this file is also necessary if the package is in a subdir of a
+;; git repository, because the information isn't present in Juliahub.
+;; There's a last case where some Julia packages are not based on a particular
+;; git tag, and where looking for the commit is tedious and artificial. In these
+;; cases, we introduce the tree-commit which is available in the Versions.toml
+;; file in the General repository. This is equivalent to a commit, since we have
+;; a unique hash of the listing of files and directories, thus it can be used to
+;; identify the state of a repository.
+
+(define %general-base-url
+  "https://raw.githubusercontent.com/JuliaRegistries/General/master/")
+
+(define (general-url package-name file)
+  (let ((folder (string-capitalize (string-take package-name 1))))
+    (string-append
+     %general-base-url folder "/" package-name "/" file)))
+
+(define (ini-list->alist lst)
+  (match lst
+    ((attribute '= value ooo ...)
+     `((,attribute . ,value) ,@(ini-list->alist ooo)))
+    ('()
+     '())))
+
+(define (ini-fetch url)
+  (let* ((port (http-fetch url #:text? #t))
+         (ini-list (stream->list (port->stream port read))))
+    (close-port port)
+    (ini-list->alist ini-list)))
+
+(define (latest-git-tag repo)
+  (let* ((last-ref (last (remote-refs repo #:tags? #t)))
+         (last-git-tag (last (string-split last-ref #\/))))
+    (string-drop last-git-tag 1)))
+
 ;; To update, see file sysimg.jl
 (define %julia-stdlibs
   (list "julia"
@@ -194,14 +237,21 @@ (define (make-julia-sexp name source home-page synopsis description
                  ((license) (license->symbol license))
                  (_ `(list ,@(map license->symbol licenses)))))))
 
+;; TODO handle subdir case properly.
+
 (define* (juliahub->guix-package package-name
                                  #:key version #:allow-other-keys)
   "Fetch the metadata for PACKAGE-NAME from juliahub.org, and return the
 `package' s-expression corresponding to that package, or #f on failure.
 Optionally include a VERSION string to fetch a specific version juliahub."
-  (let ((package (if version
-                     (juliahub-fetch package-name version)
-                     (juliahub-fetch package-name))))
+  (let* ((package-toml (ini-fetch (general-url name "Package.toml")))
+         (subdir (assoc-ref package-toml 'subdir))
+         (tag (latest-git-tag (assoc-ref package-toml 'repo)))
+         (package (if version
+                      (juliahub-fetch package-name version)
+                      (if tag
+                          (juliahub-fetch package-name tag)
+                          (juliahub-fetch package-name)))))
     (if package
         (let-values (((source directory)
                       (git->origin+dir
@@ -233,13 +283,13 @@ (define* (juliahub->guix-package package-name
 
 (define (guix-package->juliahub-name package)
   (let* ((url (juliahub-package-url package))
-         (git-name (car (last-pair (string-split url #\/))))
+         (git-name (last (string-split url #\/)))
          (ungitted-name (if (string-suffix? ".git" git-name)
                             (string-drop-right git-name 4)
                             git-name))
          (package-name (if (string-suffix? ".jl" ungitted-name)
-                            (string-drop-right ungitted-name 4)
-                            ungitted-name)))
+                           (string-drop-right ungitted-name 4)
+                           ungitted-name)))
     package-name))
 
 (define* (import-release package #:key (version #f))
-- 
2.38.1