# HG changeset patch # User Matt Mackall # Date 1166038029 21600 # Node ID abaee83ce0a6deb8adb8fc1f67685954fc7a4e27 # Parent 1e0b94cfba0e84a62753efd647966c0c1af68450 Replace demandload with new demandimport diff --git a/contrib/churn.py b/contrib/churn.py --- a/contrib/churn.py +++ b/contrib/churn.py @@ -11,10 +11,9 @@ # # -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): diff --git a/hg b/hg --- a/hg +++ b/hg @@ -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() diff --git a/hgext/acl.py b/hgext/acl.py --- a/hgext/acl.py +++ b/hgext/acl.py @@ -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.''' diff --git a/hgext/bugzilla.py b/hgext/bugzilla.py --- a/hgext/bugzilla.py +++ b/hgext/bugzilla.py @@ -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 diff --git a/hgext/extdiff.py b/hgext/extdiff.py --- a/hgext/extdiff.py +++ b/hgext/extdiff.py @@ -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): diff --git a/hgext/fetch.py b/hgext/fetch.py --- a/hgext/fetch.py +++ b/hgext/fetch.py @@ -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. diff --git a/hgext/hbisect.py b/hgext/hbisect.py --- a/hgext/hbisect.py +++ b/hgext/hbisect.py @@ -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" diff --git a/hgext/hgk.py b/hgext/hgk.py --- a/hgext/hgk.py +++ b/hgext/hgk.py @@ -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""" diff --git a/hgext/mq.py b/hgext/mq.py --- a/hgext/mq.py +++ b/hgext/mq.py @@ -29,11 +29,9 @@ remove patch from applied stack 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" diff --git a/hgext/notify.py b/hgext/notify.py --- a/hgext/notify.py +++ b/hgext/notify.py @@ -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 = ''' diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py --- a/hgext/patchbomb.py +++ b/hgext/patchbomb.py @@ -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 * diff --git a/hgext/transplant.py b/hgext/transplant.py --- a/hgext/transplant.py +++ b/hgext/transplant.py @@ -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 diff --git a/mercurial/appendfile.py b/mercurial/appendfile.py --- a/mercurial/appendfile.py +++ b/mercurial/appendfile.py @@ -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. diff --git a/mercurial/archival.py b/mercurial/archival.py --- a/mercurial/archival.py +++ b/mercurial/archival.py @@ -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 diff --git a/mercurial/bundlerepo.py b/mercurial/bundlerepo.py --- a/mercurial/bundlerepo.py +++ b/mercurial/bundlerepo.py @@ -12,8 +12,7 @@ of the GNU General Public License, incor 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 diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py --- a/mercurial/changegroup.py +++ b/mercurial/changegroup.py @@ -6,9 +6,9 @@ changegroup.py - Mercurial changegroup m 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""" diff --git a/mercurial/changelog.py b/mercurial/changelog.py --- a/mercurial/changelog.py +++ b/mercurial/changelog.py @@ -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): """ diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -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 = ':' diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -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.""" diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -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 diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py new file mode 100644 --- /dev/null +++ b/mercurial/demandimport.py @@ -0,0 +1,104 @@ +# demandimport.py - global demand-loading of modules for Mercurial +# +# Copyright 2006 Matt Mackall +# +# 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 "" % 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 + diff --git a/mercurial/demandload.py b/mercurial/demandload.py deleted file mode 100644 --- a/mercurial/demandload.py +++ /dev/null @@ -1,135 +0,0 @@ -'''Demand load modules when used, not when imported.''' - -__author__ = '''Copyright 2006 Vadim Gelfer . -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_) diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py --- a/mercurial/dirstate.py +++ b/mercurial/dirstate.py @@ -9,8 +9,7 @@ of the GNU General Public License, incor 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" diff --git a/mercurial/filelog.py b/mercurial/filelog.py --- a/mercurial/filelog.py +++ b/mercurial/filelog.py @@ -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): diff --git a/mercurial/hg.py b/mercurial/hg.py --- a/mercurial/hg.py +++ b/mercurial/hg.py @@ -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 diff --git a/mercurial/hgweb/__init__.py b/mercurial/hgweb/__init__.py --- a/mercurial/hgweb/__init__.py +++ b/mercurial/hgweb/__init__.py @@ -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) + diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py --- a/mercurial/hgweb/common.py +++ b/mercurial/hgweb/common.py @@ -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") diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -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] != "/": diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py +++ b/mercurial/hgweb/hgwebdir_mod.py @@ -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): diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py --- a/mercurial/hgweb/request.py +++ b/mercurial/hgweb/request.py @@ -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): diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py --- a/mercurial/hgweb/server.py +++ b/mercurial/hgweb/server.py @@ -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): diff --git a/mercurial/httprepo.py b/mercurial/httprepo.py --- a/mercurial/httprepo.py +++ b/mercurial/httprepo.py @@ -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): diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -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') diff --git a/mercurial/lock.py b/mercurial/lock.py --- a/mercurial/lock.py +++ b/mercurial/lock.py @@ -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): diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -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.''' diff --git a/mercurial/manifest.py b/mercurial/manifest.py --- a/mercurial/manifest.py +++ b/mercurial/manifest.py @@ -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): diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py --- a/mercurial/mdiff.py +++ b/mercurial/mdiff.py @@ -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.''' diff --git a/mercurial/merge.py b/mercurial/merge.py --- a/mercurial/merge.py +++ b/mercurial/merge.py @@ -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 diff --git a/mercurial/node.py b/mercurial/node.py --- a/mercurial/node.py +++ b/mercurial/node.py @@ -7,8 +7,7 @@ This software may be used and distribute of the GNU General Public License, incorporated herein by reference. """ -from demandload import demandload -demandload(globals(), "binascii") +import binascii nullrev = -1 nullid = "\0" * 20 diff --git a/mercurial/packagescan.py b/mercurial/packagescan.py deleted file mode 100644 --- a/mercurial/packagescan.py +++ /dev/null @@ -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 -# -# 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 """ - 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 diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -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 diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -12,9 +12,8 @@ of the GNU General Public License, incor 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 diff --git a/mercurial/sshrepo.py b/mercurial/sshrepo.py --- a/mercurial/sshrepo.py +++ b/mercurial/sshrepo.py @@ -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): diff --git a/mercurial/sshserver.py b/mercurial/sshserver.py --- a/mercurial/sshserver.py +++ b/mercurial/sshserver.py @@ -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): diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py --- a/mercurial/statichttprepo.py +++ b/mercurial/statichttprepo.py @@ -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): diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py --- a/mercurial/streamclone.py +++ b/mercurial/streamclone.py @@ -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. diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -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. diff --git a/mercurial/transaction.py b/mercurial/transaction.py --- a/mercurial/transaction.py +++ b/mercurial/transaction.py @@ -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): diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -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()) diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -13,9 +13,8 @@ platform-specific details from the core. """ 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 @@ def checkfolding(path): # Platform specific variants if os.name == 'nt': - demandload(globals(), "msvcrt") + import msvcrt nulldev = 'NUL:' class winstdout: diff --git a/mercurial/util_win32.py b/mercurial/util_win32.py --- a/mercurial/util_win32.py +++ b/mercurial/util_win32.py @@ -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 = { diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -13,8 +13,6 @@ import os 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 @@ try: 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