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
parent d31ac1df83
commit 05515873ea

View File

@ -3,6 +3,7 @@
# This file is part of gunicorn released under the MIT license. # This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information. # See the NOTICE for more information.
import importlib.util import importlib.util
import importlib.machinery
import os import os
import sys import sys
import traceback import traceback
@ -94,9 +95,17 @@ class Application(BaseApplication):
if not os.path.exists(filename): if not os.path.exists(filename):
raise RuntimeError("%r doesn't exist" % filename) raise RuntimeError("%r doesn't exist" % filename)
ext = os.path.splitext(filename)[1]
try: try:
module_name = '__config__' 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) mod = importlib.util.module_from_spec(spec)
sys.modules[module_name] = mod sys.modules[module_name] = mod
spec.loader.exec_module(mod) spec.loader.exec_module(mod)