Skip to main content

Add two useful scripts

ID
4a1a488
date
2022-08-01 13:40:34+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
3e461dc
message
Add two useful scripts
changed files
2 files, 59 additions

Changed files

greedy_tar_gz (0) → greedy_tar_gz (1400)

diff --git a/greedy_tar_gz b/greedy_tar_gz
new file mode 100755
index 0000000..ac7273c
--- /dev/null
+++ b/greedy_tar_gz
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+"""
+This is a "greedy" script to compress a directory into a tar.gz.
+
+I use it when I'm in a space-constrained environment (e.g. AWS CloudShell)
+where I have enough space to hold the uncompressed files OR the compressed
+archive, but not both.  It compresses the directory and deletes the
+original files as it goes.
+
+Use with caution; I only use it when I can easily recreate the original.
+
+"""
+
+import os
+import sys
+import tarfile
+
+import tqdm
+
+
+def get_file_paths_under(root=".", *, suffix=""):
+    """Generates the paths to every file under ``root``."""
+    if not os.path.isdir(root):
+        raise ValueError(f"Cannot find files under non-existent directory: {root!r}")
+
+    for dirpath, _, filenames in os.walk(root):
+        for f in filenames:
+            if os.path.isfile(os.path.join(dirpath, f)) and f.lower().endswith(suffix):
+                yield os.path.join(dirpath, f)
+
+
+if __name__ == '__main__':
+    try:
+        dirname = sys.argv[1]
+    except IndexError:
+        sys.exit(f"Usage: {__file__} <DIRNAME>")
+
+    if not os.path.isdir(dirname):
+        sys.exit(f"Usage: {__file__} <DIRNAME>")
+
+    with tarfile.open(os.path.basename(dirname.strip('/')) + '.tar.gz', 'w:gz') as tar:
+        for path in tqdm.tqdm(list(get_file_paths_under(dirname))):
+            tar.add(path, arcname=os.path.relpath(path, dirname), recursive=True)
+            os.unlink(path)
\ No newline at end of file

randline (0) → randline (254)

diff --git a/randline b/randline
new file mode 100755
index 0000000..fac50b1
--- /dev/null
+++ b/randline
@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+
+import random
+import sys
+
+
+if __name__ == "__main__":
+    lines = sys.stdin.read().splitlines()
+    try:
+        k = int(sys.argv[1])
+    except IndexError:
+        k = 1
+
+    random.shuffle(lines)
+    print("\n".join(lines[:k]))