-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile_cache.py
27 lines (23 loc) · 956 Bytes
/
file_cache.py
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
import os
import shutil
import hashlib
def folder_filename_hash(dir):
filenames = sorted(os.listdir(dir))
folder_hash = hashlib.new('sha256')
for filename in filenames:
hash_obj = hashlib.new('sha256')
hash_obj.update(filename.encode('utf-8'))
folder_hash.update(hash_obj.hexdigest().encode('utf-8'))
return folder_hash.hexdigest()
def update_cache(cache_directory, runtime_xpi_directory, cache_hash_filename):
if not cache_directory or not runtime_xpi_directory or cache_directory == runtime_xpi_directory:
return
try:
shutil.rmtree(cache_directory)
shutil.move(runtime_xpi_directory, cache_directory)
folder_hash = folder_filename_hash(cache_directory)
with open(os.path.join(cache_directory, cache_hash_filename), 'w') as file:
file.write(folder_hash)
print(folder_hash)
except Exception as e:
print(f'update cache failed: {e}')