mercurial/util.py
changeset 2471 6904e1ef8ad1
parent 2470 fe1689273f84
parent 2462 d610bcfd66a8
child 2480 519a1011db91
equal deleted inserted replaced
2470:fe1689273f84 2471:6904e1ef8ad1
   819                 self.iterempty = True
   819                 self.iterempty = True
   820             self.buf = collector.getvalue()
   820             self.buf = collector.getvalue()
   821         s, self.buf = self.buf[:l], buffer(self.buf, l)
   821         s, self.buf = self.buf[:l], buffer(self.buf, l)
   822         return s
   822         return s
   823 
   823 
   824 def filechunkiter(f, size = 65536):
   824 def filechunkiter(f, size=65536, limit=None):
   825     """Create a generator that produces all the data in the file size
   825     """Create a generator that produces the data in the file size
   826     (default 65536) bytes at a time.  Chunks may be less than size
   826     (default 65536) bytes at a time, up to optional limit (default is
   827     bytes if the chunk is the last chunk in the file, or the file is a
   827     to read all data).  Chunks may be less than size bytes if the
   828     socket or some other type of file that sometimes reads less data
   828     chunk is the last chunk in the file, or the file is a socket or
   829     than is requested."""
   829     some other type of file that sometimes reads less data than is
   830     s = f.read(size)
   830     requested."""
   831     while len(s) > 0:
   831     assert size >= 0
       
   832     assert limit is None or limit >= 0
       
   833     while True:
       
   834         if limit is None: nbytes = size
       
   835         else: nbytes = min(limit, size)
       
   836         s = nbytes and f.read(nbytes)
       
   837         if not s: break
       
   838         if limit: limit -= len(s)
   832         yield s
   839         yield s
   833         s = f.read(size)
       
   834 
   840 
   835 def makedate():
   841 def makedate():
   836     lt = time.localtime()
   842     lt = time.localtime()
   837     if lt[8] == 1 and time.daylight:
   843     if lt[8] == 1 and time.daylight:
   838         tz = time.altzone
   844         tz = time.altzone