Changeset View
Changeset View
Standalone View
Standalone View
ext/python/Tools/freeze/checkextensions_win32.py
Show All 16 Lines | - The MSVC .dsp file for the extension. The .c source file names | ||||
are extraced from there. | are extraced from there. | ||||
- Specific compiler/linker options | - Specific compiler/linker options | ||||
- Flag to indicate if Unicode compilation is expected. | - Flag to indicate if Unicode compilation is expected. | ||||
At the moment the name and location of this INI file is hardcoded, | At the moment the name and location of this INI file is hardcoded, | ||||
but an obvious enhancement would be to provide command line options. | but an obvious enhancement would be to provide command line options. | ||||
""" | """ | ||||
import os, sys | import os, sys | ||||
Lint: PEP8 E401: multiple imports on one line | |||||
try: | try: | ||||
import win32api | import win32api | ||||
except ImportError: | except ImportError: | ||||
win32api = None # User has already been warned | win32api = None # User has already been warned | ||||
class CExtension: | class CExtension: | ||||
"""An abstraction of an extension implemented in C/C++ | """An abstraction of an extension implemented in C/C++ | ||||
""" | """ | ||||
def __init__(self, name, sourceFiles): | def __init__(self, name, sourceFiles): | ||||
self.name = name | self.name = name | ||||
# A list of strings defining additional compiler options. | # A list of strings defining additional compiler options. | ||||
self.sourceFiles = sourceFiles | self.sourceFiles = sourceFiles | ||||
# A list of special compiler options to be applied to | # A list of special compiler options to be applied to | ||||
# all source modules in this extension. | # all source modules in this extension. | ||||
self.compilerOptions = [] | self.compilerOptions = [] | ||||
# A list of .lib files the final .EXE will need. | # A list of .lib files the final .EXE will need. | ||||
self.linkerLibs = [] | self.linkerLibs = [] | ||||
def GetSourceFiles(self): | def GetSourceFiles(self): | ||||
return self.sourceFiles | return self.sourceFiles | ||||
def AddCompilerOption(self, option): | def AddCompilerOption(self, option): | ||||
self.compilerOptions.append(option) | self.compilerOptions.append(option) | ||||
def GetCompilerOptions(self): | def GetCompilerOptions(self): | ||||
Lint: PEP8 E301 expected 1 blank line, found 0 Lint: PEP8 E301: expected 1 blank line, found 0 | |||||
return self.compilerOptions | return self.compilerOptions | ||||
def AddLinkerLib(self, lib): | def AddLinkerLib(self, lib): | ||||
self.linkerLibs.append(lib) | self.linkerLibs.append(lib) | ||||
def GetLinkerLibs(self): | def GetLinkerLibs(self): | ||||
Lint: PEP8 E301 expected 1 blank line, found 0 Lint: PEP8 E301: expected 1 blank line, found 0 | |||||
return self.linkerLibs | return self.linkerLibs | ||||
def checkextensions(unknown, extra_inis, prefix): | def checkextensions(unknown, extra_inis, prefix): | ||||
# Create a table of frozen extensions | # Create a table of frozen extensions | ||||
defaultMapName = os.path.join( os.path.split(sys.argv[0])[0], "extensions_win32.ini") | defaultMapName = os.path.join( os.path.split(sys.argv[0])[0], "extensions_win32.ini") | ||||
if not os.path.isfile(defaultMapName): | if not os.path.isfile(defaultMapName): | ||||
sys.stderr.write("WARNING: %s can not be found - standard extensions may not be found\n" % defaultMapName) | sys.stderr.write("WARNING: %s can not be found - standard extensions may not be found\n" % defaultMapName) | ||||
else: | else: | ||||
# must go on end, so other inis can override. | # must go on end, so other inis can override. | ||||
extra_inis.append(defaultMapName) | extra_inis.append(defaultMapName) | ||||
ret = [] | ret = [] | ||||
for mod in unknown: | for mod in unknown: | ||||
for ini in extra_inis: | for ini in extra_inis: | ||||
# print "Looking for", mod, "in", win32api.GetFullPathName(ini),"...", | # print "Looking for", mod, "in", win32api.GetFullPathName(ini),"...", | ||||
Lint: PEP8 E115 expected an indented block (comment) Lint: PEP8 E115: expected an indented block (comment) | |||||
defn = get_extension_defn( mod, ini, prefix ) | defn = get_extension_defn( mod, ini, prefix ) | ||||
if defn is not None: | if defn is not None: | ||||
# print "Yay - found it!" | # print "Yay - found it!" | ||||
Lint: PEP8 E115 expected an indented block (comment) Lint: PEP8 E115: expected an indented block (comment) | |||||
ret.append( defn ) | ret.append( defn ) | ||||
break | break | ||||
# print "Nope!" | # print "Nope!" | ||||
else: # For not broken! | else: # For not broken! | ||||
sys.stderr.write("No definition of module %s in any specified map file.\n" % (mod)) | sys.stderr.write("No definition of module %s in any specified map file.\n" % (mod)) | ||||
return ret | return ret | ||||
def get_extension_defn(moduleName, mapFileName, prefix): | def get_extension_defn(moduleName, mapFileName, prefix): | ||||
if win32api is None: return None | if win32api is None: return None | ||||
Lint: PEP8 E701 multiple statements on one line (colon) Lint: PEP8 E701: multiple statements on one line (colon) | |||||
os.environ['PYTHONPREFIX'] = prefix | os.environ['PYTHONPREFIX'] = prefix | ||||
dsp = win32api.GetProfileVal(moduleName, "dsp", "", mapFileName) | dsp = win32api.GetProfileVal(moduleName, "dsp", "", mapFileName) | ||||
if dsp=="": | if dsp=="": | ||||
return None | return None | ||||
# We allow environment variables in the file name | # We allow environment variables in the file name | ||||
dsp = win32api.ExpandEnvironmentStrings(dsp) | dsp = win32api.ExpandEnvironmentStrings(dsp) | ||||
# If the path to the .DSP file is not absolute, assume it is relative | # If the path to the .DSP file is not absolute, assume it is relative | ||||
Show All 28 Lines | for exc in exclude: | ||||
if exc in module.sourceFiles: | if exc in module.sourceFiles: | ||||
modules.sourceFiles.remove(exc) | modules.sourceFiles.remove(exc) | ||||
return module | return module | ||||
# Given an MSVC DSP file, locate C source files it uses | # Given an MSVC DSP file, locate C source files it uses | ||||
# returns a list of source files. | # returns a list of source files. | ||||
def parse_dsp(dsp): | def parse_dsp(dsp): | ||||
# print "Processing", dsp | # print "Processing", dsp | ||||
Lint: PEP8 E115 expected an indented block (comment) Lint: PEP8 E115: expected an indented block (comment) | |||||
# For now, only support | # For now, only support | ||||
ret = [] | ret = [] | ||||
dsp_path, dsp_name = os.path.split(dsp) | dsp_path, dsp_name = os.path.split(dsp) | ||||
try: | try: | ||||
lines = open(dsp, "r").readlines() | lines = open(dsp, "r").readlines() | ||||
except IOError, msg: | except IOError as msg: | ||||
sys.stderr.write("%s: %s\n" % (dsp, msg)) | sys.stderr.write("%s: %s\n" % (dsp, msg)) | ||||
return None | return None | ||||
for line in lines: | for line in lines: | ||||
fields = line.strip().split("=", 2) | fields = line.strip().split("=", 2) | ||||
if fields[0]=="SOURCE": | if fields[0]=="SOURCE": | ||||
if os.path.splitext(fields[1])[1].lower() in ['.cpp', '.c']: | if os.path.splitext(fields[1])[1].lower() in ['.cpp', '.c']: | ||||
ret.append( win32api.GetFullPathName(os.path.join(dsp_path, fields[1] ) ) ) | ret.append( win32api.GetFullPathName(os.path.join(dsp_path, fields[1] ) ) ) | ||||
return ret | return ret | ||||
def write_extension_table(fname, modules): | def write_extension_table(fname, modules): | ||||
fp = open(fname, "w") | fp = open(fname, "w") | ||||
try: | try: | ||||
fp.write (ext_src_header) | fp.write (ext_src_header) | ||||
Lint: PEP8 E211 whitespace before '(' Lint: PEP8 E211: whitespace before '(' | |||||
# Write fn protos | # Write fn protos | ||||
for module in modules: | for module in modules: | ||||
# bit of a hack for .pyd's as part of packages. | # bit of a hack for .pyd's as part of packages. | ||||
name = module.name.split('.')[-1] | name = module.name.split('.')[-1] | ||||
fp.write('extern void init%s(void);\n' % (name) ) | fp.write('extern void init%s(void);\n' % (name) ) | ||||
# Write the table | # Write the table | ||||
fp.write (ext_tab_header) | fp.write (ext_tab_header) | ||||
Lint: PEP8 E211 whitespace before '(' Lint: PEP8 E211: whitespace before '(' | |||||
for module in modules: | for module in modules: | ||||
name = module.name.split('.')[-1] | name = module.name.split('.')[-1] | ||||
fp.write('\t{"%s", init%s},\n' % (name, name) ) | fp.write('\t{"%s", init%s},\n' % (name, name) ) | ||||
fp.write (ext_tab_footer) | fp.write (ext_tab_footer) | ||||
Lint: PEP8 E211 whitespace before '(' Lint: PEP8 E211: whitespace before '(' | |||||
fp.write(ext_src_footer) | fp.write(ext_src_footer) | ||||
finally: | finally: | ||||
fp.close() | fp.close() | ||||
ext_src_header = """\ | ext_src_header = """\ | ||||
#include "Python.h" | #include "Python.h" | ||||
""" | """ | ||||
Show All 21 Lines |
multiple imports on one line