Skip to content

Commit 0019371

Browse files
author
Guanqun Lu
committed
bootstrap: don't call support_xz in hot-path
1 parent 8d56bcc commit 0019371

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/bootstrap/bootstrap.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,6 @@ def __init__(self):
331331
self.use_vendored_sources = ''
332332
self.verbose = False
333333

334-
def support_xz():
335-
try:
336-
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
337-
temp_path = temp_file.name
338-
with tarfile.open(temp_path, "w:xz") as tar:
339-
pass
340-
return True
341-
except tarfile.CompressionError:
342-
return False
343-
344-
self.tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
345334

346335
def download_stage0(self):
347336
"""Fetch the build system for Rust, written in Rust
@@ -356,19 +345,30 @@ def download_stage0(self):
356345
rustc_channel = self.rustc_channel
357346
cargo_channel = self.cargo_channel
358347

348+
def support_xz():
349+
try:
350+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
351+
temp_path = temp_file.name
352+
with tarfile.open(temp_path, "w:xz") as tar:
353+
pass
354+
return True
355+
except tarfile.CompressionError:
356+
return False
357+
359358
if self.rustc().startswith(self.bin_root()) and \
360359
(not os.path.exists(self.rustc()) or
361360
self.program_out_of_date(self.rustc_stamp())):
362361
if os.path.exists(self.bin_root()):
363362
shutil.rmtree(self.bin_root())
363+
tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
364364
filename = "rust-std-{}-{}{}".format(
365-
rustc_channel, self.build, self.tarball_suffix)
365+
rustc_channel, self.build, tarball_suffix)
366366
pattern = "rust-std-{}".format(self.build)
367-
self._download_stage0_helper(filename, pattern)
367+
self._download_stage0_helper(filename, pattern, tarball_suffix)
368368

369369
filename = "rustc-{}-{}{}".format(rustc_channel, self.build,
370-
self.tarball_suffix)
371-
self._download_stage0_helper(filename, "rustc")
370+
tarball_suffix)
371+
self._download_stage0_helper(filename, "rustc", tarball_suffix)
372372
self.fix_executable("{}/bin/rustc".format(self.bin_root()))
373373
self.fix_executable("{}/bin/rustdoc".format(self.bin_root()))
374374
with output(self.rustc_stamp()) as rust_stamp:
@@ -379,20 +379,21 @@ def download_stage0(self):
379379
# the system MinGW ones.
380380
if "pc-windows-gnu" in self.build:
381381
filename = "rust-mingw-{}-{}{}".format(
382-
rustc_channel, self.build, self.tarball_suffix)
383-
self._download_stage0_helper(filename, "rust-mingw")
382+
rustc_channel, self.build, tarball_suffix)
383+
self._download_stage0_helper(filename, "rust-mingw", tarball_suffix)
384384

385385
if self.cargo().startswith(self.bin_root()) and \
386386
(not os.path.exists(self.cargo()) or
387387
self.program_out_of_date(self.cargo_stamp())):
388+
tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
388389
filename = "cargo-{}-{}{}".format(cargo_channel, self.build,
389-
self.tarball_suffix)
390-
self._download_stage0_helper(filename, "cargo")
390+
tarball_suffix)
391+
self._download_stage0_helper(filename, "cargo", tarball_suffix)
391392
self.fix_executable("{}/bin/cargo".format(self.bin_root()))
392393
with output(self.cargo_stamp()) as cargo_stamp:
393394
cargo_stamp.write(self.date)
394395

395-
def _download_stage0_helper(self, filename, pattern):
396+
def _download_stage0_helper(self, filename, pattern, tarball_suffix):
396397
cache_dst = os.path.join(self.build_dir, "cache")
397398
rustc_cache = os.path.join(cache_dst, self.date)
398399
if not os.path.exists(rustc_cache):
@@ -402,7 +403,7 @@ def _download_stage0_helper(self, filename, pattern):
402403
tarball = os.path.join(rustc_cache, filename)
403404
if not os.path.exists(tarball):
404405
get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
405-
unpack(tarball, self.tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
406+
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
406407

407408
@staticmethod
408409
def fix_executable(fname):

0 commit comments

Comments
 (0)