-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-pipx.py
216 lines (181 loc) · 6.2 KB
/
get-pipx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
import argparse
from pathlib import Path
from shutil import which
import sys
import os
import textwrap
import subprocess
import logging
DEFAULT_PIPX_HOME = Path.home() / ".local/pipx/venvs"
DEFAULT_PIPX_BIN_DIR = Path.home() / ".local/bin"
def echo(msg=""):
sys.stdout.write(msg + "\n")
sys.stdout.flush()
def fail(msg):
sys.stderr.write(msg + "\n")
sys.stderr.flush()
sys.exit(1)
def succeed(msg=""):
if msg:
echo(msg)
sys.exit(0)
def _run(cmd, check=True):
cmd_str = " ".join(str(c) for c in cmd)
logging.info(f"running {cmd_str}")
returncode = subprocess.run(cmd).returncode
if check and returncode:
fail(f"{cmd_str!r} failed")
class Venv:
def __init__(self, path, *, verbose=False, python="python3"):
self.root = path
self._python = python
self.bin_path = path / "bin"
self.pip_path = self.bin_path / "pip"
self.python_path = self.bin_path / "python"
self.verbose = verbose
def create_venv(self):
_run([self._python, "-m", "venv", self.root])
self.upgrade_package("pip")
def install_package(self, application):
before = set(child for child in self.bin_path.iterdir())
self._run_pip(["install", application])
after = set(child for child in self.bin_path.iterdir())
new_binaries = after - before
new_binaries_str = ", ".join(str(s) for s in new_binaries)
logging.info(f"downloaded new binaries: {new_binaries_str}")
return new_binaries
def upgrade_package(self, package):
self._run_pip(["install", "--upgrade", package])
def _run_pip(self, cmd):
cmd = [self.pip_path] + cmd
if not self.verbose:
cmd.append("-q")
_run(cmd)
def ensure_pipx_on_path(bin_dir, modify_path):
if which("pipx"):
echo("pipx is installed")
return
shell = os.environ.get("SHELL", "")
if "bash" in shell:
config_file = "~/.bashrc"
elif "zsh" in shell:
config_file = "~/.zshrc"
elif "fish" in shell:
config_file = "~/.config/fish/config.fish"
else:
config_file = None
if config_file:
config_file = os.path.expanduser(config_file)
if modify_path and config_file and os.path.exists(config_file):
with open(config_file, "a") as f:
f.write("\n# added by pipx (https://github.com/cs01/pipx)\n")
if "fish" in shell:
f.write("set -x PATH %s $PATH\n\n" % bin_dir)
else:
f.write('export PATH="%s:$PATH"\n' % bin_dir)
echo("Added %s to the PATH environment variable in %s" % (bin_dir, config_file))
echo("Open a new terminal to use pipx")
else:
echo(
textwrap.dedent(
"""
%(sep)s
Note:
To finish installation, %(bin_dir)s must be added to your PATH.
This can be done by adding the following line to your shell
config file:
export PATH=%(bin_dir)s:$PATH
%(sep)s
"""
% dict(sep="=" * 60, bin_dir=bin_dir)
)
)
def get_fs_package_name(package):
illegal = ["+", "#", "/", ":"]
ret = ""
for x in package:
if x in illegal:
ret += "_"
else:
ret += x
return ret
def install(pipx_local_venvs, package, local_bin_dir, pipx_symlink, python, verbose):
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 = venv.bin_path / 'pipx'
if not binary.is_file():
fail(f"Expected to find {str(binary)}")
if pipx_symlink.is_file():
if pipx_symlink.resolve().samefile(binary):
echo(f"re-using existing symlink at {str(pipx_symlink)}")
else:
pipx_symlink.unlink()
else:
pipx_symlink.symlink_to(binary)
def parse_options(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
"--src",
default="git+https://github.com/cs01/pipx.git",
help=(
"The specific version of pipx to install. This value is passed "
'to "pip install <value>". For example, to install from master '
"use `git+https://github.com/cs01/pipx.git`. "
"Default: %(default)s"
),
)
parser.add_argument(
"--no-modify-path",
action="store_true",
help="Don't configure the PATH environment variable",
)
parser.add_argument(
"--overwrite",
action="store_true",
help=("reinstall pipx even if existing installation was found"),
)
parser.add_argument(
"--python",
default="python3",
help=("The Python binary to associate pipx with. Must be v3.6+."),
)
parser.add_argument(
"--verbose", action="store_true", help=("Display more detailed output")
)
return parser.parse_args(argv)
def main(argv=sys.argv[1:]):
args = parse_options(argv)
if args.verbose:
logging.basicConfig(level=logging.INFO, format="%(message)s")
else:
logging.basicConfig(level=logging.WARNING, format="%(message)s")
pipx_local_venvs = os.environ.get("PIPX_HOME", DEFAULT_PIPX_HOME)
local_bin_dir = os.environ.get("PIPX_BIN_DIR", DEFAULT_PIPX_BIN_DIR)
pipx_symlink = local_bin_dir / "pipx"
if which("pipx"):
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():
echo("pipx is already installed but not on your PATH")
ensure_pipx_on_path(local_bin_dir, not args.no_modify_path)
succeed()
echo("Installing pipx")
install(
pipx_local_venvs,
args.src,
local_bin_dir,
pipx_symlink,
args.python,
args.verbose,
)
ensure_pipx_on_path(local_bin_dir, not args.no_modify_path)
if __name__ == "__main__":
main()