Skip to content

Commit

Permalink
sudo option added to venv pip commands (#104)
Browse files Browse the repository at this point in the history
* Fix pip virtualenv discovery for softwareupdates

* Use setup.py frm latest OctoPrint version.

* Enable pip install if using sudo.
  • Loading branch information
amrsoll authored May 5, 2021
1 parent ca3ba52 commit ec70720
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/octoprint/util/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def _setup_pip(self):
# in a virtual environment may we proceed with the --user parameter.

ok, pip_user, pip_virtual_env, pip_install_dir = self._check_pip_setup(pip_command)
if not ok:
if not ok and (not pip_sudo or not pip_install_dir):
self._logger.error("Cannot use pip")
return

Expand Down
54 changes: 33 additions & 21 deletions src/octoprint/util/piptestballoon/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import absolute_import, division, print_function
import os
import sys
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

"""
This "python package" doesn't actually install. This is intenional. It is merely
This "python package" doesn't actually install. This is intentional. It is merely
used to figure out some information about the environment a specific pip call
is running under (installation dir, whether it belongs to a virtual environment,
whether the install location is writable by the current user), and for that it
Expand All @@ -17,32 +16,45 @@
--verbose or --log flags.
"""

import io
import os
import sys


def produce_output(stream):
from distutils.command.install import install as cmd_install
from distutils.dist import Distribution
from distutils.command.install import install as cmd_install
from distutils.dist import Distribution

cmd = cmd_install(Distribution())
cmd.finalize_options()

install_dir = cmd.install_lib
virtual_env = hasattr(sys, "real_prefix") or (
hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix
)
writable = os.access(install_dir, os.W_OK)

cmd = cmd_install(Distribution())
cmd.finalize_options()
lines = [
"PIP_INSTALL_DIR={}".format(install_dir),
"PIP_VIRTUAL_ENV={}".format(virtual_env),
"PIP_WRITABLE={}".format(writable),
]

install_dir = cmd.install_lib
virtual_env = hasattr(sys, "real_prefix")
writable = os.access(install_dir, os.W_OK)
for line in lines:
print(line, file=stream)

print("PIP_INSTALL_DIR={}".format(install_dir), file=stream)
print("PIP_VIRTUAL_ENV={}".format(virtual_env), file=stream)
print("PIP_WRITABLE={}".format(writable), file=stream)
stream.flush()
stream.flush()


path = os.environ.get("TESTBALLOON_OUTPUT", None)
if path is not None:
# environment variable set, write to a log
path = os.path.abspath(path)
with open(path, mode="w+b") as output:
produce_output(output)
# environment variable set, write to a log
path = os.path.abspath(path)
with io.open(path, "wt+", encoding="utf-8") as output:
produce_output(output)
else:
# write to stdout
produce_output(sys.stdout)
# write to stdout
produce_output(sys.stdout)

# fail intentionally
sys.exit(-1)

0 comments on commit ec70720

Please sign in to comment.