Skip to content

Instantly share code, notes, and snippets.

@tbnorth
Last active March 3, 2023 15:13
Show Gist options
  • Save tbnorth/e1711f8a9e7f1e42b70a3498a76134ff to your computer and use it in GitHub Desktop.
Save tbnorth/e1711f8a9e7f1e42b70a3498a76134ff to your computer and use it in GitHub Desktop.
pywait.py - execute arguments whenever a file changes
"""
pywait.py - execute arguments whenever a file changes
Terry N. Brown, terrynbrown@gmail.com, Mon Dec 12 10:47:49 2016
"""
import os
import sys
import time
def get_state():
"""return set of os.stat() for all files"""
state = set()
for path, dirs, files in os.walk('.'):
for file_ in files:
state.add((file_, os.stat(os.path.join(path, file_))))
return state
def main():
new_state = None
cmd = ' '.join(sys.argv[1:])
while True:
if new_state is not None:
print('\n'.join(i[0] for i in new_state.difference(state)))
if cmd:
os.system(cmd)
print("RUN: %s" % time.asctime())
new_state = state = get_state()
while new_state == state:
time.sleep(5)
new_state = get_state()
if not cmd:
exit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment