lib2def.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import re
  2. import sys
  3. import subprocess
  4. __doc__ = """This module generates a DEF file from the symbols in
  5. an MSVC-compiled DLL import library. It correctly discriminates between
  6. data and functions. The data is collected from the output of the program
  7. nm(1).
  8. Usage:
  9. python lib2def.py [libname.lib] [output.def]
  10. or
  11. python lib2def.py [libname.lib] > output.def
  12. libname.lib defaults to python<py_ver>.lib and output.def defaults to stdout
  13. Author: Robert Kern <kernr@mail.ncifcrf.gov>
  14. Last Update: April 30, 1999
  15. """
  16. __version__ = '0.1a'
  17. py_ver = "%d%d" % tuple(sys.version_info[:2])
  18. DEFAULT_NM = ['nm', '-Cs']
  19. DEF_HEADER = """LIBRARY python%s.dll
  20. ;CODE PRELOAD MOVEABLE DISCARDABLE
  21. ;DATA PRELOAD SINGLE
  22. EXPORTS
  23. """ % py_ver
  24. # the header of the DEF file
  25. FUNC_RE = re.compile(r"^(.*) in python%s\.dll" % py_ver, re.MULTILINE)
  26. DATA_RE = re.compile(r"^_imp__(.*) in python%s\.dll" % py_ver, re.MULTILINE)
  27. def parse_cmd():
  28. """Parses the command-line arguments.
  29. libfile, deffile = parse_cmd()"""
  30. if len(sys.argv) == 3:
  31. if sys.argv[1][-4:] == '.lib' and sys.argv[2][-4:] == '.def':
  32. libfile, deffile = sys.argv[1:]
  33. elif sys.argv[1][-4:] == '.def' and sys.argv[2][-4:] == '.lib':
  34. deffile, libfile = sys.argv[1:]
  35. else:
  36. print("I'm assuming that your first argument is the library")
  37. print("and the second is the DEF file.")
  38. elif len(sys.argv) == 2:
  39. if sys.argv[1][-4:] == '.def':
  40. deffile = sys.argv[1]
  41. libfile = 'python%s.lib' % py_ver
  42. elif sys.argv[1][-4:] == '.lib':
  43. deffile = None
  44. libfile = sys.argv[1]
  45. else:
  46. libfile = 'python%s.lib' % py_ver
  47. deffile = None
  48. return libfile, deffile
  49. def getnm(nm_cmd=['nm', '-Cs', 'python%s.lib' % py_ver], shell=True):
  50. """Returns the output of nm_cmd via a pipe.
  51. nm_output = getnm(nm_cmd = 'nm -Cs py_lib')"""
  52. p = subprocess.Popen(nm_cmd, shell=shell, stdout=subprocess.PIPE,
  53. stderr=subprocess.PIPE, universal_newlines=True)
  54. nm_output, nm_err = p.communicate()
  55. if p.returncode != 0:
  56. raise RuntimeError('failed to run "%s": "%s"' % (
  57. ' '.join(nm_cmd), nm_err))
  58. return nm_output
  59. def parse_nm(nm_output):
  60. """Returns a tuple of lists: dlist for the list of data
  61. symbols and flist for the list of function symbols.
  62. dlist, flist = parse_nm(nm_output)"""
  63. data = DATA_RE.findall(nm_output)
  64. func = FUNC_RE.findall(nm_output)
  65. flist = []
  66. for sym in data:
  67. if sym in func and (sym[:2] == 'Py' or sym[:3] == '_Py' or sym[:4] == 'init'):
  68. flist.append(sym)
  69. dlist = []
  70. for sym in data:
  71. if sym not in flist and (sym[:2] == 'Py' or sym[:3] == '_Py'):
  72. dlist.append(sym)
  73. dlist.sort()
  74. flist.sort()
  75. return dlist, flist
  76. def output_def(dlist, flist, header, file = sys.stdout):
  77. """Outputs the final DEF file to a file defaulting to stdout.
  78. output_def(dlist, flist, header, file = sys.stdout)"""
  79. for data_sym in dlist:
  80. header = header + '\t%s DATA\n' % data_sym
  81. header = header + '\n' # blank line
  82. for func_sym in flist:
  83. header = header + '\t%s\n' % func_sym
  84. file.write(header)
  85. if __name__ == '__main__':
  86. libfile, deffile = parse_cmd()
  87. if deffile is None:
  88. deffile = sys.stdout
  89. else:
  90. deffile = open(deffile, 'w')
  91. nm_cmd = DEFAULT_NM + [str(libfile)]
  92. nm_output = getnm(nm_cmd, shell=False)
  93. dlist, flist = parse_nm(nm_output)
  94. output_def(dlist, flist, DEF_HEADER, deffile)