Example #1
0
 def __init__(self, builder, basename, typ):
     self.typ = typ
     self.basename = basename
     self.builder = builder
     self.swig_flags = None
     self.proto_base = None
     self.clang = True
     self.binary = False
     self.post = None
     self.install = False
     self.install_path = None
     self.install_from = None
     self.use_global = True
     self.clear()
     self.deps = DictWithDefault(lambda: [])
     self.deps[BuilderType.LIB] = [BuilderType.PROTO, BuilderType.ASM]
     self.deps[BuilderType.TEST] = [BuilderType.LIB]
     self.deps[BuilderType.SAMPLE] = [BuilderType.LIB]
     self.deps[BuilderType.SWIG_H] = [BuilderType.LIB]
     self.deps[BuilderType.SWIG] = [BuilderType.LIB, BuilderType.SWIG_H]
     self.build_done = False
     for x in list(self.deps.keys()):
         new = set(self.deps[x])
         for y in self.deps[x]:
             new.update(self.deps[y])
         self.deps[x] = new
Example #2
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 #3
0
def analyse(base):

    n = 128
    data = base + [random.randint(0, 2 * n - 1) for i in range(n)]

    data = {i: data[i] for i in range(len(data))}
    idata = {v: k for k, v in data.items()}
    cnt = 0
    tot = DictWithDefault(0)
    seen = {}
    for i in range(n):
        lst, typ = get_cycle(data, i, n)
        if typ == 0:
            if not lst[-2] in seen:
                seen[lst[-2]] = 1
                tot[lst[-1]] += 1
            cnt += 1
            print(len(lst))
    tb = []
    print('####')
    for i in range(n, 2 * n):
        print(tot[i])
        if tot[i] == 0:
            tb.append(i)
    print(tb, len(tb))
    print(cnt)
Example #4
0
    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: [])
Example #5
0
class YamlStructView(object):
    def __init__(self, typ, ctx):
        self.typ = typ
        self.ctx = ctx
        self.ctx.update(accessors_ctx)
        self.elems = OrderedDict()
        self.processed = DictWithDefault(lambda: 0)

    def add_view(self, desc):
        view = YamlViewElement(self.typ, desc, self.ctx)
        self.elems[view.name] = view

    def load_views(self, desc):
        for x in desc.items():
            self.add_view(x)
        return self

    def get(self, *args, **kwargs):
        s = Structure(self.typ, *args, **kwargs)
        self.augment(s)
        return s

    def augment(self, s):
        self.processed.clear()
        vals = list(self.elems.values())
        vals.sort(key=lambda x: x.name)

        for e in vals:
            self._add_element(s, e)

    def _add_element(self, s, e):

        assert self.processed[e.name] != -1
        if self.processed[e.name] == 1: return
        self.processed[e.name] = -1
        for x in e.deps:
            if x in self.elems:
                self.add_element(s, self.elems[x])

        e.add_to_struct(s)
        self.processed[e.name] = 1
Example #6
0
 def __init__(self, buf=None, lazy=False, allocator=None):
     super().__init__(buf=buf)
     self.allocator = allocator
     self.lazy = lazy
     self.ops = DictWithDefault(default=lambda: [])
Example #7
0
class ExtraConf:
    def __init__(self, builder, basename, typ):
        self.typ = typ
        self.basename = basename
        self.builder = builder
        self.swig_flags = None
        self.proto_base = None
        self.clang = True
        self.binary = False
        self.post = None
        self.install = False
        self.install_path = None
        self.install_from = None
        self.use_global = True
        self.clear()
        self.deps = DictWithDefault(lambda: [])
        self.deps[BuilderType.LIB] = [BuilderType.PROTO, BuilderType.ASM]
        self.deps[BuilderType.TEST] = [BuilderType.LIB]
        self.deps[BuilderType.SAMPLE] = [BuilderType.LIB]
        self.deps[BuilderType.SWIG_H] = [BuilderType.LIB]
        self.deps[BuilderType.SWIG] = [BuilderType.LIB, BuilderType.SWIG_H]
        self.build_done = False
        for x in list(self.deps.keys()):
            new = set(self.deps[x])
            for y in self.deps[x]:
                new.update(self.deps[y])
            self.deps[x] = new

    def clear(self):
        self.includes = []
        self.libs = []
        self.exports = []
        self.sources = []
        self.features = []
        self.headers = []
        #not working
        self.deps_list = []

    def setup(self):
        self.libs = self.builder.proc_libs(self.libs)

    def update(self, register_name=None, append=True, **kwargs):

        for k, v in kwargs.items():
            assert hasattr(self, k)
            cur = getattr(self, k)

            if isinstance(cur, list):
                if append: cur += to_list(v)
                else:
                    cur.clear()
                    cur.extend(to_list(v))
            else:
                setattr(self, k, v)

        if register_name:
            print('registering name', register_name)
            self.builder.add_name(register_name, self.basename)
            if register_name in self.deps_list:
                self.deps_list.remove(register_name)

        return self

    def norm_path(self, source):
        if os.path.isabs(source):
            return os.path.relpath(source, self.builder.path)
        return source

    def guess_feature(self):

        features = self.features
        has_h = False
        for x in self.sources:
            if x.endswith('.cpp'):
                features.append('cxx')
            if x.endswith('.cc'):
                features.append('cxx')
            if x.endswith('.c'):
                features.append('c')
            if x.endswith('.S'):
                features.append('asm')
            if x.endswith('.py'):
                features.append('py')
            if x.endswith('.h'):
                has_h = True
        if len(features) == 0 and has_h:
            features.append('includes c')
        features = list(set(features))

        if self.binary:
            features.append('cprogram')
        else:
            if 'asm' in features:
                features.append('cxxshlib')
            else:
                if not 'cxxstlib' in features:
                    features.append('cxxshlib')

        tsf_features = []
        for feature in features:
            if feature == 'cxxshlib' and opa_waf.pin_mode:
                feature = 'pincxxshlib'
            tsf_features.append(feature)

        return tsf_features

    def build(self):
        if self.build_done: return
        self.build_done = 1

        if self.basename == None:
            assert len(self.sources) == 1
            self.basename = os.path.splitext(os.path.basename(
                self.sources[0]))[0]

        self.name = self.builder.get_target_name(self.basename)
        tb = Attributize({})
        print('GOT DEPS >> ', self.deps)
        if self.use_global:
            for x in self.deps[self.typ]:
                for j in self.builder.build_desc[x]:
                    print(type(j), j.basename, self.name)
                    j.build()
                    print(j.name)
                    self.libs.append(j.name)

        self.sources = flatten(self.sources, True)
        self.features = flatten(self.features, True)
        self.features = self.guess_feature()
        self.sources = [self.norm_path(x) for x in self.sources]
        self.deps_list = [
            self.builder.normalize_name(x) for x in self.deps_list
        ]
        self.libs = [self.builder.normalize_name(x) for x in self.libs]
        mapper = 'includes export_includes:exports target:name source:sources swig_flags proto_base use:libs features install_path install_from after:deps_list'
        for x in mapper.split(' '):
            y = x.split(':')
            if len(y) == 1:
                y.append(y[0])
            attr = getattr(self, y[1])
            if attr is None:
                continue
            tb[y[0]] = attr

        if self.install and not self.install_path:
            print('INSTALL >>> ', self.features, self.sources)
            if 'py' in self.features or 'pyembed' in self.features:
                tb.install_path = opa_waf.PYTHON_INSTALL_DIR
                print('INSTALL TO ', opa_waf.PYTHON_INSTALL_DIR)
            else:
                tb.install_path = '${PREFIX}/lib'

            tb['install_path'] = tb.install_path
        if not 'install_from' in tb:
            tb['install_from'] = self.builder.ctx.path.get_bld()
        else:
            before = tb['install_from']
            tb['install_from'] = self.builder.ctx.path.get_bld().make_node(
                before)
            assert tb[
                'install_from'] is not None, 'before=%s, curpath=%s %s %s' % (
                    before, self.builder.ctx.path.abspath(), self.sources,
                    self.features)

        tmp = tb._elem
        x = self.builder.ctx(**tmp)
        x.opa_data = self

        if 0 and self.install:
            if 'pyembed' in self.features:
                assert len(self.sources) == 1

                modulename = os.path.splitext(os.path.basename(
                    self.sources[0]))[0]
                filename = os.path.join(os.path.dirname(self.sources[0]),
                                        modulename + '.py')
                from waflib import Build
                self.builder.ctx.post_mode = Build.POST_LAZY
                self.builder.ctx.add_group()
                generated_py = self.builder.ctx.path.find_or_declare(filename)
                print('GOT FILE >> ', generated_py, filename, self.sources,
                      self.builder.ctx.path.get_bld().abspath())
                self.builder.ctx(feature='py',
                                 source=generated_py,
                                 name='kappa',
                                 install_path=tb.install_path,
                                 install_from=self.builder.ctx.path.get_bld())

        if self.post:
            self.post(x)
        if self.clang:
            wafClang.addHeaders(x, self.headers, self.sources)
Example #8
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=[])
Example #9
0
 def __init__(self, typ, ctx):
     self.typ = typ
     self.ctx = ctx
     self.ctx.update(accessors_ctx)
     self.elems = OrderedDict()
     self.processed = DictWithDefault(lambda: 0)