author | Volker Kleinfeld <Volker.Kleinfeld@gmx.de> |
Fri, 19 May 2006 08:54:28 -0700 | |
changeset 2323 | c58a403aa830 |
parent 1841 | 7f12a63568ae |
child 2325 | c4ea7f927dab |
permissions | -rw-r--r-- |
1308
2073e5a71008
Cleanup of tabs and trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1283
diff
changeset
|
1 |
# packagescan.py - Helper module for identifing used modules. |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
2 |
# Used for the py2exe distutil. |
2323
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
3 |
# This module must be the first mercurial module imported in setup.py |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
4 |
# |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
5 |
# Copyright 2005 Volker Kleinfeld <Volker.Kleinfeld@gmx.de> |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
6 |
# |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
7 |
# This software may be used and distributed according to the terms |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
8 |
# of the GNU General Public License, incorporated herein by reference. |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
9 |
import glob |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
10 |
import os |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
11 |
import sys |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
12 |
import ihooks |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
13 |
|
2323
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
14 |
# Install this module as fake demandload module |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
15 |
sys.modules['mercurial.demandload'] = sys.modules[__name__] |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
16 |
|
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
17 |
# Requiredmodules contains the modules imported by demandload. |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
18 |
# Please note that demandload can be invoked before the |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
19 |
# mercurial.packagescan.scan method is invoked in case a mercurial |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
20 |
# module is imported. |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
21 |
requiredmodules = {} |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
22 |
def demandload(scope, modules): |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
23 |
""" fake demandload function that collects the required modules """ |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
24 |
for m in modules.split(): |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
25 |
mod = None |
1841
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
26 |
try: |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
27 |
module, submodules = m.split(':') |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
28 |
submodules = submodules.split(',') |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
29 |
except: |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
30 |
module = m |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
31 |
submodules = [] |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
32 |
mod = __import__(module, scope, scope, submodules) |
7f12a63568ae
Fix py2exe packagescan problem with new demandload
Eung-ju Park <eungju@gmail.com>
parents:
1308
diff
changeset
|
33 |
scope[module] = mod |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
34 |
requiredmodules[mod.__name__] = 1 |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
35 |
|
2323
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
36 |
def scan(libpath,packagename): |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
37 |
""" helper for finding all required modules of package <packagename> """ |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
38 |
# Use the package in the build directory |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
39 |
libpath = os.path.abspath(libpath) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
40 |
sys.path.insert(0,libpath) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
41 |
packdir = os.path.join(libpath,packagename) |
1308
2073e5a71008
Cleanup of tabs and trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1283
diff
changeset
|
42 |
# A normal import would not find the package in |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
43 |
# the build directory. ihook is used to force the import. |
1308
2073e5a71008
Cleanup of tabs and trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1283
diff
changeset
|
44 |
# After the package is imported the import scope for |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
45 |
# the following imports is settled. |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
46 |
p = importfrom(packdir) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
47 |
globals()[packagename] = p |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
48 |
sys.modules[packagename] = p |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
49 |
# Fetch the python modules in the package |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
50 |
cwd = os.getcwd() |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
51 |
os.chdir(packdir) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
52 |
pymodulefiles = glob.glob('*.py') |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
53 |
extmodulefiles = glob.glob('*.pyd') |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
54 |
os.chdir(cwd) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
55 |
# Import all python modules and by that run the fake demandload |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
56 |
for m in pymodulefiles: |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
57 |
if m == '__init__.py': continue |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
58 |
tmp = {} |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
59 |
mname,ext = os.path.splitext(m) |
1308
2073e5a71008
Cleanup of tabs and trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1283
diff
changeset
|
60 |
fullname = packagename+'.'+mname |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
61 |
__import__(fullname,tmp,tmp) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
62 |
requiredmodules[fullname] = 1 |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
63 |
# Import all extension modules and by that run the fake demandload |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
64 |
for m in extmodulefiles: |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
65 |
tmp = {} |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
66 |
mname,ext = os.path.splitext(m) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
67 |
fullname = packagename+'.'+mname |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
68 |
__import__(fullname,tmp,tmp) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
69 |
requiredmodules[fullname] = 1 |
2323
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
70 |
|
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
71 |
def getmodules(): |
c58a403aa830
setup.py: install packagescan before any mercurial modules is imported
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents:
1841
diff
changeset
|
72 |
return requiredmodules.keys() |
1283
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
73 |
|
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
74 |
def importfrom(filename): |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
75 |
""" |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
76 |
import module/package from a named file and returns the module. |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
77 |
It does not check on sys.modules or includes the module in the scope. |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
78 |
""" |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
79 |
loader = ihooks.BasicModuleLoader() |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
80 |
path, file = os.path.split(filename) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
81 |
name, ext = os.path.splitext(file) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
82 |
m = loader.find_module_in_dir(name, path) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
83 |
if not m: |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
84 |
raise ImportError, name |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
85 |
m = loader.load_module(name, m) |
f5faab34f32e
Support for the distutils extention 'py2exe' added.
Volker.Kleinfeld@gmx.de
parents:
diff
changeset
|
86 |
return m |