コード例 #1
0
def inspect(charm, force_styling=False):
    tw = utils.TermWriter(force_styling=force_styling)
    manp = charm / ".composer.manifest"
    comp = charm / "composer.yaml"
    if not manp.exists() or not comp.exists():
        return
    manifest = json.loads(manp.text())
    composer = yaml.load(comp.open())
    a, c, d = utils.delta_signatures(manp)

    # ordered list of layers used for legend
    layers = list(manifest['layers'])

    def get_depth(e):
        rel = e.relpath(charm)
        depth = len(rel.splitall()) - 2
        return rel, depth

    def get_suffix(rel):
        suffix = ""
        if rel in a:
            suffix = "+"
        elif rel in c:
            suffix = "*"
        return suffix

    def get_color(rel):
        # name of layer this belongs to
        color = tw.term.normal
        if rel in manifest['signatures']:
            layer = manifest['signatures'][rel][0]
            layer_key = layers.index(layer)
            color = getattr(tw, theme.get(layer_key, "normal"))
        else:
            if entry.isdir():
                color = tw.blue
        return color

    tw.write("Inspect %s\n" % composer["is"])
    for layer in layers:
        tw.write("# {color}{layer}{t.normal}\n",
                 color=getattr(tw, theme.get(layers.index(layer), "normal")),
                 layer=layer)
    tw.write("\n")
    tw.write("{t.blue}{target}{t.normal}\n", target=charm)

    ignorer = utils.ignore_matcher(config.DEFAULT_IGNORES)
    walk = sorted(utils.walk(charm, get_depth), key=lambda x: x[1][0])
    for i in range(len(walk) - 1):
        entry, (rel, depth) = walk[i]
        nEnt, (nrel, ndepth) = walk[i + 1]
        if not ignorer(rel):
            continue

        tw.write("{prefix}{layerColor}{entry} "
                 "{t.bold}{suffix}{t.normal}\n",
                 prefix=get_prefix(walk, i, depth, ndepth),
                 layerColor=get_color(rel),
                 suffix=get_suffix(rel),
                 entry=rel.name)
コード例 #2
0
    def test_delta_python(self):
        a = StringIO("""
        def foo(n):
            return n * 2


        @when('db.ready')
        def react(db):
            print(db)
        """)

        b = StringIO("""
        def foo(n):
            return n * 2


        @when('db.ready', 'bar')
        def react(db):
            print(db)
        """)

        result = StringIO()
        t = utils.TermWriter(fp=result)
        rc = utils.delta_python_dump(a,
                                     b,
                                     utils.REACTIVE_PATTERNS,
                                     context=3,
                                     term=t,
                                     from_name="Alpha",
                                     to_name="Beta")
        # return code here indicates that there was a diff
        self.assertFalse(rc)
        result.seek(0)
        output = result.read()
        self.assertIn("Alpha", output)
        self.assertIn("Beta", output)
        self.assertIn("@when('db.ready'", output)
        self.assertIn("bar", output)
コード例 #3
0
def inspect(charm, force_styling=False, annotate=False):
    tw = utils.TermWriter(force_styling=force_styling)
    manp = charm / ".build.manifest"
    comp = charm / "layer.yaml"
    if not manp.exists() or not comp.exists():
        return
    manifest = json.loads(manp.text())
    composer = yaml.safe_load(comp.open())
    a, c, d = utils.delta_signatures(manp)

    # ordered list of layers used for legend
    if isinstance(manifest['layers'][0], dict):
        layers = [layer['url'] for layer in manifest['layers']]
    else:
        layers = list(manifest['layers'])
    layers.reverse()
    while layers[0].startswith('interface:'):
        layers.append(layers.pop(0))

    def get_depth(e):
        rel = e.relpath(charm)
        depth = len(rel.splitall()) - 2
        return rel, depth

    def get_suffix(rel):
        suffix = ""
        if rel in a:
            suffix = "+"
        elif rel in c:
            suffix = "*"
        return suffix

    def get_color(rel):
        # name of layer this belongs to
        color = tw.term.normal
        if rel in manifest['signatures']:
            layer = manifest['signatures'][rel][0]
            if layer in layers:
                layer_key = layers.index(layer)
            else:
                # handle special build created artifacts, which have
                # a "layer name" of "build" (mostly the manifest itself)
                layer_key = -1
            color = getattr(tw, theme.get(layer_key, "normal"))
        else:
            if entry.isdir():
                color = tw.blue
        return color

    tw.write("Inspect %s\n" % composer["is"])
    if tw.does_styling or force_styling:
        tw.write("\n")
        tw.write("Color key:\n")
        for i, layer in enumerate(layers):
            tw.write("# {color}{layer}{t.normal}\n",
                     color=getattr(tw, theme.get(i, "normal")),
                     layer=layer)
    else:
        # force annotations if we can't use color
        annotate = True
    tw.write("\n")
    tw.write("{t.blue}{target}{t.normal}\n", target=charm)

    ignorer = utils.ignore_matcher(config.DEFAULT_IGNORES)
    walk = sorted(utils.walk(charm, get_depth), key=lambda x: x[1][0])
    for i in range(len(walk) - 1):
        entry, (rel, depth) = walk[i]
        nEnt, (nrel, ndepth) = walk[i + 1]
        if not ignorer(rel):
            continue

        if annotate and rel in manifest['signatures']:
            layer_name = manifest['signatures'][rel][0]
            if layer_name == 'build':
                # handle special build created artifacts, which have
                # a "layer name" of "build" (mostly the manifest itself)
                annotation = ' ({}build artifact{})'.format(
                    tw.bright_black, tw.normal)
            else:
                annotation = ' (from {}{}{})'.format(get_color(rel),
                                                     layer_name, tw.normal)
        else:
            annotation = ''
        tw.write(
            "{prefix}{layerColor}{entry} "
            "{t.bold}{suffix}{t.normal}{annotation}\n",
            prefix=get_prefix(walk, i, depth, ndepth),
            layerColor=get_color(rel),
            suffix=get_suffix(rel),
            entry=rel.name,
            annotation=annotation)