-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip-plugin.py
More file actions
74 lines (63 loc) · 2.8 KB
/
zip-plugin.py
File metadata and controls
74 lines (63 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import zipfile
import time
import hashlib
def zip_plugin():
"""Zips the './polyfem' directory, only adding or updating files that have changed."""
def get_file_hash(filepath):
"""Compute the MD5 hash of a file for change detection."""
hasher = hashlib.md5()
with open(filepath, 'rb') as f:
# Read the file in chunks to handle large files
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
start_time = time.time()
zip_filename = 'polyfem.zip'
source_dir = './polyfem'
file_hashes = {}
hash_filename = 'file_hashes.txt'
# Load existing file hashes if the zip exists
if os.path.exists(zip_filename):
print(f"Zip file '{zip_filename}' exists. Checking for updates.")
zip_mode = 'a' # Open in append mode
if os.path.exists(hash_filename):
with open(hash_filename, 'r') as f:
for line in f:
filepath, filehash = line.strip().split(',')
file_hashes[filepath] = filehash
else:
print(f"Creating new zip file '{zip_filename}'.")
zip_mode = 'w' # Create new zip
# Open the zip file and process files
try:
with zipfile.ZipFile(zip_filename, zip_mode, zipfile.ZIP_DEFLATED) as zipf:
new_hashes = {}
for root, dirs, files in os.walk(source_dir):
for file in files:
filepath = os.path.join(root, file)
relative_path = os.path.relpath(filepath, start=source_dir)
# Compute the file's hash
current_hash = get_file_hash(filepath)
new_hashes[relative_path] = current_hash
# Check if the file is new or has been modified
if relative_path not in file_hashes or file_hashes[relative_path] != current_hash:
print(f"Adding/Updating: {relative_path}")
zipf.write(filepath, arcname=relative_path)
else:
print(f"Skipping (unchanged): {relative_path}")
# Update the hash file with the latest file hashes
with open(hash_filename, 'w') as f:
for filepath, filehash in new_hashes.items():
f.write(f"{filepath},{filehash}\n")
total_time = time.time() - start_time
zip_size = os.path.getsize(zip_filename)
print(f"Zip file processed in {total_time:.2f} seconds.")
print(f"Size of the zip file: {zip_size} bytes.")
except Exception as e:
print(f"An error occurred during zipping: {e}")
if os.path.exists(zip_filename):
os.remove(zip_filename)
exit(1)
if __name__ == "__main__":
zip_plugin()