Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/distutils/lib2def.py
7757 views
import re1import sys2import subprocess34__doc__ = """This module generates a DEF file from the symbols in5an MSVC-compiled DLL import library. It correctly discriminates between6data and functions. The data is collected from the output of the program7nm(1).89Usage:10python lib2def.py [libname.lib] [output.def]11or12python lib2def.py [libname.lib] > output.def1314libname.lib defaults to python<py_ver>.lib and output.def defaults to stdout1516Author: Robert Kern <[email protected]>17Last Update: April 30, 199918"""1920__version__ = '0.1a'2122py_ver = "%d%d" % tuple(sys.version_info[:2])2324DEFAULT_NM = ['nm', '-Cs']2526DEF_HEADER = """LIBRARY python%s.dll27;CODE PRELOAD MOVEABLE DISCARDABLE28;DATA PRELOAD SINGLE2930EXPORTS31""" % py_ver32# the header of the DEF file3334FUNC_RE = re.compile(r"^(.*) in python%s\.dll" % py_ver, re.MULTILINE)35DATA_RE = re.compile(r"^_imp__(.*) in python%s\.dll" % py_ver, re.MULTILINE)3637def parse_cmd():38"""Parses the command-line arguments.3940libfile, deffile = parse_cmd()"""41if len(sys.argv) == 3:42if sys.argv[1][-4:] == '.lib' and sys.argv[2][-4:] == '.def':43libfile, deffile = sys.argv[1:]44elif sys.argv[1][-4:] == '.def' and sys.argv[2][-4:] == '.lib':45deffile, libfile = sys.argv[1:]46else:47print("I'm assuming that your first argument is the library")48print("and the second is the DEF file.")49elif len(sys.argv) == 2:50if sys.argv[1][-4:] == '.def':51deffile = sys.argv[1]52libfile = 'python%s.lib' % py_ver53elif sys.argv[1][-4:] == '.lib':54deffile = None55libfile = sys.argv[1]56else:57libfile = 'python%s.lib' % py_ver58deffile = None59return libfile, deffile6061def getnm(nm_cmd=['nm', '-Cs', 'python%s.lib' % py_ver], shell=True):62"""Returns the output of nm_cmd via a pipe.6364nm_output = getnm(nm_cmd = 'nm -Cs py_lib')"""65p = subprocess.Popen(nm_cmd, shell=shell, stdout=subprocess.PIPE,66stderr=subprocess.PIPE, universal_newlines=True)67nm_output, nm_err = p.communicate()68if p.returncode != 0:69raise RuntimeError('failed to run "%s": "%s"' % (70' '.join(nm_cmd), nm_err))71return nm_output7273def parse_nm(nm_output):74"""Returns a tuple of lists: dlist for the list of data75symbols and flist for the list of function symbols.7677dlist, flist = parse_nm(nm_output)"""78data = DATA_RE.findall(nm_output)79func = FUNC_RE.findall(nm_output)8081flist = []82for sym in data:83if sym in func and (sym[:2] == 'Py' or sym[:3] == '_Py' or sym[:4] == 'init'):84flist.append(sym)8586dlist = []87for sym in data:88if sym not in flist and (sym[:2] == 'Py' or sym[:3] == '_Py'):89dlist.append(sym)9091dlist.sort()92flist.sort()93return dlist, flist9495def output_def(dlist, flist, header, file = sys.stdout):96"""Outputs the final DEF file to a file defaulting to stdout.9798output_def(dlist, flist, header, file = sys.stdout)"""99for data_sym in dlist:100header = header + '\t%s DATA\n' % data_sym101header = header + '\n' # blank line102for func_sym in flist:103header = header + '\t%s\n' % func_sym104file.write(header)105106if __name__ == '__main__':107libfile, deffile = parse_cmd()108if deffile is None:109deffile = sys.stdout110else:111deffile = open(deffile, 'w')112nm_cmd = DEFAULT_NM + [str(libfile)]113nm_output = getnm(nm_cmd, shell=False)114dlist, flist = parse_nm(nm_output)115output_def(dlist, flist, DEF_HEADER, deffile)116117118