bindepend.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # copyright 1999 McMillan Enterprises, Inc.
  2. # license: use as you please. No warranty.
  3. #
  4. # use dumpbin.exe (if present) to find the binary
  5. # dependencies of an extension module.
  6. # if dumpbin not available, pick apart the PE hdr of the binary
  7. # while this appears to work well, it is complex and subject to
  8. # problems with changes to PE hdrs (ie, this works only on 32 bit Intel
  9. # Windows format binaries)
  10. #
  11. # Note also that you should check the results to make sure that the
  12. # dlls are redistributable. I've listed most of the common MS dlls
  13. # under "excludes" below; add to this list as necessary (or use the
  14. # "excludes" option in the INSTALL section of the config file).
  15. import os
  16. import time
  17. import string
  18. import sys
  19. import tempfile
  20. import finder
  21. seen = {}
  22. excludes = {'KERNEL32.DLL':1,
  23. 'ADVAPI.DLL':1,
  24. 'MSVCRT.DLL':1,
  25. 'ADVAPI32.DLL':1,
  26. 'COMCTL32.DLL':1,
  27. 'CRTDLL.DLL':1,
  28. 'GDI32.DLL':1,
  29. 'MFC42.DLL':1,
  30. 'NTDLL.DLL':1,
  31. 'OLE32.DLL':1,
  32. 'OLEAUT32.DLL':1,
  33. 'RPCRT4.DLL':1,
  34. 'SHELL32.DLL':1,
  35. 'USER32.DLL':1,
  36. 'WINSPOOL.DRV':1,
  37. 'WS2HELP.DLL':1,
  38. 'WS2_32.DLL':1,
  39. 'WSOCK32.DLL':1,
  40. 'WINMM.DLL':1,
  41. 'COMDLG32.DLL':1,
  42. 'ZLIB.DLL':1,
  43. 'ODBC32.DLL':1,
  44. 'VERSION.DLL':1}
  45. def getfullnameof(mod, xtrapath = None):
  46. """Return the full path name of MOD.
  47. MOD is the basename of a dll or pyd.
  48. XTRAPATH is a path or list of paths to search first.
  49. Return the full path name of MOD.
  50. Will search the full Windows search path, as well as sys.path"""
  51. epath = finder.getpath()
  52. if mod[-4:] in ('.pyd', '.PYD'):
  53. epath = epath + sys.path
  54. if xtrapath is not None:
  55. if type(xtrapath) == type(''):
  56. epath.insert(0, xtrapath)
  57. else:
  58. epath = xtrapath + epath
  59. for p in epath:
  60. npth = os.path.join(p, mod)
  61. if os.path.exists(npth):
  62. return npth
  63. return ''
  64. def getImports1(pth):
  65. """Find the binary dependencies of PTH.
  66. This implementation (not used right now) uses the MSVC utility dumpbin"""
  67. rslt = []
  68. tmpf = tempfile.mktemp()
  69. os.system('dumpbin /IMPORTS "%s" >%s' %(pth, tmpf))
  70. time.sleep(0.1)
  71. txt = open(tmpf,'r').readlines()
  72. os.remove(tmpf)
  73. i = 0
  74. while i < len(txt):
  75. tokens = string.split(txt[i])
  76. if len(tokens) == 1 and string.find(tokens[0], '.') > 0:
  77. rslt.append(string.strip(tokens[0]))
  78. i = i + 1
  79. return rslt
  80. def getImports2(pth):
  81. """Find the binary dependencies of PTH.
  82. This implementation walks through the PE header"""
  83. import struct
  84. rslt = []
  85. try:
  86. f = open(pth, 'rb').read()
  87. pehdrd = struct.unpack('l', f[60:64])[0]
  88. magic = struct.unpack('l', f[pehdrd:pehdrd+4])[0]
  89. numsecs = struct.unpack('h', f[pehdrd+6:pehdrd+8])[0]
  90. numdirs = struct.unpack('l', f[pehdrd+116:pehdrd+120])[0]
  91. idata = ''
  92. if magic == 17744:
  93. importsec, sz = struct.unpack('2l', f[pehdrd+128:pehdrd+136])
  94. secttbl = pehdrd + 120 + 8*numdirs
  95. secttblfmt = '8s7l2h'
  96. seclist = []
  97. for i in range(numsecs):
  98. seclist.append(struct.unpack(secttblfmt, f[secttbl+i*40:secttbl+(i+1)*40]))
  99. #nm, vsz, va, rsz, praw, preloc, plnnums, qrelocs, qlnnums, flags \
  100. # = seclist[-1]
  101. for i in range(len(seclist)-1):
  102. if seclist[i][2] <= importsec < seclist[i+1][2]:
  103. break
  104. vbase = seclist[i][2]
  105. raw = seclist[i][4]
  106. idatastart = raw + importsec - vbase
  107. idata = f[idatastart:idatastart+seclist[i][1]]
  108. i = 0
  109. while 1:
  110. vsa = struct.unpack('5l', idata[i*20:i*20+20])[3]
  111. if vsa == 0:
  112. break
  113. sa = raw + vsa - vbase
  114. end = string.find(f, '\000', sa)
  115. rslt.append(f[sa:end])
  116. i = i + 1
  117. except IOError:
  118. print "bindepend cannot analyze %s - file not found!"
  119. except struct.error:
  120. print "bindepend cannot analyze %s - error walking thru pehdr"
  121. return rslt
  122. def Dependencies(lTOC):
  123. """Expand LTOC to include all the closure of binary dependencies.
  124. LTOC is a logical table of contents, ie, a seq of tuples (name, path).
  125. Return LTOC expanded by all the binary dependencies of the entries
  126. in LTOC, except those listed in the module global EXCLUDES"""
  127. for (nm, pth) in lTOC:
  128. fullnm = string.upper(os.path.basename(pth))
  129. if seen.get(string.upper(nm), 0):
  130. continue
  131. print "analyzing", nm
  132. seen[string.upper(nm)] = 1
  133. dlls = getImports(pth)
  134. for lib in dlls:
  135. print " found", lib
  136. if excludes.get(string.upper(lib), 0):
  137. continue
  138. if seen.get(string.upper(lib), 0):
  139. continue
  140. npth = getfullnameof(lib)
  141. if npth:
  142. lTOC.append((lib, npth))
  143. else:
  144. print " lib not found:", lib, "dependency of",
  145. return lTOC
  146. ##if getfullnameof('dumpbin.exe') == '':
  147. ## def getImports(pth):
  148. ## return getImports2(pth)
  149. ##else:
  150. ## def getImports(pth):
  151. ## return getImports1(pth)
  152. def getImports(pth):
  153. """Forwards to either getImports1 or getImports2
  154. """
  155. return getImports2(pth)