Skip to content

Instantly share code, notes, and snippets.

@karlicoss
Created April 21, 2020 19:29
Show Gist options
  • Save karlicoss/ca5e5915e0c19bd90956da2d373cff60 to your computer and use it in GitHub Desktop.
Save karlicoss/ca5e5915e0c19bd90956da2d373cff60 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import logging
import os
import sys
from pathlib import Path
PATHS = [] # type: ignore
STATE_PATH = "/tmp/state.json"
def ignore(path: str):
return False
logger = logging.getLogger('org-paranoid')
def lines(f: str) -> int:
with open(f, 'r') as fo:
return len(fo.readlines())
def load_state():
import json
if not os.path.lexists(STATE_PATH):
return {}
with open(STATE_PATH, 'r') as fo:
return json.load(fo)
def save_state(state):
import json
Path(STATE_PATH).write_text(json.dumps(state))
def check_states(old, new) -> bool:
common = set(old.keys()).intersection(set(new.keys()))
ok = True
for k in common:
if ignore(k):
continue
before = old[k]
after = new[k]
if before > 10 and after / before < 0.3:
logger.error(f"File: {k}, before {before}, currently {after}")
ok = False
return ok
prev_state = load_state()
cur_state = {}
paths = PATHS
for p in paths:
for root, dirs, files in os.walk(p):
for fname in files:
if not fname.endswith('.org'):
continue
ff = os.path.join(root, fname)
wcl = None
try:
wcl = lines(ff)
cur_state[ff] = wcl
except FileNotFoundError:
# whatever, got removed right under our feet
pass
ok = check_states(prev_state, cur_state)
save_state(cur_state)
if not ok:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment