Skip to content

Commit

Permalink
exit process with return value of ephemeral binary run
Browse files Browse the repository at this point in the history
  • Loading branch information
cs01 committed Oct 11, 2018
1 parent 7c3c626 commit d242624
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# pipx - Execute binaries from Python packages in isolated environments

**Under development, not for general use yet**

<p align="center">
<a href="https://github.com/cs01/pipx/raw/master/pipx_demo.gif">
<img src="https://github.com/cs01/pipx/raw/master/pipx_demo.gif">
Expand Down
14 changes: 5 additions & 9 deletions get-pipx.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,14 @@ def get_fs_package_name(package):


def install(pipx_local_venvs, package, local_bin_dir, pipx_symlink, python, verbose):

fs_package_name = get_fs_package_name(package)
venv_dir = pipx_local_venvs / fs_package_name

venv_dir = pipx_local_venvs / "pipx"
venv_dir.mkdir(parents=True, exist_ok=True)
pipx_symlink.parent.mkdir(parents=True, exist_ok=True)
logging.info(f"virtualenv location is {venv_dir}")
venv = Venv(venv_dir, python=python, verbose=verbose)
venv.create_venv()
venv.install_package(package)
binary_name = "pipx"
binary = venv.bin_path / binary_name
binary = venv.bin_path / 'pipx'
if not binary.is_file():
fail(f"Expected to find {str(binary)}")
if pipx_symlink.is_file():
Expand Down Expand Up @@ -169,7 +165,7 @@ def parse_options(argv):
help="Don't configure the PATH environment variable",
)
parser.add_argument(
"--ignore-existing",
"--overwrite",
action="store_true",
help=("reinstall pipx even if existing installation was found"),
)
Expand All @@ -195,8 +191,8 @@ def main(argv=sys.argv[1:]):
local_bin_dir = os.environ.get("PIPX_BIN_DIR", DEFAULT_PIPX_BIN_DIR)
pipx_symlink = local_bin_dir / "pipx"
if which("pipx"):
if args.ignore_existing:
echo("pipx is already installed. Ignoring installation and continuing.")
if args.overwrite:
echo("reinstalling pipx")
else:
succeed("You already have pipx installed. Type `pipx` to run.")
elif pipx_symlink.is_symlink() and pipx_symlink.resolve().is_file():
Expand Down
24 changes: 11 additions & 13 deletions pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_package_binary_paths(self, package):
def run_binary(self, binary, binary_args):
cmd = [self.bin_path / binary] + binary_args
try:
_run(cmd, check=False)
return _run(cmd, check=False)
except KeyboardInterrupt:
pass

Expand All @@ -147,7 +147,7 @@ def _run_pip(self, cmd):
cmd = [self.pip_path] + cmd
if not self.verbose:
cmd.append("-q")
_run(cmd)
return _run(cmd)


def _run(cmd, check=True):
Expand All @@ -156,6 +156,7 @@ def _run(cmd, check=True):
returncode = subprocess.run(cmd).returncode
if check and returncode:
raise PipxError(f"{cmd_str!r} failed")
return returncode


def rmdir(path):
Expand All @@ -180,7 +181,7 @@ def download_and_run(venv_dir, package, binary, binary_args, python, verbose):
f"{binary} not found in package {package}. Available binaries: "
f"{', '.join(b.name for b in binaries)}"
)
venv.run_binary(binary, binary_args)
return venv.run_binary(binary, binary_args)


def symlink_package_binaries(local_bin_dir, binary_paths, package):
Expand Down Expand Up @@ -282,11 +283,11 @@ def install(venv_dir, package, package_or_url, local_bin_dir, python, verbose):

if venv.get_package_version(package) is None:
venv.remove_venv()
raise PipxError("Could not find package {package} after installing. Is the name correct?")
elif not binary_paths:
raise PipxError(f"Could not find package {package}. Is the name correct?")
binary_paths = venv.get_package_binary_paths(package)
if not binary_paths:
venv.remove_venv()
raise PipxError("No binaries associated with this package")
binary_paths = venv.get_package_binary_paths(package)
logging.info(f"new binaries: {', '.join(str(b.name) for b in binary_paths)}")
symlink_package_binaries(local_bin_dir, binary_paths, package)
print("done! ✨ 🌟 ✨")
Expand Down Expand Up @@ -341,7 +342,7 @@ def run_pipx_command(args):
"package from a url, pass the --url flag."
)
if package == "pipx":
print("isntalling pipx from url f{INSTALL_PIPX_URL}")
print(f"isntalling pipx from url {INSTALL_PIPX_URL}")
args.url = INSTALL_PIPX_URL
if "url" in args:
if urllib.parse.urlparse(args.url).scheme:
Expand Down Expand Up @@ -402,7 +403,7 @@ def run_ephemeral_binary(args, binary_args):
prefix=f"{get_fs_package_name(package)}_"
) as venv_dir:
logging.info(f"virtualenv is temporary, its location is {venv_dir}")
download_and_run(
return download_and_run(
Path(venv_dir), package, binary, binary_args, args.python, verbose
)

Expand Down Expand Up @@ -576,12 +577,9 @@ def cli():
)
args = get_binary_parser(add_help=True).parse_args(pipx_args)
setup(args)
run_ephemeral_binary(args, binary_args)
exit(run_ephemeral_binary(args, binary_args))
except PipxError as e:
print(e)
exit(1)

exit(0)
exit(e)


if __name__ == "__main__":
Expand Down

0 comments on commit d242624

Please sign in to comment.