Add option --match-filename

This commit is contained in:
Peter J. Holzer 2022-01-13 13:57:10 +01:00
parent 8954588ab4
commit a4d54b8ca1
1 changed files with 12 additions and 6 deletions

18
kitsune
View File

@ -1,6 +1,8 @@
#!/usr/bin/python3 #!/usr/bin/python3
import argparse
import datetime import datetime
import fnmatch
import os import os
import sys import sys
import stat import stat
@ -23,7 +25,7 @@ def watch(args):
watched_dirs = [] watched_dirs = []
watched_files = {} watched_files = {}
filename_length = 0 filename_length = 0
for a in args: for a in args.files:
st = os.stat(a) st = os.stat(a)
if stat.S_ISDIR(st.st_mode): if stat.S_ISDIR(st.st_mode):
watched_dirs.append(a) watched_dirs.append(a)
@ -43,10 +45,11 @@ def watch(args):
last_ts = st.st_mtime_ns last_ts = st.st_mtime_ns
for de in os.scandir(d): for de in os.scandir(d):
if de.is_file(): if de.is_file():
if de.path not in watched_files: if args.match_filename is None or fnmatch.fnmatch(de.name, args.match_filename):
watched_files[de.path] = WatchedFile(de.path) if de.path not in watched_files:
if len(de.path) > filename_length: watched_files[de.path] = WatchedFile(de.path)
filename_length = len(de.path) if len(de.path) > filename_length:
filename_length = len(de.path)
# has any of the files changed # has any of the files changed
for f in watched_files.values(): for f in watched_files.values():
@ -72,6 +75,9 @@ def watch(args):
time.sleep(0.1) time.sleep(0.1)
if __name__ == "__main__": if __name__ == "__main__":
args = sys.argv[1:] if len(sys.argv) > 1 else ["."] ap = argparse.ArgumentParser()
ap.add_argument("--match-filename")
ap.add_argument("files", nargs="*", default=["."])
args = ap.parse_args()
watch(args) watch(args)