Compare commits

...

5 Commits

1 changed files with 66 additions and 32 deletions

98
kitsune
View File

@ -12,14 +12,19 @@ class WatchedFile:
def __init__(self, path, seek_to_end): def __init__(self, path, seek_to_end):
self.path = path self.path = path
self.seek_to_end = seek_to_end self.seek_to_end = seek_to_end
self.reopen() stf = os.stat(self.path)
self.st_ctime_ns = stf.st_ctime_ns
self.st_size = stf.st_size
self.fileno = None
if stf.st_ctime_ns >= time.time_ns() - 1E9:
self.reopen()
def reopen(self): def reopen(self):
self.fd = open(self.path, errors='replace') self.fd = open(self.path, errors='replace')
self.fileno = self.fd.fileno() self.fileno = self.fd.fileno()
self.last_ts = 0 self.last_ts = 0
if self.seek_to_end: if self.seek_to_end:
self.fd.seek(0, 2) self.fd.seek(self.st_size, 0)
self.seek_to_end = False self.seek_to_end = False
def format_ts(ts_ns): def format_ts(ts_ns):
@ -49,40 +54,63 @@ def watch(args):
if st.st_mtime_ns > last_ts: if st.st_mtime_ns > last_ts:
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(): try:
if args.match_filename is None or fnmatch.fnmatch(de.name, args.match_filename): 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, seek_to_end) if de.path not in watched_files:
if len(de.path) > filename_length: watched_files[de.path] = WatchedFile(de.path, seek_to_end)
filename_length = len(de.path) if len(de.path) > filename_length:
filename_length = len(de.path)
except (PermissionError, OSError):
# ignore,
# or maybe remove from watched_files?
# or just mark as unavailable?
pass
# has any of the files changed # has any of the files changed
for f in watched_files.values(): for f in watched_files.values():
# XXX - we should also detect replaced files. # XXX - we should also detect replaced files.
st = os.stat(f.fileno) if f.fileno:
try: st = os.stat(f.fileno)
stf = os.stat(f.path) try:
if stf.st_ino != st.st_ino: stf = os.stat(f.path)
f.reopen() if stf.st_ino != st.st_ino:
st = os.stat(f.fileno) f.reopen()
except FileNotFoundError: st = os.stat(f.fileno)
# ignore, except (FileNotFoundError, PermissionError):
# or maybe remove from watched_files? # ignore,
# or just mark as deleted? # or maybe remove from watched_files?
pass # or just mark as deleted?
if st.st_mtime_ns > f.last_ts: pass
if st.st_size < f.fd.tell(): if st.st_mtime_ns > f.last_ts:
# We are beyond the end of the file, so it has probably if st.st_size < f.fd.tell():
# been truncated and rewritten - read from beginning # We are beyond the end of the file, so it has probably
f.fd.seek(0, 0) # been truncated and rewritten - read from beginning
dead = "✝" if st.st_nlink == 0 else " " f.fd.seek(0, 0)
f.last_ts = st.st_mtime_ns dead = "✝" if st.st_nlink == 0 else " "
new_content = f.fd.read() f.last_ts = st.st_mtime_ns
lines = new_content.split("\n") new_content = f.fd.read()
if lines[-1] == "": lines = new_content.split("\n")
lines.pop() if lines[-1] == "":
for ln in lines: lines.pop()
print(f"{f.path:{filename_length}}", format_ts(f.last_ts), dead, ln) for ln in lines:
s = ""
if args.print_filename:
s += f"{f.path:{filename_length}} "
if args.print_timestamp:
s += format_ts(f.last_ts) + " "
s += dead + " " + ln
print(s)
else:
try:
stf = os.stat(f.path)
if stf.st_ctime_ns != f.st_ctime_ns:
f.reopen()
except (FileNotFoundError, PermissionError):
# ignore,
# or maybe remove from watched_files?
# or just mark as deleted?
pass
time.sleep(0.1) time.sleep(0.1)
seek_to_end = False seek_to_end = False
@ -93,6 +121,12 @@ if __name__ == "__main__":
ap.add_argument("--match-filename", ap.add_argument("--match-filename",
help="follow only matching files in directories", help="follow only matching files in directories",
metavar="GLOB-PATTERN") metavar="GLOB-PATTERN")
ap.add_argument("--no-filename", "--no-print-filename", action="store_false",
dest="print_filename",
help="don't print filename in each line")
ap.add_argument("--no-timestamp", "--no-print-timestamp", action="store_false",
dest="print_timestamp",
help="don't print timestamp in each line")
ap.add_argument("files", nargs="*", default=["."], ap.add_argument("files", nargs="*", default=["."],
metavar="file") metavar="file")
args = ap.parse_args() args = ap.parse_args()