lib2def.py 3.4 KB

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