changeset 791:1ffb4e15151d

ports.subr: Determine the existence of a local index file (from INDEXDIR or PORTSDIR). The algorithm follows the behaviour of pkg-version(8) with regard to SOURCE_VERSION.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 29 Oct 2024 22:44:49 +0100
parents 34fe2260251d
children 954d3607e87d
files share/local-bsdtools/ports.subr tests/ports.t
diffstat 2 files changed, 116 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/ports.subr	Tue Oct 29 21:00:28 2024 +0100
+++ b/share/local-bsdtools/ports.subr	Tue Oct 29 22:44:49 2024 +0100
@@ -133,6 +133,7 @@
 #:   line.
 #:
 get_configured_pkg_repository_names() {
+    # no args
 
     local _line _status _repository_name _repository_enabled _key _value _rest
 
@@ -231,3 +232,90 @@
 EOF_repos_d6177ceb-b027-4fe2-bc71-e6b5017c0663
     falist_debug "${_allrepos}"
 }
+
+
+#:
+#: Check whether a local index is available.
+#:
+#: The check follows largely the conventions of :manpage:`pkg-version(8)`
+#: where to check for a local index.
+#:
+#: Returns:
+#:   int: 0 if the index is available, 1 otherwise
+#:
+is_local_index_file_available() {
+    # no args
+
+    local versrc indexdir indexfile
+
+    versrc="$(LC_ALL=C.UTF-8 "${PKG}" config VERSION_SOURCE)" || fatal "${EX_UNAVAILABLE}" "cannot get VERSION_SOURCE configuration value"
+    indexfile="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXFILE)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXFILE configuration value"
+    case "${versrc}" in
+        ''|R)
+            # XXX FIXME: handle R ("remote")  really like default?
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXDIR configuration value"
+            [ -r "${indexdir}/${indexfile}" ] && return 0
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config PORTSDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get PORTSDIR configuration value"
+            [ -r "${indexdir}/${indexfile}" ]
+            ;;
+        I)
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXDIR configuration value"
+            [ -r "${indexdir}/${indexfile}" ]
+            ;;
+        P)
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config PORTSDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get PORTSDIR configuration value"
+            [ -r "${indexdir}/${indexfile}" ]
+            ;;
+        *)
+            fatal "${EX_SOFTWARE}" "unhandled value in VERSION_SOURCE: ${versrc}";;
+    esac
+}
+
+
+#:
+#: Like `is_local_index_file_available` but also print the found index file.
+#:
+#: Output (stdout):
+#:   The found index file
+#:
+get_local_index_file() {
+    # no args
+
+    local versrc indexdir indexfile
+
+    versrc="$(LC_ALL=C.UTF-8 "${PKG}" config VERSION_SOURCE)" || fatal "${EX_UNAVAILABLE}" "cannot get VERSION_SOURCE configuration value"
+    indexfile="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXFILE)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXFILE configuration value"
+    case "${versrc}" in
+        ''|R)
+            # XXX FIXME: handle R ("remote")  really like default?
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXDIR configuration value"
+            if [ -r "${indexdir}/${indexfile}" ]; then
+                printf '%s' "${indexdir}/${indexfile}"
+                return 0
+            fi
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config PORTSDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get PORTSDIR configuration value"
+            if [ -r "${indexdir}/${indexfile}" ]; then
+                printf '%s' "${indexdir}/${indexfile}"
+                return 0
+            fi
+            ;;
+        I)
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config INDEXDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get INDEXDIR configuration value"
+            if [ -r "${indexdir}/${indexfile}" ]; then
+                printf '%s' "${indexdir}/${indexfile}"
+                return 0
+            fi
+            ;;
+        P)
+            indexdir="$(LC_ALL=C.UTF-8 "${PKG}" config PORTSDIR)" || fatal "${EX_UNAVAILABLE}" "cannot get PORTSDIR configuration value"
+            if [ -r "${indexdir}/${indexfile}" ]; then
+                printf '%s' "${indexdir}/${indexfile}"
+                return 0
+            fi
+            ;;
+        *)
+            fatal "${EX_SOFTWARE}" "unhandled value in VERSION_SOURCE: ${versrc}"
+            ;;
+    esac
+    return 1
+}
--- a/tests/ports.t	Tue Oct 29 21:00:28 2024 +0100
+++ b/tests/ports.t	Tue Oct 29 22:44:49 2024 +0100
@@ -59,6 +59,34 @@
   [1]
 
 
+Local Index File
+================
+
+Assumes fag's standard: PORTSDIR=/home/fag/ports
+
+  $ is_local_index_file_available
+  $ PORTSDIR=/nonexisting is_local_index_file_available
+  [1]
+  $ export VERSION_SOURCE=I
+  $ is_local_index_file_available
+  [1]
+  $ export VERSION_SOURCE=P
+  $ is_local_index_file_available
+  $ unset VERSION_SOURCE
+
+  $ get_local_index_file
+  /home/fag/ports/INDEX-[0-9]+ \(no-eol\) (re)
+  $ PORTSDIR=/nonexisting get_local_index_file
+  [1]
+  $ export VERSION_SOURCE=I
+  $ get_local_index_file
+  [1]
+  $ export VERSION_SOURCE=P
+  $ get_local_index_file
+  /home/fag/ports/INDEX-[0-9]+ \(no-eol\) (re)
+  $ unset VERSION_SOURCE
+
+
 Repositories
 ============