5
5
import os
6
6
import sys
7
7
8
- url = sys .argv [1 ]
9
- checksum = sys .argv [2 ]
10
8
11
- if checksum .startswith ("sha256:" ):
12
- h = hashlib .sha256
13
- checksum = checksum [7 :]
14
- else :
15
- raise Exception ("Unknown hash function" )
16
-
17
-
18
- def verify ():
9
+ def verify (archive : str , h , checksum ):
19
10
with open (archive , "rb" ) as f :
20
11
actual_checksum = h (f .read ()).hexdigest ()
21
12
result = actual_checksum == checksum
@@ -25,25 +16,38 @@ def verify():
25
16
print ("Checksum verification failed" )
26
17
print ("Expected" , checksum )
27
18
print ("But got:" , actual_checksum )
28
-
29
19
return result
30
20
31
21
32
- archive = url .split ("/" )[- 1 ]
33
- if not os .path .exists ("fsbuild/_sources" ):
34
- os .makedirs ("fsbuild/_sources" )
35
- archive = os .path .join ("fsbuild/_sources" , archive )
22
+ def main ():
23
+ url = sys .argv [1 ]
24
+ checksum = sys .argv [2 ]
25
+
26
+ if checksum .startswith ("sha256:" ):
27
+ h = hashlib .sha256
28
+ checksum = checksum [7 :]
29
+ else :
30
+ raise Exception ("Unknown hash function" )
31
+
32
+ archive = url .split ("/" )[- 1 ]
33
+ if not os .path .exists ("fsbuild/_sources" ):
34
+ os .makedirs ("fsbuild/_sources" )
35
+ archive = os .path .join ("fsbuild/_sources" , archive )
36
+
37
+ if os .path .exists (archive ):
38
+ if verify (archive , h , checksum ):
39
+ sys .exit (0 )
40
+ print ("Removing archive" , archive )
41
+ os .remove (archive )
42
+
43
+ # FIXME: Replace use of wget, just use python instead
44
+ if os .system (f'cd fsbuild/_sources && wget "{ url } "' ) != 0 :
45
+ print ("Failed to download" )
46
+ sys .exit (1 )
36
47
37
- if os .path .exists (archive ):
38
- if verify ():
39
- sys .exit (0 )
40
- print ("Removing archive" , archive )
41
- os .remove (archive )
48
+ if not verify (archive , h , checksum ):
49
+ sys .exit (2 )
42
50
43
- # FIXME: Replace use of wget, just use python instead
44
- if os .system (f'cd fsbuild/_sources && wget "{ url } "' ) != 0 :
45
- print ("Failed to download" )
46
- sys .exit (1 )
47
51
48
- if not verify () :
49
- sys . exit ( 2 )
52
+ if __name__ == "__main__" :
53
+ main ( )
0 commit comments