put back configuration file without py extension and deprecate it

this change put back (and fix it) support of configuration files without
ython extension and warn about its usage.
This commit is contained in:
benoitc 2019-11-25 21:07:35 +01:00 committed by Benoit Chesneau
parent 9538358511
commit 9a3e008eca

View File

@ -3,6 +3,7 @@
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
import importlib.util
import importlib.machinery
import os
import sys
import traceback
@ -94,9 +95,17 @@ class Application(BaseApplication):
if not os.path.exists(filename):
raise RuntimeError("%r doesn't exist" % filename)
ext = os.path.splitext(filename)[1]
try:
module_name = '__config__'
spec = importlib.util.spec_from_file_location(module_name, filename)
if ext in [".py", ".pyc"]:
spec = importlib.util.spec_from_file_location(module_name, filename)
else:
msg = "configuration file should have a valid Python extension.\n"
util.warn(msg)
loader_ = importlib.machinery.SourceFileLoader(module_name, filename)
spec = importlib.util.spec_from_file_location(module_name, filename, loader=loader_)
mod = importlib.util.module_from_spec(spec)
sys.modules[module_name] = mod
spec.loader.exec_module(mod)