Created
May 2, 2018 20:46
-
-
Save x10an14/9d7597e354d08d8cca7947a84fc04bb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.6 | |
from contextlib import suppress | |
from pathlib import Path, PosixPath | |
from subprocess import run, PIPE | |
from sys import exit, argv | |
from sys import stderr, executable | |
if __name__ == '__main__': | |
DEFAULT_LOCATION = (Path.home() / '.python-venvs').expanduser() | |
USAGE_STR = f""" | |
The following syntax is valid when using this script/program: | |
$ python(3.6[+]) {argv[0]} <pypi package to install globally> | |
With an additional optional parameter at the end, for specifying | |
where on the filesystem to put the virtualenv. | |
(Defaults to '{DEFAULT_LOCATION}'). | |
""" | |
if len(argv) < 2: | |
print(USAGE_STR, file=stderr) | |
exit(2) | |
if len(argv) >= 3: | |
package, install_path = argv[1:3] | |
else: | |
package, install_path = argv[1], DEFAULT_LOCATION | |
# Ensure existence of install path | |
if '~' in str(install_path): | |
install_path = install_path.expanduser() | |
install_path = Path(install_path).resolve() | |
if not install_path.is_dir(): | |
install_path.mkdir(parents=True) | |
print(f"\tCreated missing install path: '{install_path}'.", file=stderr) | |
install_path = install_path / package | |
install_cmd = f"{executable} -m venv {install_path}" | |
run( | |
args=install_cmd.split(' '), | |
stdout=PIPE, stderr=PIPE, | |
universal_newlines=True, | |
) | |
print( | |
f"\tCreated virtualenv for {package} at '{install_path}'.", file=stderr | |
) | |
# Remember to update PIP | |
pip_binary = install_path / 'bin' / 'pip' | |
update_pip = f"{pip_binary} install -U pip" | |
run( | |
args=update_pip.split(' '), | |
stdout=PIPE, stderr=PIPE, | |
universal_newlines=True, | |
) | |
print(f"\tUpdated pip in virtualenv '{install_path}'.", file=stderr) | |
# Actual install: | |
install_package = f"{pip_binary} install {package}" | |
run( | |
args=install_package.split(' '), | |
stdout=PIPE, stderr=PIPE, | |
universal_newlines=True, | |
) | |
print( | |
f"\tInstalled package '{package}' in virtualenv at '{install_path}'.", | |
file=stderr | |
) | |
with suppress(FileNotFoundError): | |
package_bin = (pip_binary / '..' / package).resolve(strict=True) | |
symlink = (Path.home() / 'bin').resolve(strict=True) / package | |
if ( | |
symlink.exists() or | |
not isinstance(package_bin, PosixPath) | |
): | |
exit(0) | |
answer = input( | |
f"Do you want to create a symlink from " | |
f"\n'{package_bin}' to \n'{symlink}'? [Y/n] " | |
) | |
if answer and not answer.lower().startswith('y'): | |
exit(0) | |
# Create symlink: | |
symlink.symlink_to(package_bin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment