-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_hash.py
More file actions
39 lines (30 loc) · 905 Bytes
/
generate_hash.py
File metadata and controls
39 lines (30 loc) · 905 Bytes
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
import json
import requests
import hashlib
imageList = []
def loadImageList():
global imageList
with open('vm-iso.json', 'r') as f:
imageList = json.load(f)
def saveImageList():
global imageList
with open('vm-iso.json', 'w') as f:
json.dump(imageList, f, indent=4)
loadImageList()
for image in imageList:
response = requests.head(image['url'])
if response.status_code != 200:
print(f"{image['id']} is not available")
continue
if image['hash'] != '':
print(f"{image['id']} hash already exists")
continue
# download image to temp file
temp_file = f"/tmp/{image['id']}.iso"
with open(temp_file, 'wb') as f:
f.write(response.content)
# generate hash
hash = hashlib.sha256(open(temp_file, 'rb').read()).hexdigest()
# compute once save once
image['hash'] = hash
saveImageList()