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.
This commit is contained in:
Avery Pennarun 2018-12-05 00:18:07 -05:00
commit f1305b49eb
5 changed files with 32 additions and 25 deletions

24
redo/cycles.py Normal file
View file

@ -0,0 +1,24 @@
"""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()