Skip to content

Commit aef0fd7

Browse files
committed
build: use common function for compressing Python archive
1 parent d5e1d17 commit aef0fd7

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

build-linux.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ def bootstrap():
4747

4848

4949
def run():
50-
import zstandard
5150
from pythonbuild.downloads import DOWNLOADS
52-
from pythonbuild.utils import hash_path
51+
from pythonbuild.utils import compress_python_archive
5352

5453
now = datetime.datetime.utcnow()
5554

@@ -66,18 +65,10 @@ def run():
6665
basename += '.tar'
6766

6867
source_path = BUILD / basename
69-
dest_path = DIST / ('cpython-%s-linux64%s-%s.tar.zst' % (
68+
compress_python_archive(source_path, DIST, 'cpython-%s-linux64%s-%s' % (
7069
DOWNLOADS['cpython-3.7']['version'], extra,
7170
now.strftime('%Y%m%dT%H%M')))
7271

73-
print('compressing Python archive to %s' % dest_path)
74-
with source_path.open('rb') as ifh, dest_path.open('wb') as ofh:
75-
cctx = zstandard.ZstdCompressor(level=15)
76-
cctx.copy_stream(ifh, ofh, source_path.stat().st_size)
77-
78-
sha256 = hash_path(dest_path)
79-
print('%s has SHA256 %s' % (dest_path, sha256))
80-
8172

8273
if __name__ == '__main__':
8374
try:

build-macos.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,18 @@ def bootstrap():
3737

3838

3939
def run():
40-
import zstandard
4140
from pythonbuild.downloads import DOWNLOADS
42-
from pythonbuild.utils import hash_path
41+
from pythonbuild.utils import compress_python_archive
4342

4443
now = datetime.datetime.utcnow()
4544

4645
subprocess.run(['make'],
4746
cwd=str(MAKE_DIR), check=True)
4847

4948
source_path = BUILD / 'cpython-macos.tar'
50-
dest_path = DIST / ('cpython-%s-macos-%s.tar.zst' % (
51-
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
52-
53-
print('compressing Python archive to %s' % dest_path)
54-
with source_path.open('rb') as ifh, dest_path.open('wb') as ofh:
55-
cctx = zstandard.ZstdCompressor(level=15)
56-
cctx.copy_stream(ifh, ofh, source_path.stat().st_size)
5749

58-
sha256 = hash_path(dest_path)
59-
print('%s has SHA256 %s' % (dest_path, sha256))
50+
compress_python_archive(source_path, DIST, 'cpython-%s-macos-%s' % (
51+
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
6052

6153

6254
if __name__ == '__main__':

build-windows.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ def bootstrap():
3737

3838

3939
def run():
40-
import zstandard
4140
from pythonbuild.downloads import DOWNLOADS
42-
from pythonbuild.utils import hash_path
41+
from pythonbuild.utils import compress_python_archive
4342

4443
now = datetime.datetime.utcnow()
4544

@@ -51,16 +50,9 @@ def run():
5150
bufsize=0)
5251

5352
source_path = BUILD / 'cpython-windows.tar'
54-
dest_path = DIST / ('cpython-%s-windows-amd64-%s.tar.zst' % (
55-
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
56-
57-
print('compressing Python archive to %s' % dest_path)
58-
with source_path.open('rb') as ifh, dest_path.open('wb') as ofh:
59-
cctx = zstandard.ZstdCompressor(level=15)
60-
cctx.copy_stream(ifh, ofh, source_path.stat().st_size)
6153

62-
sha256 = hash_path(dest_path)
63-
print('%s has SHA256 %s' % (dest_path, sha256))
54+
compress_python_archive(source_path, DIST, 'cpython-%s-windows-amd64-%s' % (
55+
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
6456

6557

6658
if __name__ == '__main__':

pythonbuild/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import tarfile
1010
import urllib.request
1111

12+
import zstandard
13+
1214
from .downloads import (
1315
DOWNLOADS,
1416
)
@@ -132,3 +134,26 @@ def create_tar_from_directory(fh, base_path: pathlib.Path):
132134
def extract_tar_to_directory(source: pathlib.Path, dest: pathlib.Path):
133135
with tarfile.open(source, 'r') as tf:
134136
tf.extractall(dest)
137+
138+
139+
def compress_python_archive(source_path: pathlib.Path,
140+
dist_path: pathlib.Path,
141+
basename: str):
142+
dest_path = dist_path / ('%s.tar.zst' % basename)
143+
temp_path = dist_path / ('%s.tar.zst.tmp' % basename)
144+
145+
print('compressing Python archive to %s' % dest_path)
146+
147+
try:
148+
with source_path.open('rb') as ifh, temp_path.open('wb') as ofh:
149+
cctx = zstandard.ZstdCompressor(level=15)
150+
cctx.copy_stream(ifh, ofh, source_path.stat().st_size)
151+
152+
temp_path.rename(dest_path)
153+
except Exception:
154+
temp_path.unlink()
155+
raise
156+
157+
print('%s has SHA256 %s' % (dest_path, hash_path(dest_path)))
158+
159+
return dest_path

0 commit comments

Comments
 (0)