lib2def.py 3.4 KB

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