1111import argparse
1212import contextlib
1313from fnmatch import fnmatch
14+ import hashlib
1415import os
1516from pathlib import Path
17+ import platform
1618import re
1719import shutil
1820import subprocess
1921import sys
20- import hashlib
22+ import time
23+ import urllib .request
24+ import zipfile
2125
2226SHA256_SUMS = {
2327 "0e2819135366f150d9906e294b61dff58fd1996ebd26c2f8e979d6c0b7a79580" : {"tag" : "v0.14.3" , "archive" : "bitcoin-0.14.3-aarch64-linux-gnu.tar.gz" },
2428 "d86fc90824a85c38b25c8488115178d5785dbc975f5ff674f9f5716bc8ad6e65" : {"tag" : "v0.14.3" , "archive" : "bitcoin-0.14.3-arm-linux-gnueabihf.tar.gz" },
2529 "1b0a7408c050e3d09a8be8e21e183ef7ee570385dc41216698cc3ab392a484e7" : {"tag" : "v0.14.3" , "archive" : "bitcoin-0.14.3-osx64.tar.gz" },
2630 "706e0472dbc933ed2757650d54cbcd780fd3829ebf8f609b32780c7eedebdbc9" : {"tag" : "v0.14.3" , "archive" : "bitcoin-0.14.3-x86_64-linux-gnu.tar.gz" },
31+ "5ea84b6a4c1b03d5f4d1a718fbed215e2fd0e66ca7d59ca6141218d163ce1301" : {"tag" : "v0.14.3" , "archive" : "bitcoin-0.14.3-win64.zip" },
2732
2833 "60c93e3462c303eb080be7cf623f1a7684b37fd47a018ad3848bc23e13c84e1c" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-aarch64-linux-gnu.tar.gz" },
2934 "55b577e0fb306fb429d4be6c9316607753e8543e5946b542d75d876a2f08654c" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-arm-linux-gnueabihf.tar.gz" },
3035 "b9024dde373ea7dad707363e07ec7e265383204127539ae0c234bff3a61da0d1" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-osx64.tar.gz" },
3136 "fa71cb52ee5e0459cbf5248cdec72df27995840c796f58b304607a1ed4c165af" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-riscv64-linux-gnu.tar.gz" },
3237 "376194f06596ecfa40331167c39bc70c355f960280bd2a645fdbf18f66527397" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-x86_64-linux-gnu.tar.gz" },
38+ "e59fba67afce011d32b5d723a3a0be12da1b8a34f5d7966e504520c48d64716d" : {"tag" : "v0.20.1" , "archive" : "bitcoin-0.20.1-win64.zip" },
3339
3440 "43416854330914992bbba2d0e9adf2a6fff4130be9af8ae2ef1186e743d9a3fe" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-aarch64-linux-gnu.tar.gz" },
3541 "f028af308eda45a3c4c90f9332f96b075bf21e3495c945ebce48597151808176" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-arm-linux-gnueabihf.tar.gz" },
3642 "695fb624fa6423f5da4f443b60763dd1d77488bfe5ef63760904a7b54e91298d" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-osx64.tar.gz" },
3743 "f8b2adfeae021a672effbc7bd40d5c48d6b94e53b2dd660f787340bf1a52e4e9" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-riscv64-linux-gnu.tar.gz" },
3844 "da7766775e3f9c98d7a9145429f2be8297c2672fe5b118fd3dc2411fb48e0032" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-x86_64-linux-gnu.tar.gz" },
45+ "1d0052c4ce80227fb6d0bc1c4e673ba21033e219c1f935d25f130ef7f43360d4" : {"tag" : "v0.21.0" , "archive" : "bitcoin-0.21.0-win64.zip" },
3946
4047 "ac718fed08570a81b3587587872ad85a25173afa5f9fbbd0c03ba4d1714cfa3e" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-aarch64-linux-gnu.tar.gz" },
4148 "b8713c6c5f03f5258b54e9f436e2ed6d85449aa24c2c9972f91963d413e86311" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-arm-linux-gnueabihf.tar.gz" },
4451 "91b1e012975c5a363b5b5fcc81b5b7495e86ff703ec8262d4b9afcfec633c30d" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-powerpc64le-linux-gnu.tar.gz" },
4552 "9cc3a62c469fe57e11485fdd32c916f10ce7a2899299855a2e479256ff49ff3c" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-riscv64-linux-gnu.tar.gz" },
4653 "59ebd25dd82a51638b7a6bb914586201e67db67b919b2a1ff08925a7936d1b16" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-x86_64-linux-gnu.tar.gz" },
54+ "9485e4b52ed6cebfe474ab4d7d0c1be6d0bb879ba7246a8239326b2230a77eb1" : {"tag" : "v22.0" , "archive" : "bitcoin-22.0-win64.zip" },
4755
4856 "06f4c78271a77752ba5990d60d81b1751507f77efda1e5981b4e92fd4d9969fb" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-aarch64-linux-gnu.tar.gz" },
4957 "952c574366aff76f6d6ad1c9ee45a361d64fa04155e973e926dfe7e26f9703a3" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-arm-linux-gnueabihf.tar.gz" },
5361 "078f96b1e92895009c798ab827fb3fde5f6719eee886bd0c0e93acab18ea4865" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-riscv64-linux-gnu.tar.gz" },
5462 "c816780583009a9dad426dc0c183c89be9da98906e1e2c7ebae91041c1aaaaf3" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-x86_64-apple-darwin.tar.gz" },
5563 "2cca490c1f2842884a3c5b0606f179f9f937177da4eadd628e3f7fd7e25d26d0" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-x86_64-linux-gnu.tar.gz" },
64+ "004b2e25b21e0f14cbcce6acec37f221447abbb3ea7931c689e508054bfc6cf6" : {"tag" : "v23.0" , "archive" : "bitcoin-23.0-win64.zip" },
5665
5766 "0b48b9e69b30037b41a1e6b78fb7cbcc48c7ad627908c99686e81f3802454609" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-aarch64-linux-gnu.tar.gz" },
5867 "37d7660f0277301744e96426bbb001d2206b8d4505385dfdeedf50c09aaaef60" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-arm-linux-gnueabihf.tar.gz" },
6271 "6b163cef7de4beb07b8cb3347095e0d76a584019b1891135cd1268a1f05b9d88" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-riscv64-linux-gnu.tar.gz" },
6372 "e2f751512f3c0f00eb68ba946d9c829e6cf99422a61e8f5e0a7c109c318674d0" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-x86_64-apple-darwin.tar.gz" },
6473 "49df6e444515d457ea0b885d66f521f2a26ca92ccf73d5296082e633544253bf" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-x86_64-linux-gnu.tar.gz" },
74+ "8784ce304f22c495392d3adfd7fc2c645d093db9bd4d42666c41adf540539fff" : {"tag" : "v24.0.1" , "archive" : "bitcoin-24.0.1-win64.zip" },
6575
6676 "3a7bdd959a0b426624f63f394f25e5b7769a5a2f96f8126dcc2ea53f3fa5212b" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-aarch64-linux-gnu.tar.gz" },
6777 "e537c8630b05e63242d979c3004f851fd73c2a10b5b4fdbb161788427c7b3c0f" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-arm-linux-gnueabihf.tar.gz" },
7181 "fe6e347a66043946920c72c9c4afca301968101e6b82fb90a63d7885ebcceb32" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-riscv64-linux-gnu.tar.gz" },
7282 "5708fc639cdfc27347cccfd50db9b73b53647b36fb5f3a4a93537cbe8828c27f" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-x86_64-apple-darwin.tar.gz" },
7383 "33930d432593e49d58a9bff4c30078823e9af5d98594d2935862788ce8a20aec" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-x86_64-linux-gnu.tar.gz" },
84+ "7154b35ecc8247589070ae739b7c73c4dee4794bea49eb18dc66faed65b819e7" : {"tag" : "v25.0" , "archive" : "bitcoin-25.0-win64.zip" },
7485
7586 "7fa582d99a25c354d23e371a5848bd9e6a79702870f9cbbf1292b86e647d0f4e" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-aarch64-linux-gnu.tar.gz" },
7687 "e004b7910bedd6dd18b6c52b4eef398d55971da666487a82cd48708d2879727e" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-arm-linux-gnueabihf.tar.gz" },
7990 "6ee1a520b638132a16725020146abea045db418ce91c02493f02f541cd53062a" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-riscv64-linux-gnu.tar.gz" },
8091 "77e931bbaaf47771a10c376230bf53223f5380864bad3568efc7f4d02e40a0f7" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-x86_64-apple-darwin.tar.gz" },
8192 "7fe294b02b25b51acb8e8e0a0eb5af6bbafa7cd0c5b0e5fcbb61263104a82fbc" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-x86_64-linux-gnu.tar.gz" },
93+ "85282f4ec1bcb0cfe8db0f195e8e0f6fb77cfbe89242a81fff2bc2e9292f7acf" : {"tag" : "v28.0" , "archive" : "bitcoin-28.0-win64.zip" },
8294}
8395
8496
@@ -92,31 +104,71 @@ def pushd(new_dir) -> None:
92104 os .chdir (previous_dir )
93105
94106
107+ def download_from_url (url , archive ):
108+ last_print_time = time .time ()
109+
110+ def progress_hook (progress_bytes , total_size ):
111+ nonlocal last_print_time
112+ now = time .time ()
113+ percent = min (100 , (progress_bytes * 100 ) / total_size )
114+ bar_length = 40
115+ filled_length = int (bar_length * percent / 100 )
116+ bar = '#' * filled_length + '-' * (bar_length - filled_length )
117+ if now - last_print_time >= 1 or percent >= 100 :
118+ print (f'\r Downloading: [{ bar } ] { percent :.1f} %' , flush = True , end = "" )
119+ last_print_time = now
120+
121+ with urllib .request .urlopen (url ) as response :
122+ if response .status != 200 :
123+ raise RuntimeError (f"HTTP request failed with status code: { response .status } " )
124+
125+ total_size = int (response .getheader ('Content-Length' , 0 ))
126+ progress_bytes = 0
127+
128+ with open (archive , 'wb' ) as file :
129+ while True :
130+ chunk = response .read (8192 )
131+ if not chunk :
132+ break
133+ file .write (chunk )
134+ progress_bytes += len (chunk )
135+ progress_hook (progress_bytes , total_size )
136+
137+ print ('\n ' , flush = True , end = "" ) # Flush to avoid error output on the same line.
138+
139+
95140def download_binary (tag , args ) -> int :
96141 if Path (tag ).is_dir ():
97142 if not args .remove_dir :
98143 print ('Using cached {}' .format (tag ))
99144 return 0
100145 shutil .rmtree (tag )
101- Path ( tag ). mkdir ()
146+
102147 bin_path = 'bin/bitcoin-core-{}' .format (tag [1 :])
148+
103149 match = re .compile ('v(.*)(rc[0-9]+)$' ).search (tag )
104150 if match :
105151 bin_path = 'bin/bitcoin-core-{}/test.{}' .format (
106152 match .group (1 ), match .group (2 ))
107- platform = args .platform
108- if tag < "v23" and platform in ["x86_64-apple-darwin" , "arm64-apple-darwin" ]:
109- platform = "osx64"
110- archive = 'bitcoin-{tag}-{platform}.tar.gz' .format (
111- tag = tag [1 :], platform = platform )
112- archiveUrl = 'https://bitcoincore.org/{bin_path}/{archive}' .format (
113- bin_path = bin_path , archive = archive )
114153
115- print ('Fetching: {archiveUrl}' .format (archiveUrl = archiveUrl ))
154+ host = args .host
155+ if tag < "v23" and host in ["x86_64-apple-darwin" , "arm64-apple-darwin" ]:
156+ host = "osx64"
116157
117- ret = subprocess .run (['curl' , '--fail' , '--remote-name' , archiveUrl ]).returncode
118- if ret :
119- return ret
158+ archive_format = 'tar.gz'
159+ if host == 'win64' :
160+ archive_format = 'zip'
161+
162+ archive = f'bitcoin-{ tag [1 :]} -{ host } .{ archive_format } '
163+ archive_url = f'https://bitcoincore.org/{ bin_path } /{ archive } '
164+
165+ print (f'Fetching: { archive_url } ' )
166+
167+ try :
168+ download_from_url (archive_url , archive )
169+ except Exception as e :
170+ print (f"\n Download failed: { e } " , file = sys .stderr )
171+ return 1
120172
121173 hasher = hashlib .sha256 ()
122174 with open (archive , "rb" ) as afile :
@@ -125,20 +177,37 @@ def download_binary(tag, args) -> int:
125177
126178 if archiveHash not in SHA256_SUMS or SHA256_SUMS [archiveHash ]['archive' ] != archive :
127179 if archive in [v ['archive' ] for v in SHA256_SUMS .values ()]:
128- print ("Checksum did not match" )
129- return 1
130-
131- print ("Checksum for given version doesn't exist" )
180+ print (f"Checksum { archiveHash } did not match" , file = sys .stderr )
181+ else :
182+ print ("Checksum for given version doesn't exist" , file = sys .stderr )
132183 return 1
184+
133185 print ("Checksum matched" )
186+ Path (tag ).mkdir ()
134187
135188 # Extract archive
136- ret = subprocess .run (['tar' , '-zxf' , archive , '-C' , tag ,
137- '--strip-components=1' ,
138- 'bitcoin-{tag}' .format (tag = tag [1 :])]).returncode
139- if ret != 0 :
140- print (f"Failed to extract the { tag } archive" )
141- return ret
189+ if host == 'win64' :
190+ try :
191+ with zipfile .ZipFile (archive , 'r' ) as zip :
192+ zip .extractall (tag )
193+ # Remove the top level directory to match tar's --strip-components=1
194+ extracted_items = os .listdir (tag )
195+ top_level_dir = os .path .join (tag , extracted_items [0 ])
196+ # Move all files & subdirectories up one level
197+ for item in os .listdir (top_level_dir ):
198+ shutil .move (os .path .join (top_level_dir , item ), tag )
199+ # Remove the now-empty top-level directory
200+ os .rmdir (top_level_dir )
201+ except Exception as e :
202+ print (f"Zip extraction failed: { e } " , file = sys .stderr )
203+ return 1
204+ else :
205+ ret = subprocess .run (['tar' , '-zxf' , archive , '-C' , tag ,
206+ '--strip-components=1' ,
207+ 'bitcoin-{tag}' .format (tag = tag [1 :])]).returncode
208+ if ret != 0 :
209+ print (f"Failed to extract the { tag } tarball" , file = sys .stderr )
210+ return ret
142211
143212 Path (archive ).unlink ()
144213
@@ -158,22 +227,28 @@ def download_binary(tag, args) -> int:
158227 ['codesign' , '-s' , '-' , binary_path + arm_binary ]
159228 ).returncode
160229 if ret != 0 :
161- print (f"Failed to self-sign { tag } { arm_binary } arm64 binary" )
230+ print (f"Failed to self-sign { tag } { arm_binary } arm64 binary" , file = sys . stderr )
162231 return 1
163232
164233 # Confirm success
165234 ret = subprocess .run (
166235 ['codesign' , '-v' , binary_path + arm_binary ]
167236 ).returncode
168237 if ret != 0 :
169- print (f"Failed to verify the self-signed { tag } { arm_binary } arm64 binary" )
238+ print (f"Failed to verify the self-signed { tag } { arm_binary } arm64 binary" , file = sys . stderr )
170239 return 1
171240
172241 return 0
173242
174243
175- def check_host (args ) -> int :
176- args .host = os .environ .get ('HOST' , subprocess .check_output (
244+ def set_host (args ) -> int :
245+ if platform .system ().lower () == 'windows' :
246+ if platform .machine () != 'AMD64' :
247+ print ('Only 64bit Windows supported' , file = sys .stderr )
248+ return 1
249+ args .host = 'win64'
250+ return 0
251+ host = os .environ .get ('HOST' , subprocess .check_output (
177252 './depends/config.guess' ).decode ())
178253 platforms = {
179254 'aarch64-*-linux*' : 'aarch64-linux-gnu' ,
@@ -183,20 +258,20 @@ def check_host(args) -> int:
183258 'x86_64-apple-darwin*' : 'x86_64-apple-darwin' ,
184259 'aarch64-apple-darwin*' : 'arm64-apple-darwin' ,
185260 }
186- args .platform = ''
261+ args .host = ''
187262 for pattern , target in platforms .items ():
188- if fnmatch (args . host , pattern ):
189- args .platform = target
190- if not args .platform :
191- print ('Not sure which binary to download for {}' .format (args . host ))
263+ if fnmatch (host , pattern ):
264+ args .host = target
265+ if not args .host :
266+ print ('Not sure which binary to download for {}' .format (host ), file = sys . stderr )
192267 return 1
193268 return 0
194269
195270
196271def main (args ) -> int :
197272 Path (args .target_dir ).mkdir (exist_ok = True , parents = True )
198273 print ("Releases directory: {}" .format (args .target_dir ))
199- ret = check_host (args )
274+ ret = set_host (args )
200275 if ret :
201276 return ret
202277 with pushd (args .target_dir ):
0 commit comments