diff options
Diffstat (limited to 'python/servo/command_base.py')
-rw-r--r-- | python/servo/command_base.py | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/python/servo/command_base.py b/python/servo/command_base.py index a5b4a555190..dd02446eb63 100644 --- a/python/servo/command_base.py +++ b/python/servo/command_base.py @@ -1,22 +1,23 @@ import os from os import path +import contextlib import subprocess import sys import toml from mach.registrar import Registrar -class cd: - """Context manager for changing the current working directory""" - def __init__(self, newPath): - self.newPath = newPath - def __enter__(self): - self.savedPath = os.getcwd() - os.chdir(self.newPath) +@contextlib.contextmanager +def cd(new_path): + """Context manager for changing the current working directory""" + previous_path = os.getcwd() + try: + os.chdir(new_path) + yield + finally: + os.chdir(previous_path) - def __exit__(self, etype, value, traceback): - os.chdir(self.savedPath) class CommandBase(object): """Base class for mach command providers. @@ -42,23 +43,29 @@ class CommandBase(object): self.config["tools"].setdefault("rust-root", "") self.config["tools"].setdefault("cargo-root", "") if not self.config["tools"]["system-rust"]: - self.config["tools"]["rust-root"] = path.join(context.topdir, "rust") + self.config["tools"]["rust-root"] = path.join( + context.topdir, "rust") if not self.config["tools"]["system-cargo"]: - self.config["tools"]["cargo-root"] = path.join(context.topdir, "cargo") + self.config["tools"]["cargo-root"] = path.join( + context.topdir, "cargo") def build_env(self): """Return an extended environment dictionary.""" env = os.environ.copy() extra_path = [] extra_lib = [] - if not self.config["tools"]["system-rust"] or self.config["tools"]["rust-root"]: + if not self.config["tools"]["system-rust"] \ + or self.config["tools"]["rust-root"]: extra_path += [path.join(self.config["tools"]["rust-root"], "bin")] extra_lib += [path.join(self.config["tools"]["rust-root"], "lib")] - if not self.config["tools"]["system-cargo"] or self.config["tools"]["cargo-root"]: - extra_path += [path.join(self.config["tools"]["cargo-root"], "bin")] + if not self.config["tools"]["system-cargo"] \ + or self.config["tools"]["cargo-root"]: + extra_path += [ + path.join(self.config["tools"]["cargo-root"], "bin")] if extra_path: - env["PATH"] = "%s%s%s" % (os.pathsep.join(extra_path), os.pathsep, env["PATH"]) + env["PATH"] = "%s%s%s" % ( + os.pathsep.join(extra_path), os.pathsep, env["PATH"]) if extra_lib: if sys.platform == "darwin": env["DYLD_LIBRARY_PATH"] = "%s%s%s" % \ @@ -74,7 +81,8 @@ class CommandBase(object): return env def ensure_bootstrapped(self): - if self.context.bootstrapped: return + if self.context.bootstrapped: + return submodules = subprocess.check_output(["git", "submodule", "status"]) for line in submodules.split('\n'): @@ -82,13 +90,16 @@ class CommandBase(object): if len(components) > 1 and components[0].startswith(('-', '+')): module_path = components[1] subprocess.check_call(["git", "submodule", "update", - "--init", "--recursive", "--", module_path]) + "--init", "--recursive", + "--", module_path]) if not self.config["tools"]["system-rust"] and \ - not path.exists(path.join(self.context.topdir, "rust", "bin", "rustc")): + not path.exists(path.join( + self.context.topdir, "rust", "bin", "rustc")): Registrar.dispatch("bootstrap-rust", context=self.context) if not self.config["tools"]["system-cargo"] and \ - not path.exists(path.join(self.context.topdir, "cargo", "bin", "cargo")): + not path.exists(path.join( + self.context.topdir, "cargo", "bin", "cargo")): Registrar.dispatch("bootstrap-cargo", context=self.context) self.context.bootstrapped = True |