Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:09:20

0001 #!/usr/bin/env python3
0002 
0003 import argparse
0004 import os
0005 import sys
0006 import time
0007 from collections import defaultdict
0008 from pathlib import Path
0009 
0010 
0011 def main():
0012     parser = argparse.ArgumentParser(
0013         description="Displays a text-based histogram of Snakemake cache entry size distribution in hourly bins",
0014     )
0015     parser.add_argument(
0016         "--cache-dir",
0017         "--snakemake-output-cache-dir",
0018         type=str,
0019         default=os.environ.get("SNAKEMAKE_OUTPUT_CACHE"),
0020         help="Path to Snakemake cache directory (defaults to $SNAKEMAKE_OUTPUT_CACHE)",
0021     )
0022     args = parser.parse_args()
0023 
0024     if not args.cache_dir or not os.path.isdir(args.cache_dir):
0025         return 0
0026 
0027     cache_dir = Path(args.cache_dir)
0028     try:
0029         entries = list(cache_dir.iterdir())
0030     except OSError:
0031         return 0
0032 
0033     now = time.time()
0034     bins = defaultdict(int)
0035 
0036     for p in entries:
0037         try:
0038             if not p.is_file():
0039                 continue
0040             st = p.stat()
0041         except OSError:
0042             continue
0043         age_h = int(max(0, now - st.st_mtime) // 3600)
0044         bins[age_h] += st.st_size
0045 
0046     if not bins:
0047         print(f"Snakemake cache ({cache_dir}) is empty.")
0048         return 0
0049 
0050     max_h = max(bins)
0051     max_sz = max(bins.values())
0052     total_sz = sum(bins.values())
0053     print(f"Snakemake cache hourly size distribution ({total_sz / (1024**3):.1f} GiB total in {cache_dir}):")
0054 
0055     hours_to_print = range(max_h + 1) if max_h <= 72 else sorted(bins.keys())
0056     for h in hours_to_print:
0057         sz = bins[h]
0058         bar = "#" * int(40 * sz / max_sz) if max_sz else ""
0059         print(f"{h:3d}h: {sz / (1024**3):6.2f} GiB | {bar}".rstrip())
0060 
0061     return 0
0062 
0063 
0064 if __name__ == "__main__":
0065     sys.exit(main())