1
Fork 0

bootstrap: Fix some PEP8 issues

This commit also adds a few missing docstrings
This commit is contained in:
Milton Mazzarri 2017-06-30 23:24:35 -05:00
parent 7d89b20669
commit ebf24ad3e9
No known key found for this signature in database
GPG key ID: 9F4193F2B5A558FE

View file

@ -25,10 +25,11 @@ from time import time
def get(url, path, verbose=False): def get(url, path, verbose=False):
sha_url = url + ".sha256" suffix = '.sha256'
sha_url = url + suffix
with tempfile.NamedTemporaryFile(delete=False) as temp_file: with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_path = temp_file.name temp_path = temp_file.name
with tempfile.NamedTemporaryFile(suffix=".sha256", delete=False) as sha_file: with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as sha_file:
sha_path = sha_file.name sha_path = sha_file.name
try: try:
@ -55,6 +56,7 @@ def get(url, path, verbose=False):
def delete_if_present(path, verbose): def delete_if_present(path, verbose):
"""Remove the given file if present"""
if os.path.isfile(path): if os.path.isfile(path):
if verbose: if verbose:
print("removing " + path) print("removing " + path)
@ -92,12 +94,13 @@ def _download(path, url, probably_big, verbose, exception):
def verify(path, sha_path, verbose): def verify(path, sha_path, verbose):
"""Check if the sha256 sum of the given path is valid"""
if verbose: if verbose:
print("verifying " + path) print("verifying " + path)
with open(path, "rb") as f: with open(path, "rb") as source:
found = hashlib.sha256(f.read()).hexdigest() found = hashlib.sha256(source.read()).hexdigest()
with open(sha_path, "r") as f: with open(sha_path, "r") as sha256sum:
expected = f.readline().split()[0] expected = sha256sum.readline().split()[0]
verified = found == expected verified = found == expected
if not verified: if not verified:
print("invalid checksum:\n" print("invalid checksum:\n"
@ -107,6 +110,7 @@ def verify(path, sha_path, verbose):
def unpack(tarball, dst, verbose=False, match=None): def unpack(tarball, dst, verbose=False, match=None):
"""Unpack the given tarball file"""
print("extracting " + tarball) print("extracting " + tarball)
fname = os.path.basename(tarball).replace(".tar.gz", "") fname = os.path.basename(tarball).replace(".tar.gz", "")
with contextlib.closing(tarfile.open(tarball)) as tar: with contextlib.closing(tarfile.open(tarball)) as tar:
@ -128,6 +132,7 @@ def unpack(tarball, dst, verbose=False, match=None):
shutil.move(tp, fp) shutil.move(tp, fp)
shutil.rmtree(os.path.join(dst, fname)) shutil.rmtree(os.path.join(dst, fname))
def run(args, verbose=False, exception=False, **kwargs): def run(args, verbose=False, exception=False, **kwargs):
if verbose: if verbose:
print("running: " + ' '.join(args)) print("running: " + ' '.join(args))
@ -245,7 +250,8 @@ class RustBuild(object):
return return
# At this point we're pretty sure the user is running NixOS # At this point we're pretty sure the user is running NixOS
print("info: you seem to be running NixOS. Attempting to patch " + fname) nix_os_msg = "info: you seem to be running NixOS. Attempting to patch"
print(" ".join([nix_os_msg, fname]))
try: try:
interpreter = subprocess.check_output( interpreter = subprocess.check_output(
@ -293,18 +299,22 @@ class RustBuild(object):
return self._cargo_channel return self._cargo_channel
def rustc_stamp(self): def rustc_stamp(self):
"""Return the path for .rustc-stamp"""
return os.path.join(self.bin_root(), '.rustc-stamp') return os.path.join(self.bin_root(), '.rustc-stamp')
def cargo_stamp(self): def cargo_stamp(self):
"""Return the path for .cargo-stamp"""
return os.path.join(self.bin_root(), '.cargo-stamp') return os.path.join(self.bin_root(), '.cargo-stamp')
def rustc_out_of_date(self): def rustc_out_of_date(self):
"""Check if rustc is out of date"""
if not os.path.exists(self.rustc_stamp()) or self.clean: if not os.path.exists(self.rustc_stamp()) or self.clean:
return True return True
with open(self.rustc_stamp(), 'r') as f: with open(self.rustc_stamp(), 'r') as f:
return self.stage0_date() != f.read() return self.stage0_date() != f.read()
def cargo_out_of_date(self): def cargo_out_of_date(self):
"""Check if cargo is out of date"""
if not os.path.exists(self.cargo_stamp()) or self.clean: if not os.path.exists(self.cargo_stamp()) or self.clean:
return True return True
with open(self.cargo_stamp(), 'r') as f: with open(self.cargo_stamp(), 'r') as f:
@ -357,7 +367,6 @@ class RustBuild(object):
def exe_suffix(self): def exe_suffix(self):
if sys.platform == 'win32': if sys.platform == 'win32':
return '.exe' return '.exe'
else:
return '' return ''
def print_what_it_means_to_bootstrap(self): def print_what_it_means_to_bootstrap(self):
@ -366,7 +375,7 @@ class RustBuild(object):
self.printed = True self.printed = True
if os.path.exists(self.bootstrap_binary()): if os.path.exists(self.bootstrap_binary()):
return return
if not '--help' in sys.argv or len(sys.argv) == 1: if '--help' not in sys.argv or len(sys.argv) == 1:
return return
print('info: the build system for Rust is written in Rust, so this') print('info: the build system for Rust is written in Rust, so this')
@ -461,8 +470,8 @@ class RustBuild(object):
# always emit 'i386' on x86/amd64 systems). As such, isainfo -k # always emit 'i386' on x86/amd64 systems). As such, isainfo -k
# must be used instead. # must be used instead.
try: try:
cputype = subprocess.check_output(['isainfo', cputype = subprocess.check_output(
'-k']).strip().decode(default_encoding) ['isainfo', '-k']).strip().decode(default_encoding)
except (subprocess.CalledProcessError, OSError): except (subprocess.CalledProcessError, OSError):
err = "isainfo not found" err = "isainfo not found"
if self.verbose: if self.verbose:
@ -562,21 +571,26 @@ class RustBuild(object):
default_encoding = sys.getdefaultencoding() default_encoding = sys.getdefaultencoding()
run(["git", "submodule", "-q", "sync"], cwd=self.rust_root) run(["git", "submodule", "-q", "sync"], cwd=self.rust_root)
submodules = [s.split(' ', 1)[1] for s in subprocess.check_output( submodules = [s.split(' ', 1)[1] for s in subprocess.check_output(
["git", "config", "--file", os.path.join(self.rust_root, ".gitmodules"), ["git", "config", "--file",
os.path.join(self.rust_root, ".gitmodules"),
"--get-regexp", "path"] "--get-regexp", "path"]
).decode(default_encoding).splitlines()] ).decode(default_encoding).splitlines()]
submodules = [module for module in submodules submodules = [module for module in submodules
if not ((module.endswith("llvm") and if not ((module.endswith("llvm") and
(self.get_toml('llvm-config') or self.get_mk('CFG_LLVM_ROOT'))) or (self.get_toml('llvm-config') or
self.get_mk('CFG_LLVM_ROOT'))) or
(module.endswith("jemalloc") and (module.endswith("jemalloc") and
(self.get_toml('jemalloc') or self.get_mk('CFG_JEMALLOC_ROOT')))) (self.get_toml('jemalloc') or
] self.get_mk('CFG_JEMALLOC_ROOT'))))]
run(["git", "submodule", "update", run(["git", "submodule", "update",
"--init"] + submodules, cwd=self.rust_root, verbose=self.verbose) "--init"] + submodules,
cwd=self.rust_root, verbose=self.verbose)
run(["git", "submodule", "-q", "foreach", "git", run(["git", "submodule", "-q", "foreach", "git",
"reset", "-q", "--hard"], cwd=self.rust_root, verbose=self.verbose) "reset", "-q", "--hard"],
cwd=self.rust_root, verbose=self.verbose)
run(["git", "submodule", "-q", "foreach", "git", run(["git", "submodule", "-q", "foreach", "git",
"clean", "-qdfx"], cwd=self.rust_root, verbose=self.verbose) "clean", "-qdfx"],
cwd=self.rust_root, verbose=self.verbose)
def bootstrap(): def bootstrap():
@ -692,5 +706,6 @@ def main():
format_build_time(time() - start_time)) format_build_time(time() - start_time))
sys.exit(exit_code) sys.exit(exit_code)
if __name__ == '__main__': if __name__ == '__main__':
main() main()