tailhead icon indicating copy to clipboard operation
tailhead copied to clipboard

follow_path() on windows just loops over the file

Open vasakt opened this issue 8 years ago • 6 comments

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

vasakt avatar Sep 13 '16 01:09 vasakt

Looks like os.stat(...).st_ino doesn't work on windows on python 2.7. Always returns 0. So everything breaks.

vasakt avatar Sep 13 '16 01:09 vasakt

@vasakt Interesting. What solution would you propose in that case? We could resort to some WinAPI call here through ctypes.

Kentzo avatar Sep 13 '16 18:09 Kentzo

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 avatar Sep 14 '16 08:09 vasakt

@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.

Kentzo avatar Sep 14 '16 18:09 Kentzo

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.

pankajp avatar Mar 27 '17 12:03 pankajp

Within a certain interval. Probably good enough as a first approach to the problem.

Kentzo avatar Mar 27 '17 19:03 Kentzo