Last active
February 2, 2024 22:04
-
-
Save nvgordeev/fd6be0405abdec2f642bac2ddede553e to your computer and use it in GitHub Desktop.
Watch files and create zip archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import zipfile | |
import time | |
class FileStat: | |
def __init__(self, filename, mod_time): | |
self.name = filename | |
self.mod_time = mod_time | |
def get_mod_time(self): | |
stat = os.stat(self.name) | |
return stat.st_mtime | |
def is_updated(self): | |
new_mod_time = self.get_mod_time() | |
if self.mod_time != new_mod_time: | |
self.mod_time = new_mod_time | |
return True | |
return False | |
def make_archive(files, zipname): | |
if os.path.exists(zipname): | |
os.remove(zipname) | |
z = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) | |
for file in files: | |
z.write(file, os.path.basename(file)) | |
z.close() | |
def watch(files, handler, timeout=3): | |
filestats = [FileStat(filename, 0) for filename in files] | |
while True: | |
modified = any([filestat.is_updated() for filestat in filestats]) | |
if modified: | |
handler(files) | |
print("{0}: update".format(time.asctime())) | |
time.sleep(timeout) | |
if __name__ == '__main__': | |
PATH = 'C:\\Users\\nv-go\\algs4\\projects\\ColinearPoints' | |
OUTPUT_PATH = 'C:\\Users\\nv-go\\algs4\\projects\\ColinearPoints' | |
FILES = ['BruteCollinearPoints.java', 'FastCollinearPoints.java', 'Point.java'] | |
ZIPNAME = 'collinear.zip' | |
watch( | |
files = [os.path.join(PATH, filename) for filename in FILES], | |
handler = lambda files: make_archive(files, os.path.join(OUTPUT_PATH, ZIPNAME)), | |
timeout=3 | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment