MCPcopy Create free account
hub / github.com/ElementsProject/elements / download_binary

Function download_binary

test/get_previous_releases.py:83–139  ·  view source on GitHub ↗
(tag, args)

Source from the content-addressed store, hash-verified

81
82
83def download_binary(tag, args) -> int:
84 if Path(tag).is_dir():
85 if not args.remove_dir:
86 print('Using cached {}'.format(tag))
87 return 0
88 shutil.rmtree(tag)
89 Path(tag).mkdir()
90 bin_path = 'bin/bitcoin-core-{}'.format(tag[1:])
91 match = re.compile('v(.*)(rc[0-9]+)$').search(tag)
92 if match:
93 bin_path = 'bin/bitcoin-core-{}/test.{}'.format(
94 match.group(1), match.group(2))
95 tarball = 'bitcoin-{tag}-{platform}.tar.gz'.format(
96 tag=tag[1:], platform=args.platform)
97 tarballUrl = 'https://bitcoincore.org/{bin_path}/{tarball}'.format(
98 bin_path=bin_path, tarball=tarball)
99
100 print('Fetching: {tarballUrl}'.format(tarballUrl=tarballUrl))
101
102 header, status = subprocess.Popen(
103 ['curl', '--head', tarballUrl], stdout=subprocess.PIPE).communicate()
104 if re.search("404 Not Found", header.decode("utf-8")):
105 print("Binary tag was not found")
106 return 1
107
108 curlCmds = [
109 ['curl', '--remote-name', tarballUrl]
110 ]
111
112 for cmd in curlCmds:
113 ret = subprocess.run(cmd).returncode
114 if ret:
115 return ret
116
117 hasher = hashlib.sha256()
118 with open(tarball, "rb") as afile:
119 hasher.update(afile.read())
120 tarballHash = hasher.hexdigest()
121
122 if tarballHash not in SHA256_SUMS or SHA256_SUMS[tarballHash] != tarball:
123 if tarball in SHA256_SUMS.values():
124 print("Checksum did not match")
125 return 1
126
127 print("Checksum for given version doesn't exist")
128 return 1
129 print("Checksum matched")
130
131 # Extract tarball
132 ret = subprocess.run(['tar', '-zxf', tarball, '-C', tag,
133 '--strip-components=1',
134 'bitcoin-{tag}'.format(tag=tag[1:])]).returncode
135 if ret:
136 return ret
137
138 Path(tarball).unlink()
139 return 0
140

Callers 1

mainFunction · 0.85

Calls 5

formatMethod · 0.80
decodeMethod · 0.45
runMethod · 0.45
updateMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected