Script to sort list of S3 buckets by size.

This commit is contained in:
Ray Miller 2025-02-12 11:19:59 +00:00
parent c7f8b16a51
commit b3e4d6e422

48
guile/sort-buckets.scm Normal file
View file

@ -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 <ray@1729.org.uk>.
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 <http://www.gnu.org/licenses/>.
!#
(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]"))))