Example #1
0
    def compute_score_en(self, s):
        x = DictWithDefault(lambda: 0)
        nspace = 0
        nalpha = 0
        nbad = 0
        n = len(s)
        for c in s:
            if isspace(c) or c == '_':
                nspace += 1
            elif isalpha(c):
                nalpha += 1
                x[c | 32] += 1
            elif not isgoodchar(c):
                nbad += 1

        tb = sorted(x.values())
        tb.reverse()
        score = 0
        if nalpha == 0:
            score += 1000
        else:
            for c, v in self.freq_en.items():
                score += (v - x[c] / nalpha)**2
        #score += (nspace / n - self.freq_sorted[0])**2
        score += (300 * nbad / n)**2
        return Attributize(score=score, nbad=nbad)
Example #2
0
class WafBuilder:
    PATH_SEPARATOR = '/'
    TARGET_SEPARATOR = '_'

    def __init__(self, var_globals):
        self.libs = opa_waf.libs
        self.packages = opa_waf.packages
        self._packages = []
        self.children = None
        self.typ = BuilderType
        self.ctx = None
        self._libs = []
        self._builders = []
        self._configure_list = []
        self.var_globals = var_globals
        self.a = opa_waf

        self.target_path = None

        data = self.var_globals['__waf_data']
        self.path = os.path.dirname(data['path'])
        self.stack_path = list(data['stack_path'])

        self.build_desc = DictWithDefault(lambda: [])

    def get(self, typ, pos=0):
        assert typ in self.build_desc, typ
        return self.build_desc[typ][pos]

    def list_dir(self, subdir):
        path = os.path.join(self.path, subdir)
        if not os.path.exists(path):
            return []
        return [os.path.join(subdir, x) for x in os.listdir(path)]

    def get_children(self):
        if self.children == None:
            self.children = []
            for x in self.list_dir('.'):
                t1 = os.path.join(self.path, x)
                t2 = os.path.join(t1, 'wscript')
                if os.path.isdir(t1) and os.path.exists(t2):
                    self.children.append(x)

        return self.children

    def get_target_path(self):
        if not self.target_path:
            n = len(self.stack_path) - 1
            tb = []
            cur = self.path
            for x in range(n):
                cur, tmp = os.path.split(cur)
                tb.append(tmp)
            tb.reverse()
            self.target_path = self.PATH_SEPARATOR.join(tb)
        return self.target_path

    def recurse(self):
        children = self.get_children()
        self.ctx.recurse(children)

    def has_child(self, path):
        return os.path.exists(os.path.join(self.path, path))

    def setup(self):
        for x in self.build_desc.values():
            for y in x:
                y.setup()

        lst = 'options build build2 configure run'.split(' ')
        for x in lst:
            val = WafBuilder.__dict__[x]

            def dispatcher2(val):
                return lambda ctx: self.dispatch(ctx, val)

            self.var_globals[x] = dispatcher2(val)

    def dispatch(self, ctx, func):
        self.ctx = ctx
        self.recurse()
        func(self)

    def get_target_name(self, name):
        path = '%s%s%s' % (self.get_target_path(), self.TARGET_SEPARATOR, name)
        res = path.replace('/', self.TARGET_SEPARATOR)
        return res

    def is_full_target(self, target):
        return bool(target.find('#'))

    def options(self):
        RunnerContext.options(self.ctx)
        opa_waf.options(self.ctx)

    def configure(self):
        opa_waf.configure(self.ctx)
        if not self.ctx.options.target_pin:
            self._packages.append(self.packages.Python)
        self._packages.append(self.packages.Swig)

        if self.ctx.options.arch == Target.X86_64:
            for x in self._packages:
                opa_waf.register(x, self.ctx)
        else:
            opa_waf.register(self.packages.Protoc, self.ctx)
            opa_waf.register(self.packages.Protobuf, self.ctx)
            opa_waf.register(self.packages.Gflags, self.ctx)

        for x in self._configure_list:
            x(self)

    def build(self):
        if len(self.stack_path) == 0:
            self.getter = TgenGetter(self.ctx)

        self.auto_builder()
        for x in self._builders:
            x(self)

    def build2(self):
        pass

    def run(self):
        pass

    def normalize_name(self, name):
        if name.startswith('@'):
            name = self.libs.get(name)
        elif not self.is_full_target(name):
            name = self.get_target_name(name)
        return name

    def add_builders(self, builders):
        self._builders.extend(to_list(builders))

    def auto(self):
        self.auto_asm()
        self.auto_proto()
        self.auto_lib()
        self.auto_lib_python()
        self.auto_proto_python()
        self.auto_swig_h()
        self.auto_swig()

        self.auto_sample()
        self.auto_test()

    def register_packages(self, *packages):
        self._packages.extend(flatten(packages))

    def proc_libs(self, *libs):
        packages = []
        use_libs = []
        libs = flatten(libs)

        for x in to_list(libs):
            if isinstance(x, WafBasePkg):
                packages.append(x)
                use_libs.append(x.name)
            elif isinstance(x, WafPkgList):
                packages.extend(x.tb)
                use_libs.extend([a.name for a in x.tb])
            else:
                use_libs.append(x)
        self.register_packages(packages)
        return use_libs

    def register_libs(self, *libs):
        self._libs.extend(self.proc_libs(*libs))

    def add_name(self, name, subtarget):
        self.libs.set(name, self.get_target_name(subtarget))

    def add_configure(self, configures):
        configures = to_list(configures)
        self._configure_list.extend(configures)

    def auto_builder(self):
        for x in list(self.build_desc.values()):
            for y in to_list(x):
                self.do_build(y, self._libs)

    def get_libs(self, *add):
        lst = list(self._libs)
        lst.extend(flatten(add))
        return lst

    def do_build(self, desc, libs):
        if desc.use_global:
            desc.update(libs=libs)
        return desc.build()

    def create_conf(self, typ, extra_qual=''):
        build_typ_map = {}
        build_typ_map[BuilderType.ASM] = 'asm'
        build_typ_map[BuilderType.LIB] = 'lib'
        build_typ_map[BuilderType.TEST] = 'test'
        build_typ_map[BuilderType.SAMPLE] = 'sample'
        build_typ_map[BuilderType.PROTO] = 'proto_cpp'
        build_typ_map[BuilderType.PROTO_PYTHON] = 'proto_py'
        build_typ_map[BuilderType.SWIG] = 'swig'
        build_typ_map[BuilderType.SWIG_H] = 'header_swig'

        if not typ in self.build_desc:
            self.build_desc[typ] = []

        qual = build_typ_map[typ]
        if len(extra_qual) > 0:
            qual = '%s_%s' % (qual, extra_qual)
        conf = ExtraConf(self, qual, typ)
        self.build_desc[typ].append(conf)

        return conf

    def auto_lib(self,
                 base_dir='.',
                 include_root=False,
                 filter_func=None,
                 **kwargs):
        srcs, headers = opa_waf.get_files(os.path.join(self.path, base_dir),
                                          include_root)
        if filter_func:
            srcs = query(srcs).where(filter_func).to_list()
            headers = query(srcs).where(filter_func).to_list()

        ipath = ['./include-internal']
        eipath = ['./inc', './include']

        ipath = [os.path.join(base_dir, x) for x in ipath]
        eipath = [os.path.join(base_dir, x) for x in eipath]
        if include_root:
            eipath.append(base_dir)
            ipath.append(base_dir)

        num_inc_dir = query(eipath).where(lambda x: self.has_child(x)).count()
        found = False

        if len(srcs) + len(headers) == 0 and num_inc_dir == 0:
            return

        return self.create_conf(BuilderType.LIB,
                                **kwargs).update(includes=ipath + eipath,
                                                 exports=eipath,
                                                 sources=srcs,
                                                 headers=headers)

    def auto_lib_python(self, base_dir='.'):
        pass
        #srcs = opa_waf.get_python(os.path.join(self.path, base_dir))

        #num_inc_dir = query(eipath).where(lambda x: self.has_child(x)).count()
        #found = False

        #if len(srcs) + len(headers) == 0 and num_inc_dir == 0:
        #  return

        #return self.create_conf(BuilderType.LIB).update(includes=ipath + eipath,
        #                                                exports=eipath,
        #                                                sources=srcs,
        #                                                headers=headers)

    def auto_test(self):
        srcs = './test/test.cpp'
        if not self.has_child(srcs):
            return []

        return self.create_conf(self.typ.TEST).update(libs=self.libs.GTest_N,
                                                      sources=srcs,
                                                      binary=True)

    def auto_proto_base(self, proto_base, features, typ):
        base, proto_files = opa_waf.get_proto(self.path, proto_base)
        if len(proto_files) == 0:
            return []
        export_includes = proto_base
        return self.create_conf(typ).update(
            libs=self.packages.Protobuf,
            includes=export_includes,
            exports=export_includes,
            sources=proto_files,
            features=[features, opa_proto_feature],
            install_from=proto_base,
            proto_base=proto_base)

    def auto_proto_python(self, proto_base='./proto/msg'):
        return self.auto_proto_base(proto_base, 'py', self.typ.PROTO_PYTHON)

    def auto_proto(self, proto_base='./proto/msg'):
        return self.auto_proto_base(proto_base, 'cxx', self.typ.PROTO)

    def auto_sample(self):
        dirlist = ['./samples', './sample']
        allowed_extensions = ['.cpp', '.cc', '.c', '.py']
        for dircnd in dirlist:
            for x in self.list_dir(dircnd):
                for e in allowed_extensions:
                    if x.endswith(e):
                        break
                else:
                    continue
                self.create_conf(self.typ.SAMPLE,
                                 os.path.basename(x)).update(sources=x,
                                                             binary=True)

    def auto_asm(self, base_dir='.'):
        base_dir = os.path.join(self.path, base_dir)
        srcs = opa_waf.get_asm(base_dir)
        if len(srcs) == 0:
            return []

        return self.create_conf(self.typ.ASM).update(
            sources=srcs, includes=['./inc', './src'], features='cxx cxxshlib')

    def auto_swig(self):
        srcs = opa_waf.get_swig(self.path)
        #print('FIND >> ', self.path, srcs)
        if len(srcs) == 0:
            return []

        def set_pattern(x):
            x.env.cxxshlib_PATTERN = '_%s.so'

        return self.create_conf(self.typ.SWIG).update(
            sources=srcs,
            libs=[self.libs.SwigCommon_N],
            includes=['./inc', './src', './swig'],
            exports=['./swig'],
            features='cxx cxxshlib pyembed',
            swig_flags='-c++ -python -I/usr/include',
            post=set_pattern)

    def auto_swig_h(self):
        return self.create_conf(BuilderType.SWIG_H).update(
            includes=['./swig'],
            libs=[self.libs.SwigCommon_N],
            exports=['./swig'],
            sources=[],
            headers=[])