| 52 | |
| 53 | |
| 54 | def try_run_program( |
| 55 | program, # type: str |
| 56 | args, # type: Iterable[str] |
| 57 | program_info_url=None, # type: Optional[str] |
| 58 | error=None, # type: Optional[str] |
| 59 | disown=False, # type: bool |
| 60 | **kwargs # type: Any |
| 61 | ): |
| 62 | # type: (...) -> Result |
| 63 | cmd = [program] + list(args) |
| 64 | kwargs = dict(subprocess_daemon_kwargs() if disown else {}, **kwargs) |
| 65 | try: |
| 66 | process = subprocess.Popen(cmd, **kwargs) |
| 67 | if not disown and process.wait() != 0: |
| 68 | return Error( |
| 69 | str( |
| 70 | CalledProcessError( |
| 71 | returncode=process.returncode, |
| 72 | cmd=" ".join(shlex_quote(arg) for arg in cmd), |
| 73 | ) |
| 74 | ), |
| 75 | exit_code=process.returncode, |
| 76 | ) |
| 77 | return Ok() |
| 78 | except OSError as e: |
| 79 | msg = [error] if error else [] |
| 80 | msg.append("Do you have `{}` installed on the $PATH?: {}".format(program, e)) |
| 81 | if program_info_url: |
| 82 | msg.append( |
| 83 | "Find more information on `{program}` at {url}.".format( |
| 84 | program=program, url=program_info_url |
| 85 | ) |
| 86 | ) |
| 87 | return Error("\n".join(msg)) |
| 88 | |
| 89 | |
| 90 | def try_open( |