Files
pyinstaller/bindepend.py
T
naufraghi 82ab43bb1e Merged revisions 342-393,401-404,413,424,426-427,429-433,435-437,439-442,445,448 via svnmerge from
https://svn.pyinstaller.python-hosting.com/branches/python2.5

........
  r342 | naufraghi | 2007-12-03 20:02:47 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r238@rosen:  naufraghi | 2007-12-03 19:59:24 +0100
    r200@rosen:  naufraghi | 2007-11-23 15:54:13 +0100
    Cleanup, remove some warning
   
........
  r343 | naufraghi | 2007-12-03 20:02:56 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r239@rosen:  naufraghi | 2007-12-03 19:59:24 +0100
    r201@rosen:  naufraghi | 2007-11-23 17:23:15 +0100
    Allow starting runtests from any path
   
........
  r344 | naufraghi | 2007-12-03 20:03:04 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r240@rosen:  naufraghi | 2007-12-03 20:00:51 +0100
    r202@rosen:  naufraghi | 2007-11-23 17:24:17 +0100
    Add natural sort for tests
   
........
  r345 | naufraghi | 2007-12-03 20:03:12 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r241@rosen:  naufraghi | 2007-12-03 20:00:51 +0100
    r203@rosen:  naufraghi | 2007-11-23 17:32:46 +0100
    Fix some fragile import?
   
........
  r346 | naufraghi | 2007-12-03 20:03:21 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r242@rosen:  naufraghi | 2007-12-03 20:01:01 +0100
    r204@rosen:  naufraghi | 2007-11-23 17:46:41 +0100
    Adds a basic way to choose tests to run
   
........
  r347 | naufraghi | 2007-12-03 20:03:29 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r243@rosen:  naufraghi | 2007-12-03 20:01:04 +0100
    r205@rosen:  naufraghi | 2007-11-23 17:57:31 +0100
    Adds and xml.dom test
   
........
  r348 | naufraghi | 2007-12-03 20:03:38 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r244@rosen:  naufraghi | 2007-12-03 20:01:04 +0100
    r206@rosen:  naufraghi | 2007-11-23 18:05:04 +0100
    Remove cmd line quoting
   
........
  r349 | naufraghi | 2007-12-03 20:03:46 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r245@rosen:  naufraghi | 2007-12-03 20:01:06 +0100
    r207@rosen:  naufraghi | 2007-11-23 19:47:13 +0100
    Adds 'import os' in one specfile
   
........
  r350 | naufraghi | 2007-12-03 20:03:54 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r246@rosen:  naufraghi | 2007-12-03 20:01:08 +0100
    r208@rosen:  naufraghi | 2007-11-23 19:49:21 +0100
    Add an 'encoding' hiddeimport test
   
........
  r351 | naufraghi | 2007-12-03 20:04:05 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r248@rosen:  naufraghi | 2007-12-03 20:01:08 +0100
    r211@rosen:  naufraghi | 2007-11-27 16:13:30 +0100
    Rewrite exec in execfile
   
........
  r352 | naufraghi | 2007-12-03 20:04:15 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r250@rosen:  naufraghi | 2007-12-03 20:01:09 +0100
    r214@rosen:  naufraghi | 2007-11-30 11:49:23 +0100
    Adds support for extended __path__
   
........
  r353 | naufraghi | 2007-12-03 20:04:23 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r251@rosen:  naufraghi | 2007-12-03 20:01:13 +0100
    r215@rosen:  naufraghi | 2007-11-30 11:52:18 +0100
    Simplify execfile
   
........
  r354 | naufraghi | 2007-12-03 20:04:30 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r252@rosen:  naufraghi | 2007-12-03 20:01:15 +0100
    r216@rosen:  naufraghi | 2007-11-30 11:52:56 +0100
    Fix output path
   
........
  r355 | naufraghi | 2007-12-03 20:04:38 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r253@rosen:  naufraghi | 2007-12-03 20:01:16 +0100
    r217@rosen:  naufraghi | 2007-11-30 12:13:17 +0100
    Add optional logging of self.moduleds
   
........
  r356 | naufraghi | 2007-12-03 20:04:46 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r254@rosen:  naufraghi | 2007-12-03 20:01:16 +0100
    r218@rosen:  naufraghi | 2007-11-30 12:19:13 +0100
    Add __str__ at Module class (and adds a default __file__ too)
   
........
  r357 | naufraghi | 2007-12-03 20:04:54 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r255@rosen:  naufraghi | 2007-12-03 20:01:18 +0100
    r219@rosen:  naufraghi | 2007-11-30 12:30:19 +0100
    Add version info to log filename
   
........
  r358 | naufraghi | 2007-12-03 20:05:01 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r256@rosen:  naufraghi | 2007-12-03 20:01:19 +0100
    r220@rosen:  naufraghi | 2007-11-30 16:11:07 +0100
    Asset-ize a comment + cosmetics (align variable name to other scopes)
   
........
  r359 | naufraghi | 2007-12-03 20:05:09 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r257@rosen:  naufraghi | 2007-12-03 20:01:21 +0100
    r221@rosen:  naufraghi | 2007-11-30 16:46:17 +0100
    Add comments and a None-iti assert
   
........
  r360 | naufraghi | 2007-12-03 20:05:16 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r258@rosen:  naufraghi | 2007-12-03 20:01:24 +0100
    r222@rosen:  naufraghi | 2007-12-03 16:23:17 +0100
    Remove user interaction
   
........
  r361 | naufraghi | 2007-12-03 20:05:25 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r259@rosen:  naufraghi | 2007-12-03 20:01:25 +0100
    r223@rosen:  naufraghi | 2007-12-03 16:39:48 +0100
    Activate the option to run selected tests: runtests.py --run 12 13
   
........
  r362 | naufraghi | 2007-12-03 20:05:32 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r260@rosen:  naufraghi | 2007-12-03 20:01:28 +0100
    r224@rosen:  naufraghi | 2007-12-03 16:51:02 +0100
    Adds a test that fails
   
........
  r363 | naufraghi | 2007-12-03 20:05:41 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r261@rosen:  naufraghi | 2007-12-03 20:01:28 +0100
    r225@rosen:  naufraghi | 2007-12-03 17:11:07 +0100
    Expand imports
   
........
  r364 | naufraghi | 2007-12-03 20:05:48 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r262@rosen:  naufraghi | 2007-12-03 20:01:29 +0100
    r226@rosen:  naufraghi | 2007-12-03 17:12:21 +0100
    Reorder imports
   
........
  r365 | naufraghi | 2007-12-03 20:05:55 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r263@rosen:  naufraghi | 2007-12-03 20:01:30 +0100
    r227@rosen:  naufraghi | 2007-12-03 17:16:11 +0100
    Turns 1-0 to True-False
   
........
  r366 | naufraghi | 2007-12-03 20:06:03 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r264@rosen:  naufraghi | 2007-12-03 20:01:30 +0100
    r228@rosen:  naufraghi | 2007-12-03 17:32:17 +0100
    Path cleanup
   
........
  r367 | naufraghi | 2007-12-03 20:06:10 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r265@rosen:  naufraghi | 2007-12-03 20:01:32 +0100
    r229@rosen:  naufraghi | 2007-12-03 17:35:21 +0100
    Followup: Path cleanup
   
........
  r368 | naufraghi | 2007-12-03 20:06:17 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r266@rosen:  naufraghi | 2007-12-03 20:01:33 +0100
    r230@rosen:  naufraghi | 2007-12-03 18:42:00 +0100
    Add some docs to assemble()
   
........
  r369 | naufraghi | 2007-12-03 20:06:23 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r267@rosen:  naufraghi | 2007-12-03 20:01:35 +0100
    r231@rosen:  naufraghi | 2007-12-03 18:55:32 +0100
    Remove criptic outN.toc numbering, use class names + reformat docs
   
........
  r370 | naufraghi | 2007-12-03 20:06:30 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r268@rosen:  naufraghi | 2007-12-03 20:01:36 +0100
    r232@rosen:  naufraghi | 2007-12-03 18:56:26 +0100
    Restore common prefix 'out'
   
........
  r371 | naufraghi | 2007-12-03 20:06:38 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r269@rosen:  naufraghi | 2007-12-03 20:01:37 +0100
    r236@rosen:  naufraghi | 2007-12-03 19:16:19 +0100
    Cosmetic
   
........
  r372 | naufraghi | 2007-12-03 20:06:45 +0100 (Lun, 03 Dic 2007) | 5 lines
  
   r270@rosen:  naufraghi | 2007-12-03 20:01:37 +0100
    r237@rosen:  naufraghi | 2007-12-03 19:17:17 +0100
    Factorize py[c|o] extension choice
   
........
  r373 | naufraghi | 2007-12-04 12:01:51 +0100 (Mar, 04 Dic 2007) | 3 lines
  
   r302@rosen:  naufraghi | 2007-12-04 12:00:38 +0100
   Restore invcnum
........
  r374 | naufraghi | 2007-12-04 12:01:58 +0100 (Mar, 04 Dic 2007) | 3 lines
  
   r303@rosen:  naufraghi | 2007-12-04 12:01:41 +0100
   Add warning for mod == None
........
  r375 | naufraghi | 2007-12-04 12:08:04 +0100 (Mar, 04 Dic 2007) | 2 lines
  
  Add final final "done" message
........
  r376 | naufraghi | 2007-12-04 12:09:30 +0100 (Mar, 04 Dic 2007) | 3 lines
  
  Ops, is "config.dat"
........
  r377 | naufraghi | 2007-12-04 12:26:20 +0100 (Mar, 04 Dic 2007) | 2 lines
  
  Restored removed "break"
........
  r378 | naufraghi | 2007-12-04 16:01:18 +0100 (Mar, 04 Dic 2007) | 2 lines
  
  Remove already logged warning
........
  r379 | naufraghi | 2007-12-04 17:46:50 +0100 (Mar, 04 Dic 2007) | 1 line
  
  Minimize test case
........
  r380 | naufraghi | 2007-12-04 17:51:07 +0100 (Mar, 04 Dic 2007) | 1 line
  
  Update PathImportDirector if __path__ changes (for example if modified by _xmlplus hook)
........
  r381 | naufraghi | 2007-12-04 18:06:10 +0100 (Mar, 04 Dic 2007) | 1 line
  
  Localize hiddenimport and add hook
........
  r382 | naufraghi | 2007-12-04 19:37:45 +0100 (Mar, 04 Dic 2007) | 1 line
  
  Add another elementree test
........
  r383 | naufraghi | 2007-12-17 12:58:57 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Fix importHook debug print, add other (commented) print debugs
........
  r384 | naufraghi | 2007-12-17 13:00:32 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Support more then one log per spec
........
  r385 | naufraghi | 2007-12-17 13:01:10 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Add hidden xml.etree.ElementTree import
........
  r386 | naufraghi | 2007-12-17 13:02:35 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Restore PATH before assert + comments
........
  r387 | naufraghi | 2007-12-17 14:42:32 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Print warnings for generic excepts in the code
........
  r388 | naufraghi | 2007-12-17 14:56:12 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Use a specific exception for Owners
........
  r389 | naufraghi | 2007-12-17 16:44:59 +0100 (Lun, 17 Dic 2007) | 1 line
  
  Remove final comma
........
  r390 | naufraghi | 2007-12-18 14:51:06 +0100 (Mar, 18 Dic 2007) | 1 line
  
  Expand debug print
........
  r391 | naufraghi | 2007-12-19 18:21:05 +0100 (Mer, 19 Dic 2007) | 1 line
  
  Avoid 'import os' that causes problems in py25
........
  r392 | naufraghi | 2007-12-19 18:34:46 +0100 (Mer, 19 Dic 2007) | 1 line
  
  Represent with %r the exception
........
  r393 | naufraghi | 2007-12-19 18:37:52 +0100 (Mer, 19 Dic 2007) | 1 line
  
  Add comments and commented prints
........
  r401 | naufraghi | 2008-01-11 16:47:02 +0100 (Ven, 11 Gen 2008) | 1 line
  
  Use sys.executable saved at build time (unknown when frozen)
........
  r402 | naufraghi | 2008-01-11 17:17:37 +0100 (Ven, 11 Gen 2008) | 1 line
  
  Restore linux compatibility
........
  r403 | naufraghi | 2008-01-11 18:37:22 +0100 (Ven, 11 Gen 2008) | 1 line
  
  Add test for sys.getfilesystemencoding()
........
  r404 | naufraghi | 2008-01-11 19:42:42 +0100 (Ven, 11 Gen 2008) | 1 line
  
  Accept different error under windows (FIXME: do a real isfile check)
........
  r413 | naufraghi | 2008-01-25 19:47:50 +0100 (Ven, 25 Gen 2008) | 1 line
  
  Restore compatibility with python 2.4
........
  r424 | naufraghi | 2008-02-18 11:53:12 +0100 (Lun, 18 Feb 2008) | 3 lines
  
   r343@trinity:  naufraghi | 2008-02-18 11:53:00 +0100
   Merge dal trunk
........
  r426 | naufraghi | 2008-02-18 14:49:15 +0100 (Lun, 18 Feb 2008) | 1 line
  
  Catch SyntaxError too
........
  r427 | naufraghi | 2008-02-18 15:02:45 +0100 (Lun, 18 Feb 2008) | 1 line
  
  Revert: DummyZlib is a zero commpression zlib
........
  r429 | naufraghi | 2008-02-23 17:05:35 +0100 (Sab, 23 Feb 2008) | 1 line
  
  Add a (failing) test on module imp
........
  r430 | naufraghi | 2008-02-23 18:12:59 +0100 (Sab, 23 Feb 2008) | 1 line
  
  Modified runtest, now all tests are executed (default) but the interactive one(s), thet are executed if -i option is provided
........
  r431 | naufraghi | 2008-02-23 18:18:02 +0100 (Sab, 23 Feb 2008) | 1 line
  
  Merge dal branch dl
........
  r432 | danielevarrazzo | 2008-02-24 14:38:15 +0100 (Dom, 24 Feb 2008) | 2 lines
  
  Added missing file from the 'dl' branch.
........
  r433 | naufraghi | 2008-02-25 13:07:44 +0100 (Lun, 25 Feb 2008) | 1 line
  
  Continue on test failure and report (...) at the end
........
  r435 | naufraghi | 2008-02-25 13:28:08 +0100 (Lun, 25 Feb 2008) | 1 line
  
  Stub of a launcher rebuilder
........
  r436 | danielevarrazzo | 2008-02-25 13:30:56 +0100 (Lun, 25 Feb 2008) | 2 lines
  
  The Python library is found both in onedir and onefile mode.
........
  r437 | naufraghi | 2008-02-25 18:42:16 +0100 (Lun, 25 Feb 2008) | 1 line
  
  Add comments and (commented) debug prints
........
  r439 | naufraghi | 2008-02-28 00:16:38 +0100 (Gio, 28 Feb 2008) | 1 line
  
  Cosmetics
........
  r440 | danielevarrazzo | 2008-02-28 20:21:15 +0100 (Gio, 28 Feb 2008) | 4 lines
  
  The Python library is added to the package even if bindepend couldn't find it.
  
  This may happen when Python is statically linked, e.g. on debian-based systems.
........
  r441 | naufraghi | 2008-03-01 00:46:00 +0100 (Sab, 01 Mar 2008) | 1 line
  
  Remove too restrictive assert
........
  r442 | naufraghi | 2008-03-01 00:47:07 +0100 (Sab, 01 Mar 2008) | 1 line
  
  Run Configure.py after make
........
  r445 | naufraghi | 2008-04-28 19:42:13 +0200 (Lun, 28 Apr 2008) | 1 line
  
  Merge follow up
........
  r448 | naufraghi | 2008-04-30 15:41:51 +0200 (Mer, 30 Apr 2008) | 2 lines
  
  ops, svnmerge was not complete...
........


git-svn-id: http://svn.pyinstaller.org/trunk@449 8dd32b29-ccff-0310-8a9a-9233e24343b1
2008-04-30 14:30:09 +00:00

442 lines
15 KiB
Python
Executable File

#! /usr/bin/env python
# Find external dependencies of binary libraries.
# Copyright (C) 2005, Giovanni Bajo
# Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
# use dumpbin.exe (if present) to find the binary
# dependencies of an extension module.
# if dumpbin not available, pick apart the PE hdr of the binary
# while this appears to work well, it is complex and subject to
# problems with changes to PE hdrs (ie, this works only on 32 bit Intel
# Windows format binaries)
#
# Note also that you should check the results to make sure that the
# dlls are redistributable. I've listed most of the common MS dlls
# under "excludes" below; add to this list as necessary (or use the
# "excludes" option in the INSTALL section of the config file).
import os
import time
import string
import sys
import re
from glob import glob
seen = {}
_bpath = None
iswin = sys.platform[:3] == 'win'
cygwin = sys.platform == 'cygwin'
excludes = {'KERNEL32.DLL':1,
'ADVAPI.DLL':1,
'MSVCRT.DLL':1,
'ADVAPI32.DLL':1,
'COMCTL32.DLL':1,
'CRTDLL.DLL':1,
'GDI32.DLL':1,
'MFC42.DLL':1,
'NTDLL.DLL':1,
'OLE32.DLL':1,
'OLEAUT32.DLL':1,
'RPCRT4.DLL':1,
'SHELL32.DLL':1,
'USER32.DLL':1,
'WINSPOOL.DRV':1,
'WS2HELP.DLL':1,
'WS2_32.DLL':1,
'WSOCK32.DLL':1,
'MSWSOCK.DLL':1,
'WINMM.DLL':1,
'COMDLG32.DLL':1,
## 'ZLIB.DLL':1, # test with python 1.5.2
'ODBC32.DLL':1,
'VERSION.DLL':1,
'IMM32.DLL':1,
'DDRAW.DLL':1,
'DCIMAN32.DLL':1,
'OPENGL32.DLL':1,
'GLU32.DLL':1,
'GLUB32.DLL':1,
'NETAPI32.DLL':1,
'PSAPI.DLL':1,
'MSVCP80.DLL':1,
'MSVCR80.DLL':1,
# regex excludes
'^/usr/lib':1,
'^/lib':1,
'^/lib/tls':1,
'^/System/Library/Frameworks':1,
}
excludesRe = re.compile('|'.join(excludes.keys()), re.I)
def getfullnameof(mod, xtrapath = None):
"""Return the full path name of MOD.
MOD is the basename of a dll or pyd.
XTRAPATH is a path or list of paths to search first.
Return the full path name of MOD.
Will search the full Windows search path, as well as sys.path"""
# Search sys.path first!
epath = sys.path + getWindowsPath()
if xtrapath is not None:
if type(xtrapath) == type(''):
epath.insert(0, xtrapath)
else:
epath = xtrapath + epath
for p in epath:
npth = os.path.join(p, mod)
if os.path.exists(npth):
return npth
return ''
def _getImports_dumpbin(pth):
"""Find the binary dependencies of PTH.
This implementation (not used right now) uses the MSVC utility dumpbin"""
import tempfile
rslt = []
tmpf = tempfile.mktemp()
os.system('dumpbin /IMPORTS "%s" >%s' %(pth, tmpf))
time.sleep(0.1)
txt = open(tmpf,'r').readlines()
os.remove(tmpf)
i = 0
while i < len(txt):
tokens = string.split(txt[i])
if len(tokens) == 1 and string.find(tokens[0], '.') > 0:
rslt.append(string.strip(tokens[0]))
i = i + 1
return rslt
def _getImports_pe_x(pth):
"""Find the binary dependencies of PTH.
This implementation walks through the PE header"""
import struct
rslt = []
try:
f = open(pth, 'rb').read()
pehdrd = struct.unpack('l', f[60:64])[0] #after the MSDOS loader is the offset of the peheader
magic = struct.unpack('l', f[pehdrd:pehdrd+4])[0] # pehdr starts with magic 'PE\000\000' (or 17744)
# then 20 bytes of COFF header
numsecs = struct.unpack('h', f[pehdrd+6:pehdrd+8])[0] # whence we get number of sections
opthdrmagic = struct.unpack('h', f[pehdrd+24:pehdrd+26])[0]
if opthdrmagic == 0x10b: # PE32 format
numdictoffset = 116
importoffset = 128
elif opthdrmagic == 0x20b: # PE32+ format
numdictoffset = 132
importoffset = 148
else:
print "E: bindepend cannot analyze %s - unknown header format! %x" % (pth, opthdrmagic)
return rslt
numdirs = struct.unpack('l', f[pehdrd+numdictoffset:pehdrd+numdictoffset+4])[0]
idata = ''
if magic == 17744:
importsec, sz = struct.unpack('2l', f[pehdrd+importoffset:pehdrd+importoffset+8])
if sz == 0:
return rslt
secttbl = pehdrd + numdictoffset + 4 + 8*numdirs
secttblfmt = '8s7l2h'
seclist = []
for i in range(numsecs):
seclist.append(struct.unpack(secttblfmt, f[secttbl+i*40:secttbl+(i+1)*40]))
#nm, vsz, va, rsz, praw, preloc, plnnums, qrelocs, qlnnums, flags \
# = seclist[-1]
for i in range(len(seclist)-1):
if seclist[i][2] <= importsec < seclist[i+1][2]:
break
vbase = seclist[i][2]
raw = seclist[i][4]
idatastart = raw + importsec - vbase
idata = f[idatastart:idatastart+seclist[i][1]]
i = 0
while 1:
chunk = idata[i*20:(i+1)*20]
if len(chunk) != 20:
print "E: premature end of import table (chunk is %d, not 20)" % len(chunk)
break
vsa = struct.unpack('5l', chunk)[3]
if vsa == 0:
break
sa = raw + vsa - vbase
end = string.find(f, '\000', sa)
nm = f[sa:end]
if nm:
rslt.append(nm)
i = i + 1
else:
print "E: bindepend cannot analyze %s - file is not in PE format!" % pth
except IOError:
print "E: bindepend cannot analyze %s - file not found!" % pth
#except struct.error:
# print "E: bindepend cannot analyze %s - error walking thru pehdr" % pth
return rslt
def _getImports_pe(path):
"""Find the binary dependencies of PTH.
This implementation walks through the PE header"""
import struct
f = open(path, 'rb')
# skip the MSDOS loader
f.seek(60)
# get offset to PE header
offset = struct.unpack('l', f.read(4))[0]
f.seek(offset)
signature = struct.unpack('l', f.read(4))[0]
coffhdrfmt = 'hhlllhh'
rawcoffhdr = f.read(struct.calcsize(coffhdrfmt))
coffhdr = struct.unpack(coffhdrfmt, rawcoffhdr)
coffhdr_numsections = coffhdr[1]
opthdrfmt = 'hbblllllllllhhhhhhllllhhllllll'
rawopthdr = f.read(struct.calcsize(opthdrfmt))
opthdr = struct.unpack(opthdrfmt, rawopthdr)
opthdr_numrvas = opthdr[-1]
datadirs = []
datadirsize = struct.calcsize('ll') # virtual address, size
for i in range(opthdr_numrvas):
rawdatadir = f.read(datadirsize)
datadirs.append(struct.unpack('ll', rawdatadir))
sectionfmt = '8s6l2hl'
sectionsize = struct.calcsize(sectionfmt)
sections = []
for i in range(coffhdr_numsections):
rawsection = f.read(sectionsize)
sections.append(struct.unpack(sectionfmt, rawsection))
importva, importsz = datadirs[1]
if importsz == 0:
return []
# figure out what section it's in
NAME, MISC, VIRTADDRESS, RAWSIZE, POINTERTORAW = range(5)
for j in range(len(sections)-1):
if sections[j][VIRTADDRESS] <= importva < sections[j+1][VIRTADDRESS]:
importsection = sections[j]
break
else:
if importva >= sections[-1][VIRTADDRESS]:
importsection = sections[-1]
else:
print "E: import section is unavailable"
return []
f.seek(importsection[POINTERTORAW] + importva - importsection[VIRTADDRESS])
data = f.read(importsz)
iidescrfmt = 'lllll'
CHARACTERISTICS, DATETIME, FWDRCHAIN, NAMERVA, FIRSTTHUNK = range(5)
iidescrsz = struct.calcsize(iidescrfmt)
dlls = []
while data:
iid = struct.unpack(iidescrfmt, data[:iidescrsz])
if iid[NAMERVA] == 0:
break
f.seek(importsection[POINTERTORAW] + iid[NAMERVA] - importsection[VIRTADDRESS])
nm = f.read(256)
nm, jnk = string.split(nm, '\0', 1)
if nm:
dlls.append(nm)
data = data[iidescrsz:]
return dlls
def Dependencies(lTOC):
"""Expand LTOC to include all the closure of binary dependencies.
LTOC is a logical table of contents, ie, a seq of tuples (name, path).
Return LTOC expanded by all the binary dependencies of the entries
in LTOC, except those listed in the module global EXCLUDES"""
for nm, pth, typ in lTOC:
fullnm = string.upper(os.path.basename(pth))
if seen.get(string.upper(nm),0):
continue
#print "I: analyzing", pth
seen[string.upper(nm)] = 1
for lib, npth in selectImports(pth):
if seen.get(string.upper(lib),0):
continue
lTOC.append((lib, npth, 'BINARY'))
return lTOC
def selectImports(pth):
"""Return the dependencies of a binary that should be included.
Return a list of pairs (name, fullpath)
"""
rv = []
dlls = getImports(pth)
for lib in dlls:
if not iswin and not cygwin:
# plain win case
npth = lib
dir, lib = os.path.split(lib)
if excludes.get(dir,0):
continue
else:
# all other platforms
npth = getfullnameof(lib, os.path.dirname(pth))
# now npth is a candidate lib
# check again for excludes but with regex FIXME: split the list
if excludesRe.search(npth):
if 'libpython' not in npth and 'Python.framework' not in npth:
# skip libs not containing (libpython or Python.framework)
#print "I: skipping %20s <- %s" % (npth, pth)
continue
else:
#print "I: inserting %20s <- %s" % (npth, pth)
pass
if npth:
rv.append((lib, npth))
else:
print "E: lib not found:", lib, "dependency of", pth
return rv
def _getImports_ldd(pth):
"""Find the binary dependencies of PTH.
This implementation is for ldd platforms"""
rslt = []
for line in os.popen('ldd "%s"' % pth).readlines():
m = re.search(r"\s+(.*?)\s+=>\s+(.*?)\s+\(.*\)", line)
if m:
name, lib = m.group(1), m.group(2)
if name[:10] == 'linux-gate':
# linux-gate is a fake library which does not exist and
# should be ignored. See also:
# http://www.trilithium.com/johan/2005/08/linux-gate/
continue
if os.path.exists(lib):
rslt.append(lib)
else:
print 'E: cannot find %s in path %s (needed by %s)' % \
(name, lib, pth)
return rslt
def _getImports_otool(pth):
"""Find the binary dependencies of PTH.
This implementation is for otool platforms"""
rslt = []
for line in os.popen('otool -L "%s"' % pth).readlines():
m = re.search(r"\s+(.*?)\s+\(.*\)", line)
if m:
lib = m.group(1)
if os.path.exists(lib):
rslt.append(lib)
else:
print 'E: cannot find path %s (needed by %s)' % \
(lib, pth)
return rslt
def getImports(pth):
"""Forwards to the correct getImports implementation for the platform.
"""
if sys.platform[:3] == 'win' or sys.platform == 'cygwin':
return _getImports_pe(pth)
elif sys.platform == 'darwin':
return _getImports_otool(pth)
else:
return _getImports_ldd(pth)
def getWindowsPath():
"""Return the path that Windows will search for dlls."""
global _bpath
if _bpath is None:
_bpath = []
if iswin:
try:
import win32api
except ImportError:
print "W: Cannot determine your Windows or System directories"
print "W: Please add them to your PATH if .dlls are not found"
print "W: or install starship.python.net/skippy/win32/Downloads.html"
else:
sysdir = win32api.GetSystemDirectory()
sysdir2 = os.path.normpath(os.path.join(sysdir, '..', 'SYSTEM'))
windir = win32api.GetWindowsDirectory()
_bpath = [sysdir, sysdir2, windir]
_bpath.extend(string.split(os.environ.get('PATH', ''), os.pathsep))
return _bpath
def fixOsxPaths(moduleName):
for name, lib in selectImports(moduleName):
dest = os.path.join("@executable_path", name)
cmd = "install_name_tool -change %s %s %s" % (lib, dest, moduleName)
os.system(cmd)
def findLibrary(name):
"""Look for a library in the system.
Emulate the algorithm used by dlopen.
`name`must include the prefix, e.g. ``libpython2.4.so``
"""
assert sys.platform == 'linux2', "Current implementation for Linux only"
lib = None
# Look in the LD_LIBRARY_PATH
lp = os.environ.get('LD_LIBRARY_PATH')
if lp:
for path in string.split(lp, os.pathsep):
libs = glob(os.path.join(path, name + '*'))
if libs:
lib = libs[0]
break
# Look in /etc/ld.so.cache
if lib is None:
expr = r'/[^\(\)\s]*%s\.[^\(\)\s]*' % re.escape(name)
m = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read())
if m:
lib = m.group(0)
# Look in the known safe paths
if lib is None:
for path in ['/lib', '/usr/lib']:
libs = glob(os.path.join(path, name + '*'))
if libs:
lib = libs[0]
break
# give up :(
if lib is None:
return None
# Resolve the file name into the soname
dir, file = os.path.split(lib)
return os.path.join(dir, getSoname(lib))
def getSoname(filename):
"""Return the soname of a library."""
cmd = "objdump -p -j .dynamic 2>/dev/null " + filename
m = re.search(r'\s+SONAME\s+([^\s]+)', os.popen(cmd).read())
if m: return m.group(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage: python %s BINARYFILE" % sys.argv[0]
sys.exit(0)
print getImports(sys.argv[1])