3
3
# with an existing JSON file containing previous checksums. If the version
4
4
# already exists in the existing JSON file, an exception is raised.
5
5
#
6
- # Usage: python generate_checksum.py <checksums.txt > <old_checksums.json>
6
+ # Usage: python generate_checksum.py <checksums_download_url > <old_checksums.json>
7
7
#
8
8
# checksums.txt format:
9
9
# 2ef0560c3c88908a22d1f302e5b0119160e72380e25fb58c2d7b153e9397a04c notation_1.0.0-rc.1_linux_arm64.tar.gz
17
17
import os
18
18
import sys
19
19
import json
20
+ import requests
21
+
22
+ def download_file (url , dest_path ):
23
+ response = requests .get (url )
24
+ if response .status_code == 200 :
25
+ with open (dest_path , 'wb' ) as f :
26
+ f .write (response .content )
27
+ return True
28
+ else :
29
+ return False
20
30
21
31
def build_url (name , version , filename ):
22
32
return {
23
- "notation" : lambda : f'https://github.com/notaryproject/notation/releases/download/v{ version } /{ filename } ' ,
24
- "notation-azure-kv" : lambda : f'https://github.com/Azure/notation-azure-kv/releases/download/v{ version } /{ filename } '
33
+ "notation" : lambda : f'https://github.com/notaryproject/notation/releases/download/v{ version } /{ filename } ' ,
34
+ "notation-azure-kv" : lambda : f'https://github.com/Azure/notation-azure-kv/releases/download/v{ version } /{ filename } '
25
35
}[name ]()
26
36
27
- def process_checksum (filepath ):
37
+ def process_checksum (checksum_text ):
28
38
verionInfo = {}
29
- with open (filepath , 'r' ) as f :
30
- for line in f .readlines ():
31
- line = line .rstrip ('\n ' )
32
- parts = line .split (' ' )
33
- checksum = parts [0 ]
34
- filename = parts [2 ]
35
- name_parts = filename .split ('_' )
36
- name = name_parts [0 ]
37
- version = name_parts [1 ]
38
- osName = name_parts [2 ]
39
- arch = name_parts [3 ].split ('.' )[0 ]
39
+ for line in checksum_text .splitlines ():
40
+ line = line .rstrip ('\n ' )
41
+ parts = line .split (' ' )
42
+ checksum = parts [0 ]
43
+ filename = parts [2 ]
44
+ name_parts = filename .split ('_' )
45
+ name = name_parts [0 ]
46
+ version = name_parts [1 ]
47
+ osName = name_parts [2 ]
48
+ arch = name_parts [3 ].split ('.' )[0 ]
40
49
41
- # generate checksum
42
- verionInfo .setdefault ('version' , version )
43
- verionInfo .setdefault (osName , {})
44
- verionInfo [osName ].setdefault (arch , {})
45
- verionInfo [osName ][arch ] = {
46
- "url" : build_url (name , version , filename ),
47
- "checksum" : checksum
48
- }
50
+ verionInfo .setdefault ('version' , version )
51
+ verionInfo .setdefault (osName , {})
52
+ verionInfo [osName ].setdefault (arch , {})
53
+ verionInfo [osName ][arch ] = {
54
+ "url" : build_url (name , version , filename ),
55
+ "checksum" : checksum
56
+ }
49
57
50
58
return verionInfo
51
59
52
60
def update_checksums (filepath , checksums ):
53
- # read old checksums
54
- versionList = []
55
61
if os .path .exists (filepath ):
56
- f = open (filepath , 'r' )
57
- versionList = json .load (f )
58
- f .close ()
62
+ with open (filepath , 'r' ) as f :
63
+ versionList = json .load (f )
64
+ else :
65
+ versionList = []
59
66
60
- # check if version exists
61
67
for versionInfo in versionList :
62
68
if versionInfo ['version' ] == checksums ['version' ]:
63
69
raise Exception (f'Version { checksums ["version" ]} already exists in { filepath } ' )
64
70
65
- # update checksums
66
71
with open (filepath , 'w' ) as f :
67
72
json .dump ([checksums ] + versionList , f , indent = 4 , sort_keys = True )
68
73
69
74
def main ():
70
75
if len (sys .argv ) < 3 :
71
- print ('Usage: python generate_checksum.py <checksums.txt > <old_checksums.json>' )
76
+ print ('Usage: python generate_checksum.py <checksums_download_url > <old_checksums.json>' )
72
77
sys .exit (1 )
73
78
74
- filepath = sys .argv [1 ]
79
+ download_url = sys .argv [1 ]
75
80
old_checksums = sys .argv [2 ]
76
81
77
- checksums = process_checksum (filepath )
78
- update_checksums (old_checksums , checksums )
82
+ response = requests .get (download_url )
83
+ if response .status_code == 200 :
84
+ checksum_text = response .text
85
+ checksums = process_checksum (checksum_text )
86
+ update_checksums (old_checksums , checksums )
87
+ print ('Checksums updated successfully.' )
88
+ else :
89
+ print ('Failed to download checksum file.' )
79
90
80
91
if __name__ == '__main__' :
81
- main ()
92
+ main ()
0 commit comments