From bc1dcd303f5c2ef95fd65f6e797c7b99607ca281 Mon Sep 17 00:00:00 2001 From: Duncan Calvert Date: Sat, 23 Nov 2024 13:06:37 -0600 Subject: [PATCH] Convert to bash. --- .tools/list-lfs-by-size.py | 37 ------------------------------------- .tools/list_lfs_by_size.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 37 deletions(-) delete mode 100644 .tools/list-lfs-by-size.py create mode 100755 .tools/list_lfs_by_size.sh diff --git a/.tools/list-lfs-by-size.py b/.tools/list-lfs-by-size.py deleted file mode 100644 index aea40a2..0000000 --- a/.tools/list-lfs-by-size.py +++ /dev/null @@ -1,37 +0,0 @@ -import re -import subprocess - -# Get the output of the git lfs ls-files -s command -def get_lfs_files(): - result = subprocess.run(['git', 'lfs', 'ls-files', '-s'], capture_output=True, text=True) - return result.stdout.strip().split('\n') - -# Convert human-readable sizes to bytes -def size_to_bytes(size_str): - size, unit = size_str.split() - size = float(size) - if unit == 'KB': - return int(size * 1024) - elif unit == 'MB': - return int(size * 1024 * 1024) - elif unit == 'GB': - return int(size * 1024 * 1024 * 1024) - return int(size) - -# Parse the LFS output and sort by size -lfs_files = get_lfs_files() -file_info = [] -for line in lfs_files: - match = re.search(r'(.*) \((.*?)\)$', line) - if match: - file_path = match.group(1) - size_str = match.group(2) - size_bytes = size_to_bytes(size_str) - file_info.append((size_bytes, file_path, size_str)) - -# Sort the files by size (largest first) -file_info.sort(key=lambda x: x[0], reverse=True) - -# Print the sorted list -for size_bytes, file_path, size_str in file_info: - print(f"{file_path} ({size_str})") diff --git a/.tools/list_lfs_by_size.sh b/.tools/list_lfs_by_size.sh new file mode 100755 index 0000000..fe13899 --- /dev/null +++ b/.tools/list_lfs_by_size.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Function to convert human-readable sizes to bytes +size_to_bytes() { + local size=$(echo $1 | cut -d' ' -f1) + local unit=$(echo $1 | cut -d' ' -f2) + size=${size%.*} # Remove decimal part + case $unit in + KB) echo $((size * 1024)) ;; + MB) echo $((size * 1024 * 1024)) ;; + GB) echo $((size * 1024 * 1024 * 1024)) ;; + *) echo $size ;; + esac +} + +# Get LFS files, convert sizes to bytes, sort, and print +git lfs ls-files -s | +while IFS= read -r line; do + if [[ $line =~ (.*)\((.*)\)$ ]]; then + file_path="${BASH_REMATCH[1]}" + size_str="${BASH_REMATCH[2]}" + size_bytes=$(size_to_bytes "$size_str") + printf "%020d|%s|%s\n" "$size_bytes" "$file_path" "$size_str" + fi +done | +sort -r | +while IFS='|' read -r size_bytes file_path size_str; do + echo "${file_path}(${size_str})" +done