Skip to content

Commit c3a130c

Browse files
committedJul 2, 2017
Auto merge of #43003 - milmazz:bootstrap-pep8, r=alexcrichton
bootstrap: Fix all the pep-8 issues reported by flake8 This commit also adds a few missing docstrings. Today, after reading this [article](https://blog.rust-lang.org/2017/06/27/Increasing-Rusts-Reach.html), I downloaded this project and started building from source. In the meantime, I began to read the `bootstrap.py`, to know more about the building process, and I made a few changes, this is my first contribution to the project, hope you like it. BTW, I have a few doubts about the `bootstrap.py`, any guidance is more than welcome: * Where can I find the unit tests for this script? In case it doesn't exist yet, do you like to include some unit tests with pytest? * Some methods like `fix_executable`, `get_string`, and `exe_suffix` in the `RustBuild` class should be converted to a function because it doesn't use `self` anywhere. What do you think?
2 parents a19693a + 44c6781 commit c3a130c

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed
 

‎src/bootstrap/bootstrap.py

+34-19
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626

2727
def get(url, path, verbose=False):
28-
sha_url = url + ".sha256"
28+
suffix = '.sha256'
29+
sha_url = url + suffix
2930
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
3031
temp_path = temp_file.name
31-
with tempfile.NamedTemporaryFile(suffix=".sha256", delete=False) as sha_file:
32+
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as sha_file:
3233
sha_path = sha_file.name
3334

3435
try:
@@ -55,6 +56,7 @@ def get(url, path, verbose=False):
5556

5657

5758
def delete_if_present(path, verbose):
59+
"""Remove the given file if present"""
5860
if os.path.isfile(path):
5961
if verbose:
6062
print("removing " + path)
@@ -92,12 +94,13 @@ def _download(path, url, probably_big, verbose, exception):
9294

9395

9496
def verify(path, sha_path, verbose):
97+
"""Check if the sha256 sum of the given path is valid"""
9598
if verbose:
9699
print("verifying " + path)
97-
with open(path, "rb") as f:
98-
found = hashlib.sha256(f.read()).hexdigest()
99-
with open(sha_path, "r") as f:
100-
expected = f.readline().split()[0]
100+
with open(path, "rb") as source:
101+
found = hashlib.sha256(source.read()).hexdigest()
102+
with open(sha_path, "r") as sha256sum:
103+
expected = sha256sum.readline().split()[0]
101104
verified = found == expected
102105
if not verified:
103106
print("invalid checksum:\n"
@@ -107,6 +110,7 @@ def verify(path, sha_path, verbose):
107110

108111

109112
def unpack(tarball, dst, verbose=False, match=None):
113+
"""Unpack the given tarball file"""
110114
print("extracting " + tarball)
111115
fname = os.path.basename(tarball).replace(".tar.gz", "")
112116
with contextlib.closing(tarfile.open(tarball)) as tar:
@@ -128,6 +132,7 @@ def unpack(tarball, dst, verbose=False, match=None):
128132
shutil.move(tp, fp)
129133
shutil.rmtree(os.path.join(dst, fname))
130134

135+
131136
def run(args, verbose=False, exception=False, **kwargs):
132137
if verbose:
133138
print("running: " + ' '.join(args))
@@ -245,7 +250,8 @@ def fix_executable(self, fname):
245250
return
246251

247252
# At this point we're pretty sure the user is running NixOS
248-
print("info: you seem to be running NixOS. Attempting to patch " + fname)
253+
nix_os_msg = "info: you seem to be running NixOS. Attempting to patch"
254+
print(nix_os_msg, fname)
249255

250256
try:
251257
interpreter = subprocess.check_output(
@@ -293,18 +299,22 @@ def stage0_cargo_channel(self):
293299
return self._cargo_channel
294300

295301
def rustc_stamp(self):
302+
"""Return the path for .rustc-stamp"""
296303
return os.path.join(self.bin_root(), '.rustc-stamp')
297304

298305
def cargo_stamp(self):
306+
"""Return the path for .cargo-stamp"""
299307
return os.path.join(self.bin_root(), '.cargo-stamp')
300308

301309
def rustc_out_of_date(self):
310+
"""Check if rustc is out of date"""
302311
if not os.path.exists(self.rustc_stamp()) or self.clean:
303312
return True
304313
with open(self.rustc_stamp(), 'r') as f:
305314
return self.stage0_date() != f.read()
306315

307316
def cargo_out_of_date(self):
317+
"""Check if cargo is out of date"""
308318
if not os.path.exists(self.cargo_stamp()) or self.clean:
309319
return True
310320
with open(self.cargo_stamp(), 'r') as f:
@@ -357,16 +367,15 @@ def get_string(self, line):
357367
def exe_suffix(self):
358368
if sys.platform == 'win32':
359369
return '.exe'
360-
else:
361-
return ''
370+
return ''
362371

363372
def print_what_it_means_to_bootstrap(self):
364373
if hasattr(self, 'printed'):
365374
return
366375
self.printed = True
367376
if os.path.exists(self.bootstrap_binary()):
368377
return
369-
if not '--help' in sys.argv or len(sys.argv) == 1:
378+
if '--help' not in sys.argv or len(sys.argv) == 1:
370379
return
371380

372381
print('info: the build system for Rust is written in Rust, so this')
@@ -461,8 +470,8 @@ def build_triple(self):
461470
# always emit 'i386' on x86/amd64 systems). As such, isainfo -k
462471
# must be used instead.
463472
try:
464-
cputype = subprocess.check_output(['isainfo',
465-
'-k']).strip().decode(default_encoding)
473+
cputype = subprocess.check_output(
474+
['isainfo', '-k']).strip().decode(default_encoding)
466475
except (subprocess.CalledProcessError, OSError):
467476
err = "isainfo not found"
468477
if self.verbose:
@@ -562,21 +571,26 @@ def update_submodules(self):
562571
default_encoding = sys.getdefaultencoding()
563572
run(["git", "submodule", "-q", "sync"], cwd=self.rust_root)
564573
submodules = [s.split(' ', 1)[1] for s in subprocess.check_output(
565-
["git", "config", "--file", os.path.join(self.rust_root, ".gitmodules"),
574+
["git", "config", "--file",
575+
os.path.join(self.rust_root, ".gitmodules"),
566576
"--get-regexp", "path"]
567577
).decode(default_encoding).splitlines()]
568578
submodules = [module for module in submodules
569579
if not ((module.endswith("llvm") and
570-
(self.get_toml('llvm-config') or self.get_mk('CFG_LLVM_ROOT'))) or
580+
(self.get_toml('llvm-config') or
581+
self.get_mk('CFG_LLVM_ROOT'))) or
571582
(module.endswith("jemalloc") and
572-
(self.get_toml('jemalloc') or self.get_mk('CFG_JEMALLOC_ROOT'))))
573-
]
583+
(self.get_toml('jemalloc') or
584+
self.get_mk('CFG_JEMALLOC_ROOT'))))]
574585
run(["git", "submodule", "update",
575-
"--init"] + submodules, cwd=self.rust_root, verbose=self.verbose)
586+
"--init"] + submodules,
587+
cwd=self.rust_root, verbose=self.verbose)
576588
run(["git", "submodule", "-q", "foreach", "git",
577-
"reset", "-q", "--hard"], cwd=self.rust_root, verbose=self.verbose)
589+
"reset", "-q", "--hard"],
590+
cwd=self.rust_root, verbose=self.verbose)
578591
run(["git", "submodule", "-q", "foreach", "git",
579-
"clean", "-qdfx"], cwd=self.rust_root, verbose=self.verbose)
592+
"clean", "-qdfx"],
593+
cwd=self.rust_root, verbose=self.verbose)
580594

581595

582596
def bootstrap():
@@ -692,5 +706,6 @@ def main():
692706
format_build_time(time() - start_time))
693707
sys.exit(exit_code)
694708

709+
695710
if __name__ == '__main__':
696711
main()

0 commit comments

Comments
 (0)
Please sign in to comment.