Python Forum
Watch files and automatically run a script in Linux - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Watch files and automatically run a script in Linux (/thread-7674.html)



Watch files and automatically run a script in Linux - Gribouillis - Jan-20-2018

The following script (download it here) named autopython3 runs a python script every time it is changed on disk. It also supports watching other files and running the script when one of these files is changed.

The script is based on the AWESOME module doit (according to doit's documentation, this script should work only in LINUX and MAC OSX). Ubuntu users can install it with
sudo apt install python3-doit
In order to run the below script, use a command line such as
autopython3 myprog.py
autopython3 -w foo.txt bar.png baz.pdf -- myprog.py
Then the script myprog.py is executed every time one of the files mentioned on the command line is changed on disk. Wink I typically use it while developing myprog.py or one of the watched files, in order to have immediate feedback from python every time I type Ctrl-s in my editor to save the file I'm working on.

#!/usr/bin/env python3
# -*-coding: utf8-*-
'''doc
'''
__version__ = '0.1.0'

from argparse import ArgumentParser
import sys

def task_AutoExecution():
    """my doc"""
    yield {
        'basename': 'AutoExecution',
        'actions': ['PYTHONIOENCODING="utf8" {python} {script}'.format(python=sys.executable, script=SCRIPT)],
        'watch': [PROG] + WATCH,
        'verbosity': 2,
        }
 
if __name__ == '__main__':
    parser = ArgumentParser(description='Automatically runs a python script every time it is changed on disk')
    parser.add_argument('script', metavar='SCRIPT', help='python script to execute', action='store')
    parser.add_argument('-w', '--watch', metavar='FILE', help='additional file to watch (end arglist with --)', nargs='*')
    args = parser.parse_args()
    SCRIPT = args.script
    PROG = SCRIPT.strip().split()[0]
    WATCH = args.watch or []
    sys.argv[1:] = ['auto',]
    import doit
    doit.run(globals())



RE: Watch files and automatically run a script in Linux - nilamo - Jan-23-2018

Quote:
    import doit
    doit.run(globals())

Your link leads to a private gist/repo, so we can't actually see the doit.py file :p


RE: Watch files and automatically run a script in Linux - Gribouillis - Jan-23-2018

(Jan-23-2018, 05:13 PM)nilamo Wrote: Your link leads to a private gist/repo, so we can't actually see the doit.py file :p
The module doit is on pypi, it's not mine! You can install it with pip3 or perhaps with your OS' software manager. Can you see and download my script in the gist? (it is the same as the one I posted here).


RE: Watch files and automatically run a script in Linux - nilamo - Jan-23-2018

Oh lol. Yes, I see what you shared in the gist.


RE: Watch files and automatically run a script in Linux - Gribouillis - Aug-14-2023

2023 UPDATE: This old script still works but modern versions of 'doit' don't accept the parameter 'auto' on the command line. The solution is to install also 'doit-auto1', so prior to using this script, type
Output:
python -m pip install doit python -m pip install doit-auto1