Replace demandload with new demandimport
authorMatt Mackall <mpm@selenic.com>
Wed, 13 Dec 2006 13:27:09 -0600
changeset 3877 abaee83ce0a6
parent 3876 1e0b94cfba0e
child 3887 57b797601b61
child 3895 79e5a6e7c451
Replace demandload with new demandimport
contrib/churn.py
hg
hgext/acl.py
hgext/bugzilla.py
hgext/extdiff.py
hgext/fetch.py
hgext/hbisect.py
hgext/hgk.py
hgext/mq.py
hgext/notify.py
hgext/patchbomb.py
hgext/transplant.py
mercurial/appendfile.py
mercurial/archival.py
mercurial/bundlerepo.py
mercurial/changegroup.py
mercurial/changelog.py
mercurial/cmdutil.py
mercurial/commands.py
mercurial/context.py
mercurial/demandimport.py
mercurial/demandload.py
mercurial/dirstate.py
mercurial/filelog.py
mercurial/hg.py
mercurial/hgweb/__init__.py
mercurial/hgweb/common.py
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/hgwebdir_mod.py
mercurial/hgweb/request.py
mercurial/hgweb/server.py
mercurial/httprepo.py
mercurial/localrepo.py
mercurial/lock.py
mercurial/mail.py
mercurial/manifest.py
mercurial/mdiff.py
mercurial/merge.py
mercurial/node.py
mercurial/packagescan.py
mercurial/patch.py
mercurial/revlog.py
mercurial/sshrepo.py
mercurial/sshserver.py
mercurial/statichttprepo.py
mercurial/streamclone.py
mercurial/templater.py
mercurial/transaction.py
mercurial/ui.py
mercurial/util.py
mercurial/util_win32.py
setup.py
--- a/contrib/churn.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/contrib/churn.py	Wed Dec 13 13:27:09 2006 -0600
@@ -11,10 +11,9 @@
 #
 # <alias email> <actual email>
 
-from mercurial.demandload import *
+import time, sys, signal, os
 from mercurial.i18n import gettext as _
-demandload(globals(), 'time sys signal os')
-demandload(globals(), 'mercurial:hg,mdiff,fancyopts,cmdutil,ui,util,templater,node')
+from mercurial import hg, mdiff, cmdutil, ui, util, templater, node
 
 def __gather(ui, repo, node1, node2):
     def dirtywork(f, mmap1, mmap2):
--- a/hg	Tue Dec 12 18:16:23 2006 -0600
+++ b/hg	Wed Dec 13 13:27:09 2006 -0600
@@ -7,6 +7,5 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial import commands
-
-commands.run()
+import mercurial.commands
+mercurial.commands.run()
--- a/hgext/acl.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/acl.py	Wed Dec 13 13:27:09 2006 -0600
@@ -45,10 +45,10 @@
 #   glob pattern = user4, user5
 #   ** = user6
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
 from mercurial.node import *
-demandload(globals(), 'getpass mercurial:util')
+from mercurial import util
+import getpass
 
 class checker(object):
     '''acl checker.'''
--- a/hgext/bugzilla.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/bugzilla.py	Wed Dec 13 13:27:09 2006 -0600
@@ -52,10 +52,10 @@
 #   [usermap]
 #   committer_email = bugzilla_user_name
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
 from mercurial.node import *
-demandload(globals(), 'mercurial:cmdutil,templater,util os re time')
+from mercurial import cmdutil, templater, util
+import os, re, time
 
 MySQLdb = None
 
--- a/hgext/extdiff.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/extdiff.py	Wed Dec 13 13:27:09 2006 -0600
@@ -48,10 +48,10 @@
 # needed files, so running the external diff program will actually be
 # pretty fast (at least faster than having to compare the entire tree).
 
-from mercurial.demandload import demandload
 from mercurial.i18n import gettext as _
 from mercurial.node import *
-demandload(globals(), 'mercurial:cmdutil,util os shutil tempfile')
+from mercurial import cmdutil, util
+import os, shutil, tempfile
 
 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
     def snapshot_node(files, node):
--- a/hgext/fetch.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/fetch.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,10 +5,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
 from mercurial.node import *
-demandload(globals(), 'mercurial:commands,hg,node,util')
+from mercurial import commands, hg, node, util
 
 def fetch(ui, repo, source='default', **opts):
     '''Pull changes from a remote repository, merge new changes if needed.
--- a/hgext/hbisect.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/hbisect.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,8 +7,8 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from mercurial.i18n import gettext as _
-from mercurial.demandload import demandload
-demandload(globals(), "os sys sets mercurial:hg,util,commands,cmdutil")
+from mercurial import hg, util, commands, cmdutil
+import os, sys, sets
 
 versionstr = "0.0.3"
 
--- a/hgext/hgk.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/hgk.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,9 +5,8 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import *
-demandload(globals(), 'time sys signal os')
-demandload(globals(), 'mercurial:hg,fancyopts,commands,ui,util,patch,revlog')
+import time, sys, signal, os
+from mercurial import hg, fancyopts, commands, ui, util, patch, revlog
 
 def difftree(ui, repo, node1=None, node2=None, *files, **opts):
     """diff trees from two commits"""
--- a/hgext/mq.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/mq.py	Wed Dec 13 13:27:09 2006 -0600
@@ -29,11 +29,9 @@
 refresh contents of top applied patch     qrefresh
 '''
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
-from mercurial import commands
-demandload(globals(), "os sys re struct traceback errno bz2")
-demandload(globals(), "mercurial:cmdutil,hg,patch,revlog,util,changegroup")
+from mercurial import commands, cmdutil, hg, patch, revlog, util, changegroup
+import os, sys, re, struct, traceback, errno, bz2
 
 commands.norepo += " qclone qversion"
 
--- a/hgext/notify.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/notify.py	Wed Dec 13 13:27:09 2006 -0600
@@ -65,11 +65,10 @@
 # if you like, you can put notify config file in repo that users can
 # push changes to, they can manage their own subscriptions.
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
 from mercurial.node import *
-demandload(globals(), 'mercurial:patch,cmdutil,templater,util,mail')
-demandload(globals(), 'email.Parser fnmatch socket time')
+from mercurial import patch, cmdutil, templater, util, mail
+import email.Parser, fnmatch, socket, time
 
 # template for single changeset can include email headers.
 single_template = '''
--- a/hgext/patchbomb.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/patchbomb.py	Wed Dec 13 13:27:09 2006 -0600
@@ -63,10 +63,9 @@
 #
 # That should be all.  Now your patchbomb is on its way out.
 
-from mercurial.demandload import *
-demandload(globals(), '''email.MIMEMultipart email.MIMEText email.Utils
-                         mercurial:cmdutil,commands,hg,mail,ui,patch
-                         os errno popen2 socket sys tempfile time''')
+import os, errno, popen2, socket, sys, tempfile, time
+import email.MIMEMultipart, email.MIMEText, email.Utils
+from mercurial import cmdutil, commands, hg, mail, ui, patch
 from mercurial.i18n import gettext as _
 from mercurial.node import *
 
--- a/hgext/transplant.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/hgext/transplant.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,11 +5,10 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import *
 from mercurial.i18n import gettext as _
-demandload(globals(), 'os tempfile')
-demandload(globals(), 'mercurial:bundlerepo,cmdutil,commands,hg,merge,patch')
-demandload(globals(), 'mercurial:revlog,util')
+import os, tempfile
+from mercurial import bundlerepo, cmdutil, commands, hg, merge, patch, revlog
+from mercurial import util
 
 '''patch transplanting tool
 
--- a/mercurial/appendfile.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/appendfile.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,8 +5,7 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import *
-demandload(globals(), "cStringIO changelog errno manifest os tempfile util")
+import cStringIO, changelog, errno, manifest, os, tempfile, util
 
 # writes to metadata files are ordered.  reads: changelog, manifest,
 # normal files.  writes: normal files, manifest, changelog.
--- a/mercurial/archival.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/archival.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,10 +5,9 @@
 # This software may be used and distributed according to the terms of
 # the GNU General Public License, incorporated herein by reference.
 
-from demandload import *
 from i18n import gettext as _
 from node import *
-demandload(globals(), 'cStringIO os stat tarfile time util zipfile')
+import cStringIO, os, stat, tarfile, time, util, zipfile
 
 def tidyprefix(dest, prefix, suffixes):
     '''choose prefix to use for names in archive.  make sure prefix is
--- a/mercurial/bundlerepo.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/bundlerepo.py	Wed Dec 13 13:27:09 2006 -0600
@@ -12,8 +12,7 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import demandload
-demandload(globals(), "changegroup util os struct bz2 tempfile")
+import changegroup, util, os, struct, bz2, tempfile
 
 import localrepo, changelog, manifest, filelog, revlog
 
--- a/mercurial/changegroup.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/changegroup.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,9 +6,9 @@
 This software may be used and distributed according to the terms
 of the GNU General Public License, incorporated herein by reference.
 """
+
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "struct os bz2 zlib util tempfile")
+import struct, os, bz2, zlib, util, tempfile
 
 def getchunk(source):
     """get a chunk from a changegroup"""
--- a/mercurial/changelog.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/changelog.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,8 +7,7 @@
 
 from revlog import *
 from i18n import gettext as _
-from demandload import demandload
-demandload(globals(), "os time util")
+import os, time, util
 
 def _string_escape(text):
     """
--- a/mercurial/cmdutil.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/cmdutil.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,11 +5,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from node import *
 from i18n import gettext as _
-demandload(globals(), 'os sys')
-demandload(globals(), 'mdiff util templater patch')
+import os, sys, mdiff, util, templater, patch
 
 revrangesep = ':'
 
--- a/mercurial/commands.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/commands.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,14 +5,14 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
+import demandimport; demandimport.enable()
 from node import *
 from i18n import gettext as _
-demandload(globals(), "bisect os re sys signal imp urllib pdb shlex stat")
-demandload(globals(), "fancyopts ui hg util lock revlog bundlerepo")
-demandload(globals(), "difflib patch time help mdiff tempfile")
-demandload(globals(), "traceback errno version atexit")
-demandload(globals(), "archival changegroup cmdutil hgweb.server sshserver")
+import bisect, os, re, sys, signal, imp, urllib, pdb, shlex, stat
+import fancyopts, ui, hg, util, lock, revlog, bundlerepo
+import difflib, patch, time, help, mdiff, tempfile
+import traceback, errno, version, atexit
+import archival, changegroup, cmdutil, hgweb.server, sshserver
 
 class UnknownCommand(Exception):
     """Exception raised if command is not in the command table."""
--- a/mercurial/context.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/context.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,8 +7,7 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import demandload
-demandload(globals(), "ancestor bdiff repo revlog util os")
+import ancestor, bdiff, repo, revlog, util, os
 
 class changectx(object):
     """A changecontext object makes access to data related to a particular
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/demandimport.py	Wed Dec 13 13:27:09 2006 -0600
@@ -0,0 +1,104 @@
+# demandimport.py - global demand-loading of modules for Mercurial
+#
+# Copyright 2006 Matt Mackall <mpm@selenic.com>
+#
+# This software may be used and distributed according to the terms
+# of the GNU General Public License, incorporated herein by reference.
+
+'''
+demandimport - automatic demandloading of modules
+
+To enable this module, do:
+
+  import demandimport; demandimport.enable()
+
+Imports of the following forms will be demand-loaded:
+
+  import a, b.c
+  import a.b as c
+  from a import b,c # a will be loaded immediately
+
+These imports will not be delayed:
+
+  from a import *
+  b = __import__(a)
+'''
+
+_origimport = __import__
+
+class _demandmod(object):
+    """module demand-loader and proxy"""
+    def __init__(self, name, globals, locals):
+        if '.' in name:
+            head, rest = name.split('.', 1)
+            after = [rest]
+        else:
+            head = name
+            after = []
+        self.__dict__["_data"] = (head, globals, locals, after)
+        self.__dict__["_module"] = None
+    def _extend(self, name):
+        """add to the list of submodules to load"""
+        self._data[3].append(name)
+    def _load(self):
+        if not self._module:
+            head, globals, locals, after = self._data
+            mod = _origimport(head, globals, locals)
+            # load submodules
+            for x in after:
+                hx = x
+                if '.' in x:
+                    hx = x.split('.')[0]
+                if not hasattr(mod, hx):
+                    setattr(mod, hx, _demandmod(x, mod.__dict__, mod.__dict__))
+            # are we in the locals dictionary still?
+            if locals and locals.get(head) == self:
+                locals[head] = mod
+            self.__dict__["_module"] = mod
+    def __repr__(self):
+        return "<unloaded module '%s'>" % self._data[0]
+    def __call__(self, *args, **kwargs):
+        raise TypeError("'unloaded module' object is not callable")
+    def __getattr__(self, attr):
+        self._load()
+        return getattr(self._module, attr)
+    def __setattr__(self, attr, val):
+        self._load()
+        setattr(self._module, attr, val)
+
+def _demandimport(name, globals=None, locals=None, fromlist=None):
+    if not locals or name in ignore or fromlist == ('*',):
+        # these cases we can't really delay
+        return _origimport(name, globals, locals, fromlist)
+    elif not fromlist:
+        # import a [as b]
+        if '.' in name: # a.b
+            base, rest = name.split('.', 1)
+            # if a is already demand-loaded, add b to its submodule list
+            if base in locals:
+                if isinstance(locals[base], _demandmod):
+                    locals[base]._extend(rest)
+                return locals[base]
+        return _demandmod(name, globals, locals)
+    else:
+        # from a import b,c,d
+        mod = _origimport(name, globals, locals)
+        # recurse down the module chain
+        for comp in name.split('.')[1:]:
+            mod = getattr(mod, comp)
+        for x in fromlist:
+            # set requested submodules for demand load
+            if not(hasattr(mod, x)):
+                setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__))
+        return mod
+
+ignore = []
+
+def enable():
+    "enable global demand-loading of modules"
+    __builtins__["__import__"] = _demandimport
+
+def disable():
+    "disable global demand-loading of modules"
+    __builtins__["__import__"] = _origimport
+
--- a/mercurial/demandload.py	Tue Dec 12 18:16:23 2006 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,135 +0,0 @@
-'''Demand load modules when used, not when imported.'''
-
-__author__ = '''Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>.
-This software may be used and distributed according to the terms
-of the GNU General Public License, incorporated herein by reference.'''
-
-# this is based on matt's original demandload module.  it is a
-# complete rewrite.  some time, we may need to support syntax of
-# "import foo as bar".
-
-class _importer(object):
-    '''import a module.  it is not imported until needed, and is
-    imported at most once per scope.'''
-
-    def __init__(self, scope, modname, fromlist):
-        '''scope is context (globals() or locals()) in which import
-        should be made.  modname is name of module to import.
-        fromlist is list of modules for "from foo import ..."
-        emulation.'''
-
-        self.scope = scope
-        self.modname = modname
-        self.fromlist = fromlist
-        self.mod = None
-
-    def module(self):
-        '''import the module if needed, and return.'''
-        if self.mod is None:
-            self.mod = __import__(self.modname, self.scope, self.scope,
-                                  self.fromlist)
-            del self.modname, self.fromlist
-        return self.mod
-
-class _replacer(object):
-    '''placeholder for a demand loaded module. demandload puts this in
-    a target scope.  when an attribute of this object is looked up,
-    this object is replaced in the target scope with the actual
-    module.
-
-    we use __getattribute__ to avoid namespace clashes between
-    placeholder object and real module.'''
-
-    def __init__(self, importer, target):
-        self.importer = importer
-        self.target = target
-        # consider case where we do this:
-        #   demandload(globals(), 'foo.bar foo.quux')
-        # foo will already exist in target scope when we get to
-        # foo.quux.  so we remember that we will need to demandload
-        # quux into foo's scope when we really load it.
-        self.later = []
-
-    def module(self):
-        return object.__getattribute__(self, 'importer').module()
-
-    def __getattribute__(self, key):
-        '''look up an attribute in a module and return it. replace the
-        name of the module in the caller\'s dict with the actual
-        module.'''
-
-        module = object.__getattribute__(self, 'module')()
-        target = object.__getattribute__(self, 'target')
-        importer = object.__getattribute__(self, 'importer')
-        later = object.__getattribute__(self, 'later')
-
-        if later:
-            demandload(module.__dict__, ' '.join(later))
-
-        importer.scope[target] = module
-
-        return getattr(module, key)
-
-class _replacer_from(_replacer):
-    '''placeholder for a demand loaded module.  used for "from foo
-    import ..." emulation. semantics of this are different than
-    regular import, so different implementation needed.'''
-
-    def module(self):
-        importer = object.__getattribute__(self, 'importer')
-        target = object.__getattribute__(self, 'target')
-
-        return getattr(importer.module(), target)
-
-    def __call__(self, *args, **kwargs):
-        target = object.__getattribute__(self, 'module')()
-        return target(*args, **kwargs)
-
-def demandload(scope, modules):
-    '''import modules into scope when each is first used.
-
-    scope should be the value of globals() in the module calling this
-    function, or locals() in the calling function.
-
-    modules is a string listing module names, separated by white
-    space.  names are handled like this:
-
-    foo            import foo
-    foo bar        import foo, bar
-    foo@bar        import foo as bar
-    foo.bar        import foo.bar
-    foo:bar        from foo import bar
-    foo:bar,quux   from foo import bar, quux
-    foo.bar:quux   from foo.bar import quux'''
-
-    for mod in modules.split():
-        col = mod.find(':')
-        if col >= 0:
-            fromlist = mod[col+1:].split(',')
-            mod = mod[:col]
-        else:
-            fromlist = []
-        as_ = None
-        if '@' in mod:
-            mod, as_ = mod.split("@")
-        importer = _importer(scope, mod, fromlist)
-        if fromlist:
-            for name in fromlist:
-                scope[name] = _replacer_from(importer, name)
-        else:
-            dot = mod.find('.')
-            if dot >= 0:
-                basemod = mod[:dot]
-                val = scope.get(basemod)
-                # if base module has already been demandload()ed,
-                # remember to load this submodule into its namespace
-                # when needed.
-                if isinstance(val, _replacer):
-                    later = object.__getattribute__(val, 'later')
-                    later.append(mod[dot+1:])
-                    continue
-            else:
-                basemod = mod
-            if not as_:
-                as_ = basemod
-            scope[as_] = _replacer(importer, as_)
--- a/mercurial/dirstate.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/dirstate.py	Wed Dec 13 13:27:09 2006 -0600
@@ -9,8 +9,7 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "struct os time bisect stat strutil util re errno")
+import struct, os, time, bisect, stat, strutil, util, re, errno
 
 class dirstate(object):
     format = ">cllll"
--- a/mercurial/filelog.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/filelog.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,8 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from revlog import *
-from demandload import *
-demandload(globals(), "os")
+import os
 
 class filelog(revlog):
     def __init__(self, opener, path, defversion=REVLOG_DEFAULT_VERSION):
--- a/mercurial/hg.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hg.py	Wed Dec 13 13:27:09 2006 -0600
@@ -8,10 +8,11 @@
 
 from node import *
 from repo import *
-from demandload import *
 from i18n import gettext as _
-demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo")
-demandload(globals(), "errno lock os shutil util merge@_merge verify@_verify")
+import localrepo, bundlerepo, httprepo, sshrepo, statichttprepo
+import errno, lock, os, shutil, util
+import merge as _merge
+import verify as _verify
 
 def _local(path):
     return (os.path.isfile(util.drop_scheme('file', path)) and
--- a/mercurial/hgweb/__init__.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/__init__.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,6 +6,11 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import demandload
-demandload(globals(), "mercurial.hgweb.hgweb_mod:hgweb")
-demandload(globals(), "mercurial.hgweb.hgwebdir_mod:hgwebdir")
+import hgweb_mod, hgwebdir_mod
+
+def hgweb(*args, **kwargs):
+    return hgweb_mod.hgweb(*args, **kwargs)
+
+def hgwebdir(*args, **kwargs):
+    return hgwebdir_mod.hgwebdir(*args, **kwargs)
+
--- a/mercurial/hgweb/common.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/common.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,7 +7,6 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 import os, mimetypes
-import os.path
 
 def get_mtime(repo_path):
     store_path = os.path.join(repo_path, ".hg")
--- a/mercurial/hgweb/hgweb_mod.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/hgweb_mod.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,17 +6,13 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import os
-import os.path
-import mimetypes
-from mercurial.demandload import demandload
-demandload(globals(), "re zlib ConfigParser mimetools cStringIO sys tempfile")
-demandload(globals(), 'urllib bz2')
-demandload(globals(), "mercurial:mdiff,ui,hg,util,archival,streamclone,patch")
-demandload(globals(), "mercurial:revlog,templater")
-demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile,style_map")
+import os, mimetypes, re, zlib, ConfigParser, mimetools, cStringIO, sys
+import tempfile, urllib, bz2
 from mercurial.node import *
 from mercurial.i18n import gettext as _
+from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
+from mercurial import revlog, templater
+from common import get_mtime, staticfile, style_map
 
 def _up(p):
     if p[0] != "/":
--- a/mercurial/hgweb/hgwebdir_mod.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/hgwebdir_mod.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,13 +6,12 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import os
-from mercurial.demandload import demandload
-demandload(globals(), "mimetools cStringIO")
-demandload(globals(), "mercurial:ui,hg,util,templater")
-demandload(globals(), "mercurial.hgweb.hgweb_mod:hgweb")
-demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile,style_map")
+from mercurial import demandimport; demandimport.enable()
+import os, mimetools, cStringIO
 from mercurial.i18n import gettext as _
+from mercurial import ui, hg, util, templater
+from common import get_mtime, staticfile, style_map
+from hgweb_mod import hgweb
 
 # This is a stopgap
 class hgwebdir(object):
--- a/mercurial/hgweb/request.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/request.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,8 +6,7 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import demandload
-demandload(globals(), "socket sys cgi os errno")
+import socket, sys, cgi, os, errno
 from mercurial.i18n import gettext as _
 
 class wsgiapplication(object):
--- a/mercurial/hgweb/server.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/hgweb/server.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,11 +6,11 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from mercurial.demandload import demandload
-import os, sys, errno
-demandload(globals(), "urllib BaseHTTPServer socket SocketServer")
-demandload(globals(), "mercurial:ui,hg,util,templater")
-demandload(globals(), "hgweb_mod:hgweb hgwebdir_mod:hgwebdir request:wsgiapplication")
+import os, sys, errno, urllib, BaseHTTPServer, socket, SocketServer
+from mercurial import ui, hg, util, templater
+from hgweb_mod import hgweb
+from hgwebdir_mod import hgwebdir
+from request import wsgiapplication
 from mercurial.i18n import gettext as _
 
 def _splitURI(uri):
--- a/mercurial/httprepo.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/httprepo.py	Wed Dec 13 13:27:09 2006 -0600
@@ -9,9 +9,8 @@
 from node import *
 from remoterepo import *
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
-demandload(globals(), "errno keepalive tempfile socket changegroup")
+import hg, os, urllib, urllib2, urlparse, zlib, util, httplib
+import errno, keepalive, tempfile, socket, changegroup
 
 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
     def __init__(self, ui):
--- a/mercurial/localrepo.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/localrepo.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,12 +7,10 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import *
-import repo
-demandload(globals(), "appendfile changegroup")
-demandload(globals(), "changelog dirstate filelog manifest context")
-demandload(globals(), "re lock transaction tempfile stat mdiff errno ui")
-demandload(globals(), "os revlog time util")
+import repo, appendfile, changegroup
+import changelog, dirstate, filelog, manifest, context
+import re, lock, transaction, tempfile, stat, mdiff, errno, ui
+import os, revlog, time, util
 
 class localrepository(repo.repository):
     capabilities = ('lookup', 'changegroupsubset')
--- a/mercurial/lock.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/lock.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,8 +5,7 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import *
-demandload(globals(), 'errno os socket time util')
+import errno, os, socket, time, util
 
 class LockException(IOError):
     def __init__(self, errno, strerror, filename, desc):
--- a/mercurial/mail.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/mail.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,8 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "os re smtplib templater util")
+import os, re, smtplib, templater, util
 
 def _smtp(ui):
     '''send mail using smtp.'''
--- a/mercurial/manifest.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/manifest.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,9 +7,7 @@
 
 from revlog import *
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "array bisect struct")
-demandload(globals(), "mdiff")
+import array, bisect, struct, mdiff
 
 class manifestdict(dict):
     def __init__(self, mapping=None, flags=None):
--- a/mercurial/mdiff.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/mdiff.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,9 +5,7 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
-import bdiff, mpatch
-demandload(globals(), "re struct util")
+import bdiff, mpatch, re, struct, util
 
 def splitnewlines(text):
     '''like str.splitlines, but only split on newlines.'''
--- a/mercurial/merge.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/merge.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,8 +7,7 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "errno util os tempfile")
+import errno, util, os, tempfile
 
 def filemerge(repo, fw, fo, wctx, mctx):
     """perform a 3-way merge in the working directory
--- a/mercurial/node.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/node.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,8 +7,7 @@
 of the GNU General Public License, incorporated herein by reference.
 """
 
-from demandload import demandload
-demandload(globals(), "binascii")
+import binascii
 
 nullrev = -1
 nullid = "\0" * 20
--- a/mercurial/packagescan.py	Tue Dec 12 18:16:23 2006 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,131 +0,0 @@
-# packagescan.py - Helper module for identifing used modules.
-# Used for the py2exe distutil.
-# This module must be the first mercurial module imported in setup.py
-#
-# Copyright 2005, 2006 Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
-#
-# This software may be used and distributed according to the terms
-# of the GNU General Public License, incorporated herein by reference.
-import glob
-import os
-import sys
-import ihooks
-import types
-import string
-
-# Install this module as fake demandload module
-sys.modules['mercurial.demandload'] = sys.modules[__name__]
-
-# Requiredmodules contains the modules imported by demandload.
-# Please note that demandload can be invoked before the
-# mercurial.packagescan.scan method is invoked in case a mercurial
-# module is imported.
-requiredmodules = {}
-def demandload(scope, modules):
-    """ fake demandload function that collects the required modules
-        foo            import foo
-        foo bar        import foo, bar
-        foo.bar        import foo.bar
-        foo@bar        import foo as bar
-        foo:bar        from foo import bar
-        foo:bar,quux   from foo import bar, quux
-        foo.bar:quux   from foo.bar import quux"""
-
-    for m in modules.split():
-        mod = None
-        try:
-            module, fromlist = m.split(':')
-            fromlist = fromlist.split(',')
-        except:
-            module = m
-            fromlist = []
-        as_ = None
-        if '@' in module:
-            module, as_ = module.split('@')
-        mod = __import__(module, scope, scope, fromlist)
-        if fromlist == []:
-            # mod is only the top package, but we need all packages
-            comp = module.split('.')
-            i = 1
-            mn = comp[0]
-            while True:
-                # mn and mod.__name__ might not be the same
-                if not as_:
-                    as_ = mn
-                scope[as_] = mod
-                requiredmodules[mod.__name__] = 1
-                if len(comp) == i: break
-                mod = getattr(mod, comp[i])
-                mn = string.join(comp[:i+1],'.')
-                i += 1
-        else:
-            # mod is the last package in the component list
-            requiredmodules[mod.__name__] = 1
-            for f in fromlist:
-                scope[f] = getattr(mod, f)
-                if type(scope[f]) == types.ModuleType:
-                    requiredmodules[scope[f].__name__] = 1
-
-class SkipPackage(Exception):
-    def __init__(self, reason):
-        self.reason = reason
-
-scan_in_progress = False
-
-def scan(libpath, packagename):
-    """ helper for finding all required modules of package <packagename> """
-    global scan_in_progress
-    scan_in_progress = True
-    # Use the package in the build directory
-    libpath = os.path.abspath(libpath)
-    sys.path.insert(0, libpath)
-    packdir = os.path.join(libpath, packagename.replace('.', '/'))
-    # A normal import would not find the package in
-    # the build directory. ihook is used to force the import.
-    # After the package is imported the import scope for
-    # the following imports is settled.
-    p = importfrom(packdir)
-    globals()[packagename] = p
-    sys.modules[packagename] = p
-    # Fetch the python modules in the package
-    cwd = os.getcwd()
-    os.chdir(packdir)
-    pymodulefiles = glob.glob('*.py')
-    extmodulefiles = glob.glob('*.pyd')
-    os.chdir(cwd)
-    # Import all python modules and by that run the fake demandload
-    for m in pymodulefiles:
-        if m == '__init__.py': continue
-        tmp = {}
-        mname, ext = os.path.splitext(m)
-        fullname = packagename+'.'+mname
-        try:
-            __import__(fullname, tmp, tmp)
-        except SkipPackage, inst:
-            print >> sys.stderr, 'skipping %s: %s' % (fullname, inst.reason)
-            continue
-        requiredmodules[fullname] = 1
-    # Import all extension modules and by that run the fake demandload
-    for m in extmodulefiles:
-        tmp = {}
-        mname, ext = os.path.splitext(m)
-        fullname = packagename+'.'+mname
-        __import__(fullname, tmp, tmp)
-        requiredmodules[fullname] = 1
-
-def getmodules():
-    return requiredmodules.keys()
-
-def importfrom(filename):
-    """
-    import module/package from a named file and returns the module.
-    It does not check on sys.modules or includes the module in the scope.
-    """
-    loader = ihooks.BasicModuleLoader()
-    path, file = os.path.split(filename)
-    name, ext  = os.path.splitext(file)
-    m = loader.find_module_in_dir(name, path)
-    if not m:
-        raise ImportError, name
-    m = loader.load_module(name, m)
-    return m
--- a/mercurial/patch.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/patch.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,12 +5,11 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from i18n import gettext as _
 from node import *
-demandload(globals(), "base85 cmdutil mdiff util")
-demandload(globals(), "cStringIO email.Parser errno os popen2 re shutil sha")
-demandload(globals(), "sys tempfile zlib")
+import base85, cmdutil, mdiff, util
+import cStringIO, email.Parser, errno, os, popen2, re, shutil, sha
+import sys, tempfile, zlib
 
 # helper functions
 
--- a/mercurial/revlog.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/revlog.py	Wed Dec 13 13:27:09 2006 -0600
@@ -12,9 +12,8 @@
 
 from node import *
 from i18n import gettext as _
-from demandload import demandload
-demandload(globals(), "binascii changegroup errno ancestor mdiff os")
-demandload(globals(), "sha struct util zlib")
+import binascii, changegroup, errno, ancestor, mdiff, os
+import sha, struct, util, zlib
 
 # revlog version strings
 REVLOGV0 = 0
--- a/mercurial/sshrepo.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/sshrepo.py	Wed Dec 13 13:27:09 2006 -0600
@@ -8,8 +8,7 @@
 from node import *
 from remoterepo import *
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "hg os re stat util")
+import hg, os, re, stat, util
 
 class sshrepository(remoterepository):
     def __init__(self, ui, path, create=0):
--- a/mercurial/sshserver.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/sshserver.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,10 +6,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from i18n import gettext as _
 from node import *
-demandload(globals(), "os streamclone sys tempfile util")
+import os, streamclone, sys, tempfile, util
 
 class sshserver(object):
     def __init__(self, ui, repo):
--- a/mercurial/statichttprepo.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/statichttprepo.py	Wed Dec 13 13:27:09 2006 -0600
@@ -7,10 +7,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import *
 from i18n import gettext as _
-demandload(globals(), "changelog filelog httprangereader")
-demandload(globals(), "repo localrepo manifest os urllib urllib2 util")
+import changelog, filelog, httprangereader
+import repo, localrepo, manifest, os, urllib, urllib2, util
 
 class rangereader(httprangereader.httprangereader):
     def read(self, size=None):
--- a/mercurial/streamclone.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/streamclone.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,9 +5,8 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from i18n import gettext as _
-demandload(globals(), "os stat util lock")
+import os, stat, util, lock
 
 # if server supports streaming clone, it advertises "stream"
 # capability with value that is version+flags of repo it is serving.
--- a/mercurial/templater.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/templater.py	Wed Dec 13 13:27:09 2006 -0600
@@ -5,10 +5,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from i18n import gettext as _
 from node import *
-demandload(globals(), "cgi re sys os time urllib util textwrap")
+import cgi, re, sys, os, time, urllib, util, textwrap
 
 def parsestring(s, quoted=True):
     '''parse a string using simple c-like syntax.
--- a/mercurial/transaction.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/transaction.py	Wed Dec 13 13:27:09 2006 -0600
@@ -11,9 +11,8 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-from demandload import demandload
 from i18n import gettext as _
-demandload(globals(), 'os')
+import os
 
 class transaction(object):
     def __init__(self, report, opener, journal, after=None):
--- a/mercurial/ui.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/ui.py	Wed Dec 13 13:27:09 2006 -0600
@@ -6,9 +6,8 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "errno getpass os re socket sys tempfile")
-demandload(globals(), "ConfigParser traceback util")
+import errno, getpass, os, re, socket, sys, tempfile
+import ConfigParser, traceback, util
 
 def dupconfig(orig):
     new = util.configparser(orig.defaults())
--- a/mercurial/util.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/util.py	Wed Dec 13 13:27:09 2006 -0600
@@ -13,9 +13,8 @@
 """
 
 from i18n import gettext as _
-from demandload import *
-demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
-demandload(globals(), "os threading time calendar ConfigParser locale")
+import cStringIO, errno, getpass, popen2, re, shutil, sys, tempfile
+import os, threading, time, calendar, ConfigParser, locale
 
 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \
             or "ascii"
@@ -693,7 +692,7 @@
 
 # Platform specific variants
 if os.name == 'nt':
-    demandload(globals(), "msvcrt")
+    import msvcrt
     nulldev = 'NUL:'
 
     class winstdout:
--- a/mercurial/util_win32.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/mercurial/util_win32.py	Wed Dec 13 13:27:09 2006 -0600
@@ -13,10 +13,10 @@
 
 import win32api
 
-from demandload import *
 from i18n import gettext as _
-demandload(globals(), 'errno os pywintypes win32con win32file win32process')
-demandload(globals(), 'cStringIO win32com.shell:shell,shellcon winerror')
+import errno, os, pywintypes, win32con, win32file, win32process
+import cStringIO, winerror
+from win32com.shell import shell,shellcon
 
 class WinError:
     winerror_map = {
--- a/setup.py	Tue Dec 12 18:16:23 2006 -0600
+++ b/setup.py	Wed Dec 13 13:27:09 2006 -0600
@@ -13,8 +13,6 @@
 from distutils.core import setup, Extension
 from distutils.command.install_data import install_data
 
-# mercurial.packagescan must be the first mercurial module imported
-import mercurial.packagescan
 import mercurial.version
 
 # py2exe needs to be installed to work
@@ -35,31 +33,6 @@
     except ImportError:
         pass
 
-    # Due to the use of demandload py2exe is not finding the modules.
-    # packagescan.getmodules creates a list of modules included in
-    # the mercurial package plus depdent modules.
-    from py2exe.build_exe import py2exe as build_exe
-
-    class py2exe_for_demandload(build_exe):
-        """ overwrites the py2exe command class for getting the build
-        directory and for setting the 'includes' option."""
-        def initialize_options(self):
-            self.build_lib = None
-            build_exe.initialize_options(self)
-        def finalize_options(self):
-            # Get the build directory, ie. where to search for modules.
-            self.set_undefined_options('build',
-                                       ('build_lib', 'build_lib'))
-            # Sets the 'includes' option with the list of needed modules
-            if not self.includes:
-                self.includes = []
-            else:
-                self.includes = self.includes.split(',')
-            mercurial.packagescan.scan(self.build_lib, 'mercurial')
-            mercurial.packagescan.scan(self.build_lib, 'mercurial.hgweb')
-            mercurial.packagescan.scan(self.build_lib, 'hgext')
-            self.includes += mercurial.packagescan.getmodules()
-            build_exe.finalize_options(self)
 except ImportError:
     py2exe_for_demandload = None