def run_cmd(cmd, show_output=True, raise_errs=True, **kwargs): """Run a console command. When show_output=True, prints output and returns exit code, otherwise returns output. When raise_errs=True, raises a subprocess.CalledProcessError if the command fails. """ internal_assert(cmd and isinstance(cmd, list), "console commands must be passed as non-empty lists") try: from shutil import which except ImportError: pass else: cmd[0] = which(cmd[0]) or cmd[0] logger.log_cmd(cmd) try: if show_output and raise_errs: return subprocess.check_call(cmd, **kwargs) elif show_output: return subprocess.call(cmd, **kwargs) else: stdout, stderr, retcode = call_output(cmd, **kwargs) output = "".join(stdout + stderr) if retcode and raise_errs: raise subprocess.CalledProcessError(retcode, cmd, output=output) return output except OSError: logger.log_exc() if raise_errs: raise subprocess.CalledProcessError(oserror_retcode, cmd) elif show_output: return oserror_retcode else: return ""
def handling_broken_process_pool(): """Handle BrokenProcessPool error.""" if sys.version_info < (3, 3): yield else: from concurrent.futures.process import BrokenProcessPool try: yield except BrokenProcessPool: logger.log_exc() raise KeyboardInterrupt("broken process pool")
def stdin_readable(): """Determine whether stdin has any data to read.""" if not WINDOWS: try: return bool(select([sys.stdin], [], [], 0)[0]) except Exception: logger.log_exc() try: return not sys.stdin.isatty() except Exception: logger.log_exc() return False
def symlink(link_to, link_from): """Link link_from to the directory link_to universally.""" if os.path.exists(link_from) and not os.path.islink(link_from): shutil.rmtree(link_from) try: if PY32: os.symlink(link_to, link_from, target_is_directory=True) elif not WINDOWS: os.symlink(link_to, link_from) except OSError: logger.log_exc() else: return if not os.path.islink(link_from): shutil.copytree(link_to, link_from)