ollie/tools/plan_finish

48 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# description: Close a plan (rename __wip -> __done); fails if any tasks remain unchecked
# Args: <plan-filename>
# Output: new filename
import sys, os, re
if len(sys.argv) < 2:
print("usage: plan_finish <plan-filename>")
sys.exit(1)
import shutil
def _find_beads():
d = os.getcwd()
while d != os.path.dirname(d):
if os.path.isdir(os.path.join(d, '.beads')):
return True
d = os.path.dirname(d)
return False
if os.environ.get('OLLIE_BEADS_ENABLE', '1') != '0' and shutil.which('bd') and _find_beads():
print("error: beads is available — use 'bd' for planning instead of plan_finish", file=sys.stderr)
sys.exit(2)
ollie = os.environ.get('OLLIE', os.path.expanduser('~/mnt/ollie'))
plan_dir = os.path.join(ollie, 'pl')
filepath = os.path.join(plan_dir, os.path.basename(sys.argv[1]))
if not os.path.exists(filepath):
print(f"error: plan not found: {os.path.basename(filepath)}")
sys.exit(1)
if '__wip' not in os.path.basename(filepath):
print(f"error: plan is not tagged __wip: {os.path.basename(filepath)}")
sys.exit(1)
with open(filepath) as f:
lines = f.readlines()
remaining = [line[6:].strip() for line in lines if re.match(r'^- \[ \] ', line)]
if remaining:
print("error: all tasks must be completed before finishing the plan. remaining:")
for task in remaining:
print(f" - [ ] {task}")
sys.exit(1)
newpath = filepath.replace('__wip.md', '__done.md')
os.rename(filepath, newpath)
print(os.path.basename(newpath))