codekingpro/portable-devtools
114k
1import sys, os2from .error import VerificationError3 4 5LIST_OF_FILE_NAMES = ['sources', 'include_dirs', 'library_dirs',6 'extra_objects', 'depends']7 8def get_extension(srcfilename, modname, sources=(), **kwds):9 from cffi._shimmed_dist_utils import Extension10 allsources = [srcfilename]11 for src in sources:12 allsources.append(os.path.normpath(src))13 return Extension(name=modname, sources=allsources, **kwds)14 15def compile(tmpdir, ext, compiler_verbose=0, debug=None):16 """Compile a C extension module using distutils."""17 18 saved_environ = os.environ.copy()19 try:20 outputfilename = _build(tmpdir, ext, compiler_verbose, debug)21 outputfilename = os.path.abspath(outputfilename)22 finally:23 # workaround for a distutils bugs where some env vars can24 # become longer and longer every time it is used25 for key, value in saved_environ.items():26 if os.environ.get(key) != value:27 os.environ[key] = value28 return outputfilename29 30def _build(tmpdir, ext, compiler_verbose=0, debug=None):31 # XXX compact but horrible :-(32 from cffi._shimmed_dist_utils import Distribution, CompileError, LinkError, set_threshold, set_verbosity33 34 dist = Distribution({'ext_modules': [ext]})35 dist.parse_config_files()36 options = dist.get_option_dict('build_ext')37 if debug is None:38 debug = sys.flags.debug39 options['debug'] = ('ffiplatform', debug)40 options['force'] = ('ffiplatform', True)41 options['build_lib'] = ('ffiplatform', tmpdir)42 options['build_temp'] = ('ffiplatform', tmpdir)43 #44 try:45 old_level = set_threshold(0) or 046 try:47 set_verbosity(compiler_verbose)48 dist.run_command('build_ext')49 cmd_obj = dist.get_command_obj('build_ext')50 [soname] = cmd_obj.get_outputs()51 finally:52 set_threshold(old_level)53 except (CompileError, LinkError) as e:54 raise VerificationError('%s: %s' % (e.__class__.__name__, e))55 #56 return soname57 58try:59 from os.path import samefile60except ImportError:61 def samefile(f1, f2):62 return os.path.abspath(f1) == os.path.abspath(f2)63 64def maybe_relative_path(path):65 if not os.path.isabs(path):66 return path # already relative67 dir = path68 names = []69 while True:70 prevdir = dir71 dir, name = os.path.split(prevdir)72 if dir == prevdir or not dir:73 return path # failed to make it relative74 names.append(name)75 try:76 if samefile(dir, os.curdir):77 names.reverse()78 return os.path.join(*names)79 except OSError:80 pass81 82# ____________________________________________________________83 84try:85 int_or_long = (int, long)86 import cStringIO87except NameError:88 int_or_long = int # Python 389 import io as cStringIO90 91def _flatten(x, f):92 if isinstance(x, str):93 f.write('%ds%s' % (len(x), x))94 elif isinstance(x, dict):95 keys = sorted(x.keys())96 f.write('%dd' % len(keys))97 for key in keys:98 _flatten(key, f)99 _flatten(x[key], f)100 elif isinstance(x, (list, tuple)):101 f.write('%dl' % len(x))102 for value in x:103 _flatten(value, f)104 elif isinstance(x, int_or_long):105 f.write('%di' % (x,))106 else:107 raise TypeError(108 "the keywords to verify() contains unsupported object %r" % (x,))109 110def flatten(x):111 f = cStringIO.StringIO()112 _flatten(x, f)113 return f.getvalue()114 