Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusions #87

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions whenchanged/whenchanged.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

class WhenChanged(FileSystemEventHandler):
# files to exclude from being watched
exclude = re.compile(r'|'.join(r'(.+/)?'+ a for a in [
excludes = [
# Vim swap files
r'\..*\.sw[px]*$',
# file creation test file 4913
Expand All @@ -53,10 +53,17 @@ class WhenChanged(FileSystemEventHandler):
r'\.git/?',
# __pycache__ directories
r'__pycache__/?',
]))

def __init__(self, files, command, recursive=False, run_once=False,
run_at_start=False, verbose_mode=0, quiet_mode=False):
]

def __init__(self,
files,
excludes,
command,
recursive=False,
run_once=False,
run_at_start=False,
verbose_mode=0,
quiet_mode=False):
self.files = files
paths = {}
for f in files:
Expand All @@ -70,7 +77,9 @@ def __init__(self, files, command, recursive=False, run_once=False,
self.verbose_mode = verbose_mode
self.quiet_mode = quiet_mode
self.process_env = os.environ.copy()

self.excludes.extend(excludes)
self.exclude = re.compile(r'|'.join(
r'(.+/)?' + a for a in self.excludes))
self.observer = Observer(timeout=0.1)

for p in self.paths:
Expand All @@ -82,27 +91,32 @@ def __init__(self, files, command, recursive=False, run_once=False,
p = os.path.dirname(p)
self.observer.schedule(self, p)


def run_command(self, thefile):
if self.run_once:
if os.path.exists(thefile) and os.path.getmtime(thefile) < self.last_run:
if os.path.exists(
thefile) and os.path.getmtime(thefile) < self.last_run:
return
new_command = []
for item in self.command:
new_command.append(item.replace('%f', thefile))
now = datetime.now()
print_message = ''
if self.verbose_mode > 0:
print_message = "'" + thefile + "' " + re.sub(r'^[^_]+_', '', self.get_envvar('event'))
print_message = "'" + thefile + "' " + re.sub(
r'^[^_]+_', '', self.get_envvar('event'))
if self.verbose_mode > 1:
print_message += ' at ' + now.strftime('%F %T')
if self.verbose_mode > 2:
print_message += '.' + now.strftime('%f') + ", running '" + ' '.join(self.command) + "'"
print_message += '.' + now.strftime(
'%f') + ", running '" + ' '.join(self.command) + "'"
if print_message:
print('==> ' + print_message + ' <==')
self.set_envvar('file', thefile)
stdout = open(os.devnull, 'wb') if self.quiet_mode else None
subprocess.call(new_command, shell=(len(new_command) == 1), env=self.process_env, stdout=stdout)
subprocess.call(new_command,
shell=(len(new_command) == 1),
env=self.process_env,
stdout=stdout)
self.last_run = time.time()

def is_interested(self, path):
Expand Down Expand Up @@ -185,6 +199,7 @@ def main():

files = []
command = []
excludes = []
recursive = False
verbose_mode = 0
run_once = False
Expand Down Expand Up @@ -236,9 +251,10 @@ def main():
else:
if verbose_mode:
print("When '%s' changes, run '%s'" % (files[0], print_command))

wc = WhenChanged(files, command, recursive, run_once, run_at_start,
verbose_mode, quiet_mode)
excludes = [f.lstrip('-') for f in files if f.startswith('-')]
files = [f for f in files if not f.startswith('-')]
wc = WhenChanged(files, excludes, command, recursive, run_once,
run_at_start, verbose_mode, quiet_mode)

try:
wc.run()
Expand Down