aboutsummaryrefslogtreecommitdiff
path: root/lib/python
diff options
context:
space:
mode:
authorZach White <skullydazed@gmail.com>2021-08-13 10:38:05 -0700
committerGitHub <noreply@github.com>2021-08-13 10:38:05 -0700
commit4742a3a48f7560ee4dd2b55d4ecad4cdf1d139cd (patch)
tree67ad48331b878911183be85d7973b88dd5db2dcc /lib/python
parent0c175d63cf35561c7a92e0bdeaef0ef185799aec (diff)
downloadqmk_firmware-4742a3a48f7560ee4dd2b55d4ecad4cdf1d139cd.tar.gz
qmk_firmware-4742a3a48f7560ee4dd2b55d4ecad4cdf1d139cd.zip
use sudo to install modules if needed (#13984)
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/qmk/cli/__init__.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 1e1c26671..de71a5d1e 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -65,6 +65,26 @@ subcommands = [
65] 65]
66 66
67 67
68def _install_deps(requirements):
69 """Perform the installation of missing requirements.
70
71 If we detect that we are running in a virtualenv we can't write into we'll use sudo to perform the pip install.
72 """
73 command = [sys.executable, '-m', 'pip', 'install']
74
75 if sys.prefix != sys.base_prefix:
76 # We are in a virtualenv, check to see if we need to use sudo to write to it
77 if not os.access(sys.prefix, os.W_OK):
78 print('Notice: Using sudo to install modules to location owned by root:', sys.prefix)
79 command.insert(0, 'sudo')
80
81 elif not os.access(sys.prefix, os.W_OK):
82 # We can't write to sys.prefix, attempt to install locally
83 command.append('--local')
84
85 return _run_cmd(*command, '-r', requirements)
86
87
68def _run_cmd(*command): 88def _run_cmd(*command):
69 """Run a command in a subshell. 89 """Run a command in a subshell.
70 """ 90 """
@@ -168,7 +188,7 @@ safe_command = args and args[0] in safe_commands
168if not safe_command: 188if not safe_command:
169 if _broken_module_imports('requirements.txt'): 189 if _broken_module_imports('requirements.txt'):
170 if yesno('Would you like to install the required Python modules?'): 190 if yesno('Would you like to install the required Python modules?'):
171 _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt') 191 _install_deps('requirements.txt')
172 else: 192 else:
173 print() 193 print()
174 print(msg_install % (str(Path('requirements.txt').resolve()),)) 194 print(msg_install % (str(Path('requirements.txt').resolve()),))
@@ -177,7 +197,7 @@ if not safe_command:
177 197
178 if cli.config.user.developer and _broken_module_imports('requirements-dev.txt'): 198 if cli.config.user.developer and _broken_module_imports('requirements-dev.txt'):
179 if yesno('Would you like to install the required developer Python modules?'): 199 if yesno('Would you like to install the required developer Python modules?'):
180 _run_cmd(sys.executable, '-m', 'pip', 'install', '-r', 'requirements-dev.txt') 200 _install_deps('requirements-dev.txt')
181 elif yesno('Would you like to disable developer mode?'): 201 elif yesno('Would you like to disable developer mode?'):
182 _run_cmd(sys.argv[0], 'config', 'user.developer=None') 202 _run_cmd(sys.argv[0], 'config', 'user.developer=None')
183 else: 203 else: