Skip to content

Fix p* scripts to produce an error when given a bad config_uri #3766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
os:
- "ubuntu-20.04"
- "windows-2022"
- "macos-11"
- "macos-12"
architecture:
- x64
- x86
Expand All @@ -41,7 +41,7 @@ jobs:
# Linux and macOS don't have x86 python
- os: "ubuntu-20.04"
architecture: x86
- os: "macos-11"
- os: "macos-12"
architecture: x86

name: "Python: ${{ matrix.py }}-${{ matrix.architecture }} on ${{ matrix.os }}"
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ Bug Fixes

See https://github.com/Pylons/pyramid/pull/3742

- When a bad settings URI (usually a path to a non-existant file) is
supplied to one of pyramid's command line programs, and in general
when there is a reason why a settings file cannot be used, produce
an error message instead of a traceback.

Applications which import pyramid.paster must handle the
plaster.exceptions.PlasterError exception themselves.

Backward Incompatibilities
--------------------------

Expand Down
11 changes: 10 additions & 1 deletion docs/narr/commandline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,16 @@ your application and tries to run it, there just is no request data, because
there isn't any real web request. Therefore some parts of your application and
some Pyramid APIs will not work.

For this reason, :app:`Pyramid` makes it possible to run a script in an
To assist with script writing :app:`Pyramid` provides the
:mod:`paster <pyramid.paster>` module.

.. versionchanged:: 2.0
Users of :mod:`paster <pyramid.paster>` are expected to themselves
handle any exceptions that might be raised by the :term:`plaster`
library, reporting to the user any problems (non-existant files,
etc.) with obtaining settings.

:app:`Pyramid` makes it possible to run a script in an
environment much like the environment produced when a particular
:term:`request` reaches your :app:`Pyramid` application. This is achieved by
using the :func:`pyramid.paster.bootstrap` command in the body of your script.
Expand Down
24 changes: 20 additions & 4 deletions src/pyramid/scripts/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import plaster
import plaster.exceptions
import sys


def parse_vars(args):
Expand All @@ -15,9 +17,23 @@ def parse_vars(args):
return result


def get_config_loader(config_uri):
"""
Find a ``plaster.ILoader`` object supporting the "wsgi" protocol.
def get_config_loader(config_uri, report_failure=None):
"""Find a ``plaster.ILoader`` object supporting the "wsgi" protocol.

``report_failure``, if passed, must be a function of one argument,
a string. The function is called with an error message when the
``config_uri`` cannot be used to obtain settings. After
``report_failure`` is called the program exits with a ``1``
(failure) status code.

When ``report_failure`` is not passed, or is None, the caller is expected
to handle PlasterError exceptions raised by :term:`plaster`.

"""
return plaster.get_loader(config_uri, protocols=['wsgi'])
try:
return plaster.get_loader(config_uri, protocols=['wsgi'])
except plaster.exceptions.PlasterError as e:
if not report_failure:
raise e
report_failure(f'The settings given are not available: {e}')
sys.exit(1)
2 changes: 1 addition & 1 deletion src/pyramid/scripts/prequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def run(self):
config_vars.setdefault('__script__', self.script_name)
path = self.args.path_info

loader = self._get_config_loader(config_uri)
loader = self._get_config_loader(config_uri, self.out)
loader.setup_logging(config_vars)

app = loader.get_wsgi_app(self.args.app_name, config_vars)
Expand Down
2 changes: 1 addition & 1 deletion src/pyramid/scripts/proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def run(self, quiet=False):
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
loader = self.get_config_loader(config_uri)
loader = self.get_config_loader(config_uri, self.out)
loader.setup_logging(config_vars)
self.proutes_file_config(loader, config_vars)

Expand Down
2 changes: 1 addition & 1 deletion src/pyramid/scripts/pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def run(self): # pragma: no cover
app_spec = self.args.config_uri
app_name = self.args.app_name

loader = self._get_config_loader(config_uri)
loader = self._get_config_loader(config_uri, self.out)

# setup logging only in the worker process incase the logging config
# opens files which should not be opened by multiple processes at once
Expand Down
2 changes: 1 addition & 1 deletion src/pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def run(self, shell=None):
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
loader = self.get_config_loader(config_uri)
loader = self.get_config_loader(config_uri, self.out)
loader.setup_logging(config_vars)
self.pshell_file_config(loader, config_vars)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_scripts/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(
self.server = server
self.calls = []

def __call__(self, uri):
def __call__(self, uri, out=None):
import plaster

self.uri = plaster.parse_uri(uri)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_scripts/test_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from plaster.exceptions import PlasterError
import pytest
import unittest

import pyramid.scripts.common


class TestParseVars(unittest.TestCase):
def test_parse_vars_good(self):
Expand All @@ -14,3 +18,22 @@ def test_parse_vars_bad(self):

vars = ['a']
self.assertRaises(ValueError, parse_vars, vars)


def test_get_config_loader_raises():
with pytest.raises(PlasterError):
pyramid.scripts.common.get_config_loader('invalidscheme:/foo')


def test_get_config_loader_calls():
def reporter(text):
nonlocal reporter_called
reporter_called = True

reporter_called = False
with pytest.raises(SystemExit):
pyramid.scripts.common.get_config_loader(
'invalidscheme:/foo', reporter
)

assert reporter_called is True