comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/distutils/lib2def.py @ 87:2a2c65a20a8b

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