Created
December 18, 2019 16:08
-
-
Save peter-mcconnell/197b26fcb08c985f0a2001f234793cf2 to your computer and use it in GitHub Desktop.
autoreload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import subprocess | |
import time | |
# How often we check the filesystem for changes (in seconds) | |
wait = 1 | |
command = ' '.join(sys.argv[1:]) | |
path = '.' | |
process = subprocess.Popen(command, shell=True) | |
last_mtime = max(file_times(path)) | |
def file_filter(name): | |
return (not name.startswith(".")) and (not name.endswith(".swp")) | |
def file_times(path): | |
for top_level in filter(file_filter, os.listdir(path)): | |
for root, dirs, files in os.walk(top_level): | |
for file in filter(file_filter, files): | |
yield os.stat(os.path.join(root, file)).st_mtime | |
def print_stdout(process): | |
stdout = process.stdout | |
if stdout != None: | |
print(stdout) | |
while True: | |
max_mtime = max(file_times(path)) | |
print_stdout(process) | |
if max_mtime > last_mtime: | |
last_mtime = max_mtime | |
print('Restarting process.') | |
process.kill() | |
process = subprocess.Popen(command, shell=True) | |
time.sleep(wait) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment