dotfiles/home/private_bin/executable_mkscratch

64 lines
1.4 KiB
Text
Raw Normal View History

2025-02-13 11:37:17 -06:00
#!/usr/bin/env python3
from pathlib import Path
import sys
from datetime import datetime
from tempfile import mkdtemp
SCRATCH_DIR = Path.home() / "scratch"
WEEK_SCRATCH = SCRATCH_DIR / datetime.now().strftime("%Y/%U")
2025-02-17 13:50:45 -06:00
2025-02-13 11:37:17 -06:00
(SCRATCH_DIR / "current").unlink()
(SCRATCH_DIR / "current").symlink_to(WEEK_SCRATCH)
2025-02-17 13:50:45 -06:00
def scratch_maintenance():
if not WEEK_SCRATCH.is_dir():
WEEK_SCRATCH.mkdir(exist_ok=True)
2025-02-13 11:37:17 -06:00
for p in WEEK_SCRATCH.iterdir():
if p.is_dir() and len(list(p.iterdir())) == 0:
sys.stderr.write(f"removing: {p}\n")
p.rmdir()
def make_temp() -> Path:
return Path(mkdtemp(prefix=datetime.now().strftime("%d") + ".", dir=WEEK_SCRATCH))
def make_named(name: str) -> Path:
d = WEEK_SCRATCH / name
d.mkdir(exist_ok=True)
return d
2025-02-17 13:50:45 -06:00
2025-02-13 11:37:17 -06:00
def return_last() -> Path:
2025-02-17 13:50:45 -06:00
dirs = [d for d in WEEK_SCRATCH.iterdir() if d.is_dir()]
2025-02-13 11:37:17 -06:00
if len(dirs) == 0:
sys.stderr.write("no directores. exiting...")
2025-02-17 13:50:45 -06:00
return sorted(dirs, key=lambda p: p.stat().st_mtime, reverse=True)[0]
2025-02-13 11:37:17 -06:00
def main():
2025-02-17 13:50:45 -06:00
scratch_maintenance()
2025-02-13 11:37:17 -06:00
if len(sys.argv) > 2:
sys.exit("unexpected number of arguments")
elif len(sys.argv) > 1:
name = sys.argv[1]
2025-02-17 15:29:18 -06:00
if name == ["last", "l"]:
2025-02-13 11:37:17 -06:00
d = return_last()
2025-02-17 15:29:18 -06:00
elif name in ["current", "c"]:
d = WEEK_SCRATCH
2025-02-13 11:37:17 -06:00
else:
d = make_named(name)
else:
d = make_temp()
print(d)
2025-02-17 13:50:45 -06:00
2025-02-13 11:37:17 -06:00
if __name__ == "__main__":
main()