def _zopfli(filepath): while True: original_ts = os.stat(filepath).st_mtime original_size = os.stat(filepath).st_size t0 = time.time() # XXX What happens if you run two threads # 1. thread1 starts `zopfli_file(path)` # 2. thread2 deletes `path` whilst thread1 is running new_filepath = zopfli_file(filepath) t1 = time.time() if new_filepath: print( "Generated {} ({} bytes, originally {} bytes) Took {:.2f}s".format( new_filepath, format(os.stat(new_filepath).st_size, ","), format(original_size, ","), t1 - t0, ) ) if original_ts != os.stat(filepath).st_mtime: print( "WARNING! The file {} changed during the " "zopfli process.".format(filepath) ) continue break
def _zopfli_html(html, filepath, url): while True: original_ts = os.stat(filepath).st_mtime original_size = os.stat(filepath).st_size t0 = time.time() new_filepath = zopfli_file(filepath) t1 = time.time() if new_filepath: new_size = os.stat(new_filepath).st_size if not new_size: print( "WARNING! {} became 0 bytes after zopfli".format(filepath)) os.remove(new_filepath) continue if new_size > original_size: print( "WARNING! {} became larger after zopfli".format(filepath)) # XXX delete it? print("Generated {} ({} bytes, originally {} bytes) Took {:.2f}s". format( new_filepath, format(new_size, ","), format(original_size, ","), t1 - t0, )) if original_ts != os.stat(filepath).st_mtime: print("WARNING! The file {} changed during the " "zopfli process.".format(filepath)) continue break
def _zopfli_html(html, filepath, url): for _ in range(5): try: original_ts = os.stat(filepath).st_mtime original_size = os.stat(filepath).st_size except FileNotFoundError: # Try again in a second. time.sleep(1) continue t0 = time.time() new_filepath = zopfli_file(filepath) t1 = time.time() if new_filepath: try: new_size = os.stat(new_filepath).st_size except FileNotFoundError: # Race conditions probably print("WARNING! {} is now gone".format(filepath)) continue if not new_size: print("WARNING! {} became 0 bytes after zopfli".format(filepath)) os.remove(new_filepath) continue if new_size > original_size: print("WARNING! {} became larger after zopfli".format(filepath)) # XXX delete it? print( "Generated {} ({} bytes, originally {} bytes) Took {:.2f}s".format( new_filepath, format(new_size, ","), format(original_size, ","), t1 - t0, ) ) if original_ts != os.stat(filepath).st_mtime: print( "WARNING! The file {} changed during the " "zopfli process.".format(filepath) ) continue break
def _zopfli_html(html, filepath, url): for _ in range(5): try: original_ts = os.stat(filepath).st_mtime original_size = os.stat(filepath).st_size except FileNotFoundError: # Try again in a second. time.sleep(1) continue t0 = time.time() new_filepath = zopfli_file(filepath) t1 = time.time() if new_filepath: try: new_size = os.stat(new_filepath).st_size except FileNotFoundError: # Race conditions probably print("WARNING! {} is now gone".format(filepath)) continue if not new_size: print( "WARNING! {} became 0 bytes after zopfli".format(filepath)) os.remove(new_filepath) continue if new_size > original_size: print( "WARNING! {} became larger after zopfli".format(filepath)) # XXX delete it? print("Generated {} ({} bytes, originally {} bytes) Took {:.2f}s". format( new_filepath, format(new_size, ","), format(original_size, ","), t1 - t0, )) if original_ts != os.stat(filepath).st_mtime: print("WARNING! The file {} changed during the " "zopfli process.".format(filepath)) continue break