Skip to content

Commit

Permalink
Add -1 option. When specified, don't rerun if files are modified
Browse files Browse the repository at this point in the history
while the command is running.
  • Loading branch information
mathieu longtin committed Feb 25, 2015
1 parent 23c5606 commit 5a605c6
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions whenchanged/whenchanged.py
@@ -1,11 +1,12 @@
#!/usr/bin/env python
"""%(prog)s - run a command when a file is changed
Usage: %(prog)s [-v] [-r] FILE COMMAND...
%(prog)s [-v] [-r] FILE [FILE ...] -c COMMAND
Usage: %(prog)s [-v] [-r] [-1] FILE COMMAND...
%(prog)s [-v] [-r] [-1] FILE [FILE ...] -c COMMAND
FILE can be a directory. Watch recursively with -r.
Verbose output with -v.
-1 (run once) to not rerun if changes occured while the command was running.
Use %%f to pass the filename to the command.
Copyright (c) 2011, Johannes H. Jensen.
Expand All @@ -17,8 +18,7 @@
import sys
import os
import re

# External modules.
import time
import pyinotify
try:
import subprocess32 as subprocess
Expand All @@ -31,22 +31,28 @@ class WhenChanged(pyinotify.ProcessEvent):
# Exclude Vim swap files, its file creation test file 4913 and backup files
exclude = re.compile(r'^\..*\.sw[px]*$|^4913$|.~$')

def __init__(self, files, command, recursive=False):
def __init__(self, files, command, recursive=False, run_once=False):
self.files = files
paths = {}
for f in files:
paths[os.path.realpath(f)] = f
self.paths = paths
self.command = command
self.recursive = recursive
self.run_once = run_once
self.last_run = 0

def run_command(self, thefile):
if self.run_once:
if os.path.exists(thefile) and os.path.getmtime(thefile) < self.last_run:
return
new_command = []
for item in self.command:
if item == "%f":
item = thefile
new_command.append(item)
subprocess.call(new_command, shell=True)
self.last_run = time.time()

def is_interested(self, path):
basename = os.path.basename(path)
Expand Down Expand Up @@ -119,14 +125,20 @@ def main():
command = []
recursive = False
verbose = False
run_once = False

flags = []
while args[0][0] == '-' and args[0] != '-c':
flags.append(args.pop(0))

if '-v' in args:
if '-v' in flags:
verbose = True
args.remove('-v')

if '-r' in args:
if '-r' in flags:
recursive = True
args.remove('-r')

if '-1' in flags:
run_once = True

if '-c' in args:
cpos = args.index('-c')
Expand All @@ -152,7 +164,7 @@ def main():
if verbose:
print("When '%s' changes, run '%s'" % (files[0], print_command))

wc = WhenChanged(files, command, recursive)
wc = WhenChanged(files, command, recursive, run_once)
try:
wc.run()
except KeyboardInterrupt:
Expand Down

0 comments on commit 5a605c6

Please sign in to comment.