From fb20b1436da56f1e2922d6866f2c696274ca813e Mon Sep 17 00:00:00 2001 From: Olivier Cervello Date: Thu, 2 Sep 2021 11:56:14 +0200 Subject: [PATCH 1/3] Add user-defined exclude paths --- whenchanged/whenchanged.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/whenchanged/whenchanged.py b/whenchanged/whenchanged.py index 1de6aa0..dc07115 100755 --- a/whenchanged/whenchanged.py +++ b/whenchanged/whenchanged.py @@ -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 @@ -53,9 +53,9 @@ class WhenChanged(FileSystemEventHandler): r'\.git/?', # __pycache__ directories r'__pycache__/?', - ])) + ] - def __init__(self, files, command, recursive=False, run_once=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 = {} @@ -70,7 +70,8 @@ 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: @@ -185,6 +186,7 @@ def main(): files = [] command = [] + excludes = [] recursive = False verbose_mode = 0 run_once = False @@ -208,6 +210,8 @@ def main(): elif flag == '-c': command = args args = [] + elif flag == '-x': + excludes.append(args[0][1]) elif flag == '-q': quiet_mode = True else: @@ -237,7 +241,7 @@ def main(): if verbose_mode: print("When '%s' changes, run '%s'" % (files[0], print_command)) - wc = WhenChanged(files, command, recursive, run_once, run_at_start, + wc = WhenChanged(files, excludes, command, recursive, run_once, run_at_start, verbose_mode, quiet_mode) try: From 080a6e208b353b2f4432858937674d9a58c60780 Mon Sep 17 00:00:00 2001 From: Olivier Cervello Date: Thu, 2 Sep 2021 12:02:12 +0200 Subject: [PATCH 2/3] Fix typo --- whenchanged/whenchanged.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/whenchanged/whenchanged.py b/whenchanged/whenchanged.py index dc07115..378d32d 100755 --- a/whenchanged/whenchanged.py +++ b/whenchanged/whenchanged.py @@ -71,7 +71,8 @@ def __init__(self, files, excludes, command, recursive=False, run_once=False, 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.exclude = re.compile(r'|'.join( + r'(.+/)?' + a for a in self.excludes)) self.observer = Observer(timeout=0.1) for p in self.paths: From 71b8f7b1a314d120fda791a7ac96bcd7b592aaec Mon Sep 17 00:00:00 2001 From: Olivier Cervello Date: Thu, 2 Sep 2021 12:22:54 +0200 Subject: [PATCH 3/3] Fix exclusions --- whenchanged/whenchanged.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/whenchanged/whenchanged.py b/whenchanged/whenchanged.py index 378d32d..758f771 100755 --- a/whenchanged/whenchanged.py +++ b/whenchanged/whenchanged.py @@ -55,8 +55,15 @@ class WhenChanged(FileSystemEventHandler): r'__pycache__/?', ] - def __init__(self, files, excludes, 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: @@ -84,10 +91,10 @@ def __init__(self, files, excludes, 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: @@ -95,16 +102,21 @@ def run_command(self, 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): @@ -211,8 +223,6 @@ def main(): elif flag == '-c': command = args args = [] - elif flag == '-x': - excludes.append(args[0][1]) elif flag == '-q': quiet_mode = True else: @@ -241,9 +251,10 @@ def main(): else: if verbose_mode: print("When '%s' changes, run '%s'" % (files[0], print_command)) - - wc = WhenChanged(files, excludes, 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()