tailhead
tailhead copied to clipboard
follow_path() on windows just loops over the file
Hi
I'm finding that follow_path() on windows just loops over the same file, over and over without waiting at the end of file.
for line in tailhead.follow_path(filepath):
if line is not None:
print(line)
else:
time.sleep(5)
on a file with lines 1 2 3 .. 11 gives line 1, 2, 3, .., 11, 1, 2, ... over and over.
Thanks
Looks like os.stat(...).st_ino doesn't work on windows on python 2.7. Always returns 0. So everything breaks.
@vasakt Interesting. What solution would you propose in that case? We could resort to some WinAPI call here through ctypes.
Not sure of the best solution but I believe that you could use the win32file package. Something like:
def getReadHandle(filename):
if os.path.isdir(filename):
dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS
else:
dwFlagsAndAttributes = 0
return win32file.CreateFile(
filename,
win32file.GENERIC_READ,
win32file.FILE_SHARE_READ,
None,
win32file.OPEN_EXISTING,
dwFlagsAndAttributes,
None
)
def getUniqueID(hFile):
(
attributes,
created_at, accessed_at, written_at,
volume,
file_hi, file_lo,
n_links,
index_hi, index_lo
) = win32file.GetFileInformationByHandle(hFile)
return volume, index_hi, index_lo
def filesIdentical(filename1, filename2):
hFile1 = getReadHandle(filename1)
hFile2 = getReadHandle(filename2)
areIdentical= getUniqueID(hFile1) == getUniqueID(hFile2)
hFile2.Close()
hFile1.Close()
return areIdentical
might work.
@vasakt I want to find out what was modified in CPython to get it to work. Probably I will be able to just port this approach.
I think on windows you could just compare the creation times of the files to check if the file has been recreated.
Replacing os.stat(file_path).st_ino != os.fstat(self.following_file.fileno()).st_ino
with os.stat(file_path).st_ctime != os.fstat(self.following_file.fileno()).st_ctime
( https://github.com/GreatFruitOmsk/tailhead/blob/master/tailhead/init.py#L442 ) on windows should work.
Within a certain interval. Probably good enough as a first approach to the problem.