diff --git a/guile/sort-buckets.scm b/guile/sort-buckets.scm new file mode 100644 index 0000000..622e2fc --- /dev/null +++ b/guile/sort-buckets.scm @@ -0,0 +1,48 @@ +#!/usr/bin/guile \ +--no-auto-compile -e main -s + +Sort Buckets: script to sort a CSV of bucket-name,size in descending order of +bucket size. + +Copyright (C) 2025 Ray Miller . + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +!# + +(use-modules (ice-9 match) + (srfi srfi-1) + (dsv)) + +(define (parse-size s) + (match-let (((val unit) (string-split s #\space))) + (let ((val (string->number val))) + (match unit + ("Bytes" val) + ("KiB" (* 1024 val)) + ("MiB" (* 1024 1024 val)) + ("GiB" (* 1024 1024 1024 val)) + ("TiB" (* 1024 1024 1024 1024 val)))))) + +(define (sort-csv in-file out-file) + (let* ((data (call-with-input-file in-file (lambda (port) (dsv->scm port #:format 'rfc4180)))) + (sorted (map cdr (sort-list + (map (lambda (xs) (cons (parse-size (last xs)) xs)) data) + (lambda (a b) (> (first a) (first b))))))) + (call-with-output-file out-file (lambda (port) (scm->dsv port #:format 'rfc4180))))) + +(define (main args) + (case (length args) + ((3) (sort-csv (second args) (third args))) + ((2) (sort-csv (second args) (second args))) + (else (error "Usage: sort-buckets FILENAME [OUTPUT]"))))