Skip to content

Commit

Permalink
Added new import_module
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrubetskoy committed Mar 23, 2002
1 parent 09b7807 commit 7d7b547
Showing 1 changed file with 56 additions and 62 deletions.
118 changes: 56 additions & 62 deletions lib/python/mod_python/apache.py
Expand Up @@ -41,7 +41,7 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================
#
# $Id: apache.py,v 1.34 2001/05/25 03:27:31 gtrubetskoy Exp $
# $Id: apache.py,v 1.34.2.1 2002/03/23 16:48:41 gtrubetskoy Exp $

import sys
import string
Expand Down Expand Up @@ -286,87 +286,81 @@ def ReportError(self, req, etype, evalue, etb, htype="N/A", hname="N/A", debug=0
# we do not return anything

def import_module(module_name, req=None, path=None):
"""
"""
Get the module to handle the request. If
autoreload is on, then the module will be reloaded
if it has changed since the last import.
"""

# get the options
autoreload, debug = 1, None
# Get options
debug, autoreload = 0, 1
if req:
config = req.get_config()
debug = config.has_key("PythonDebug")
if config.has_key("PythonAutoReload"):
autoreload = int(config["PythonAutoReload"])
debug = config.has_key("PythonDebug")

# try to import the module

oldmtime = None
mtime = None

if not autoreload:

# import module
module = __import__(module_name)
components = string.split(module_name, '.')
for cmp in components[1:]:
module = getattr(module, cmp)

else:

# keep track of file modification time and
# try to reload it if it is newer
if sys.modules.has_key(module_name):

# the we won't even bother importing
module = sys.modules[module_name]

# does it have __mtime__ ?
if sys.modules[module_name].__dict__.has_key("__mtime__"):
# remember it
oldmtime = sys.modules[ module_name ].__mtime__

# (Re)import
if sys.modules.has_key(module_name):

# The module has been imported already
module = sys.modules[module_name]

# import the module for the first time
if autoreload:
oldmtime = module.__dict__.get("__mtime__", 0)
mtime = module_mtime(module)
else:
mtime, oldmtime = 0, 0

parts = string.split(module_name, '.')
for i in range(len(parts)):
f, p, d = imp.find_module(parts[i], path)
try:
mname = string.join(parts[:i+1], ".")
module = imp.load_module(mname, f, p, d)
finally:
if f: f.close()
if hasattr(module, "__path__"):
path = module.__path__
else:
mtime, oldmtime = 0, -1

if mtime > oldmtime:

# find out the last modification time
# but only if there is a __file__ attr
if module.__dict__.has_key("__file__"):
# Import the module
if debug:
s = 'mod_python: (Re)importing %s from %s' % (module_name, path)
_apache.log_error(s, APLOG_NOERRNO|APLOG_NOTICE)

filepath = module.__file__
parts = string.split(module_name, '.')
for i in range(len(parts)):
f, p, d = imp.find_module(parts[i], path)
try:
mname = string.join(parts[:i+1], ".")
module = imp.load_module(mname, f, p, d)
finally:
if f: f.close()
if hasattr(module, "__path__"):
path = module.__path__

if os.path.exists(filepath):
if mtime == 0:
mtime = module_mtime(module)

mod = os.stat(filepath)
mtime = mod[stat.ST_MTIME]
module.__mtime__ = mtime

return module

# check also .py and take the newest
if os.path.exists(filepath[:-1]) :
def module_mtime(module):
"""Get modification time of module"""
mtime = 0
if module.__dict__.has_key("__file__"):

filepath = module.__file__

try:
# this try/except block is a workaround for a Python bug in
# 2.0, 2.1 and 2.1.1. See
# http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=422004

# get the time of the .py file
mod = os.stat(filepath[:-1])
mtime = max(mtime, mod[stat.ST_MTIME])
if os.path.exists(filepath):
mtime = os.path.getmtime(filepath)

# if module is newer - reload
if (autoreload and (oldmtime < mtime)):
module = reload(module)
if os.path.exists(filepath[:-1]) :
mtime = max(mtime, os.path.getmtime(filepath[:-1]))

# save mtime
module.__mtime__ = mtime
except OSError: pass

return module
return mtime

def resolve_object(req, module, object_str, silent=0):
"""
Expand Down

0 comments on commit 7d7b547

Please sign in to comment.