apenwarr-redo/redo/cycles.py
Avery Pennarun f1305b49eb Move env.{add,get}_lock() into cycles.py, and rename.
They really aren't locks at all, they're a cycle detector.  Also rename
REDO_LOCKS to a more meaningful REDO_CYCLES.  And we'll move the
CyclicDependencyError exception in here as well, instead of state.py
where it doesn't really belong.
2018-12-05 02:26:58 -05:00

24 lines
553 B
Python

"""Code for detecting and aborting on cyclic dependency loops."""
import os
class CyclicDependencyError(Exception):
pass
def _get():
"""Get the list of held cycle items."""
return os.environ.get('REDO_CYCLES', '').split(':')
def add(fid):
"""Add a lock to the list of held cycle items."""
items = set(_get())
items.add(str(fid))
os.environ['REDO_CYCLES'] = ':'.join(list(items))
def check(fid):
if str(fid) in _get():
# Lock already held by parent: cyclic dependency
raise CyclicDependencyError()