Skip to content

Commit

Permalink
Support installing osc into virtualenv
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Nov 3, 2023
1 parent 503cf10 commit d53b298
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,28 @@ jobs:
- name: 'Run installed osc'
run: |
osc --help
virtualenv:
name: 'virtualenv install test'
runs-on: 'ubuntu-latest'
strategy:
fail-fast: false

steps:
- name: 'Install packages'
run: |
sudo apt-get -y update
sudo apt-get -y --no-install-recommends install git python3-pip python3-rpm python3-virtualenv
- uses: actions/checkout@v3

- name: 'Initialize virtualenv'
run: |
python3 -m venv .env3
source .env3/bin/activate
pip3 install .
- name: 'Run installed osc'
run: |
source .env3/bin/activate
osc --help
3 changes: 3 additions & 0 deletions doc/plugins/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Extending osc with plugins
.. note::
New in osc 1.1.0

.. warning::
Plugins are currently NOT supported in virtualenv.


This is a simple tutorial.
More details can be found in the :py:class:`osc.commandline.OscCommand` reference.
Expand Down
3 changes: 3 additions & 0 deletions osc/_private/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def print_msg(*args, print_to="debug"):
elif print_to == "stdout":
# print the message to stdout
print(*args)
elif print_to == "stderr":

Check warning on line 20 in osc/_private/common.py

View check run for this annotation

Codecov / codecov/patch

osc/_private/common.py#L20

Added line #L20 was not covered by tests
# print the message to stderr
print(*args, file=sys.stderr)

Check warning on line 22 in osc/_private/common.py

View check run for this annotation

Codecov / codecov/patch

osc/_private/common.py#L22

Added line #L22 was not covered by tests
else:
raise ValueError(f"Invalid value of the 'print_to' option: {print_to}")

Expand Down
23 changes: 19 additions & 4 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
from .util.helper import _html_escape, format_table


# python3.6 requires reading sys.real_prefix to detect virtualenv
IN_VENV = getattr(sys, "real_prefix", sys.base_prefix) != sys.prefix


class Command:
#: Name of the command as used in the argument parser.
name: str = None
Expand Down Expand Up @@ -262,6 +266,9 @@ def load_command(self, cls, module_prefix):
return cmd

def load_commands(self):
if IN_VENV:
_private.print_msg("Running in virtual environment, skipping loading plugins installed outside the virtual environment.", print_to="stderr")

Check warning on line 270 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L270

Added line #L270 was not covered by tests

for module_prefix, module_path in self.MODULES:
module_path = os.path.expanduser(module_path)

Expand Down Expand Up @@ -317,12 +324,16 @@ class OscMainCommand(MainCommand):

MODULES = (
("osc.commands", osc_commands.__path__[0]),
("osc.commands.usr_lib", "/usr/lib/osc-plugins"),
("osc.commands.usr_local_lib", "/usr/local/lib/osc-plugins"),
("osc.commands.home_local_lib", "~/.local/lib/osc-plugins"),
("osc.commands.home", "~/.osc-plugins"),
)

if not IN_VENV:
MODULES += (
("osc.commands.usr_lib", "/usr/lib/osc-plugins"),
("osc.commands.usr_local_lib", "/usr/local/lib/osc-plugins"),
("osc.commands.home_local_lib", "~/.local/lib/osc-plugins"),
("osc.commands.home", "~/.osc-plugins"),
)

def __init__(self):
super().__init__()
self.args = None
Expand Down Expand Up @@ -10056,6 +10067,10 @@ def do_comment(self, subcmd, opts, *args):
print(result)

def _load_plugins(self):
if IN_VENV:
_private.print_msg("Running in virtual environment, skipping loading legacy plugins.", print_to="stderr")
return

Check warning on line 10072 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L10071-L10072

Added lines #L10071 - L10072 were not covered by tests

plugin_dirs = [
'/usr/lib/osc-plugins',
'/usr/local/lib/osc-plugins',
Expand Down

0 comments on commit d53b298

Please sign in to comment.