Fix spacestated script deduplication
This commit is contained in:
parent
3db5780651
commit
c7065ed27f
1 changed files with 26 additions and 17 deletions
43
spacestated
43
spacestated
|
@ -58,6 +58,11 @@ def await_state_change(prev_state):
|
||||||
print(err)
|
print(err)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
|
def script_hooks(name, active):
|
||||||
|
hook_subdir = path.join(HOOK_DIR, '%s_%s.d' % (name, 'open' if active else 'closed'))
|
||||||
|
return [ path.join(hook_subdir, f) for f in os.listdir(hook_subdir) ]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
prev_state = get_state()
|
prev_state = get_state()
|
||||||
while True:
|
while True:
|
||||||
|
@ -66,33 +71,37 @@ if __name__ == '__main__':
|
||||||
print('state: %s' % ', '.join([ '%s=%s' % (name, 'open' if op else 'closed') for name, op in state.items() ]))
|
print('state: %s' % ', '.join([ '%s=%s' % (name, 'open' if op else 'closed') for name, op in state.items() ]))
|
||||||
try:
|
try:
|
||||||
scripts = []
|
scripts = []
|
||||||
scripts_inverse = []
|
|
||||||
|
|
||||||
# Build a list of scripts that should be run.
|
|
||||||
for name, active in state.items():
|
for name, active in state.items():
|
||||||
|
scripts_past = []
|
||||||
|
scripts_future = []
|
||||||
|
for scissor_name, scissor_active in state.items():
|
||||||
|
if scissor_active and name != scissor_name:
|
||||||
|
scripts_past.extend(script_hooks(scissor_name, True))
|
||||||
|
scripts_future.extend(script_hooks(scissor_name, False))
|
||||||
|
hashes_past = { hash_file(f): f for f in scripts_past }
|
||||||
|
hashes_future = { hash_file(f): f for f in scripts_future }
|
||||||
|
|
||||||
if active != prev_state[name]:
|
if active != prev_state[name]:
|
||||||
# Add the scripts that should run.
|
hashes = { hash_file(f): f for f in script_hooks(name, active) }
|
||||||
hook_dir = path.join(HOOK_DIR, '%s_%s.d' % (name, 'open' if active else 'closed'))
|
# If the state transitions to open, the scripts that were
|
||||||
scripts.extend([ path.join(hook_dir, f) for f in os.listdir(hook_dir) ])
|
# ran in the past should not run.
|
||||||
|
# If the state transitions to closed, the scripts that will
|
||||||
|
# run in the future should not run.
|
||||||
|
scissor = hashes_past if active else hashes_future
|
||||||
|
scripts.append((hashes, scissor))
|
||||||
|
|
||||||
# Add the scripts that should run if the state is the
|
run = []
|
||||||
# opposite. This will help us to determine whether we will
|
for scriptset in scripts:
|
||||||
# be running scripts in needless succession.
|
(hashes, scissor) = scriptset
|
||||||
inv_dir = path.join(HOOK_DIR, '%s_%s.d' % (name, 'open' if not active else 'closed'))
|
run.extend([ hashes[h] for h in set(hashes.keys()) - set(scissor.keys()) ])
|
||||||
scripts_inverse.extend([ path.join(inv_dir, f) for f in os.listdir(inv_dir) ])
|
|
||||||
|
|
||||||
# Alias scripts to their contents though a hash of each of their contents.
|
|
||||||
hashes = { hash_file(f): f for f in scripts }
|
|
||||||
hashes_inverse = { hash_file(f): f for f in scripts_inverse }
|
|
||||||
|
|
||||||
run = [ hashes[h] for h in set(hashes.keys()) - set(hashes_inverse.keys()) ]
|
|
||||||
run.sort()
|
run.sort()
|
||||||
|
|
||||||
for script in run:
|
for script in run:
|
||||||
try:
|
try:
|
||||||
if os.access(script, os.X_OK):
|
if os.access(script, os.X_OK):
|
||||||
out = subprocess.check_output([ script ])[:-1].decode('utf8')
|
out = subprocess.check_output([ script ])[:-1].decode('utf8')
|
||||||
print(out)
|
print('-> %s: %s' % (script, out))
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue