Exemple #1
0
    def NotifyReviewEvent(self, request, context):
        print("got review request {}".format(request))

        comments = []

        # client connection to DataServe
        with create_channel(data_srv_addr) as channel:
            stub = pb.DataStub(channel)
            changes = stub.GetChanges(
                pb.ChangesRequest(head=request.commit_revision.head,
                                  base=request.commit_revision.base,
                                  want_contents=False,
                                  want_uast=True,
                                  exclude_vendored=True))

            for change in changes:
                if not change.HasField("head"):
                    continue

                print("analyzing '{}' in {}".format(change.head.path,
                                                    change.head.language))
                fns = list(filter_uast(change.head.uast, "//*[@roleFunction]"))
                text = "language: {}, functions: {}".format(
                    change.head.language, len(fns))
                comments.append(
                    pb.Comment(file=change.head.path, line=0, text=text))

        return pb.EventResponse(analyzer_version=version, comments=comments)
Exemple #2
0
    def NotifyReviewEvent(self, request, context):
        print("got review request {}".format(request))

        # client connection to DataServe
        channel = grpc.insecure_channel(data_srv_addr, options=[
                ("grpc.max_send_message_length", grpc_max_msg_size),
                ("grpc.max_receive_message_length", grpc_max_msg_size),
            ])
        stub = service_data_pb2_grpc.DataStub(channel)
        changes = stub.GetChanges(
            service_data_pb2.ChangesRequest(
                head=request.commit_revision.head,
                base=request.commit_revision.base,
                want_contents=False,
                want_uast=True,
                exclude_vendored=True))

        comments = []
        for change in changes:
            print("analyzing '{}' in {}".format(change.head.path, change.head.language))
            fns = list(filter_uast(change.head.uast, "//*[@roleFunction]"))
            comments.append(
                service_analyzer_pb2.Comment(
                    file=change.head.path,
                    line=0,
                    text="language: {}, functions: {}".format(change.head.language, len(fns))))
        return service_analyzer_pb2.EventResponse(analyzer_version=version, comments=comments)
def rule_chk(uast):
    findings = []

    query = "//TypeDeclaration[@interface='true']//FieldDeclaration"
    nodes = filter_uast(uast, query)
    for n in nodes:
        #print(n)
        findings.append({
            "msg": "Interface should not define a constant",
            "pos": n.start_position
        })

    return findings
def rule_chk(uast):
    findings = []
    is_left_literal = False
    is_right_literal = False

    left_node_pos = None
    right_node_pos = None

    #query = "//WhileStatement//InfixExpression"
    #query = "//*[@roleWhile and @roleBinary and @roleCondition and  @roleExpression]"
    #query = "//*[@roleWhile and @roleStatement and not(@roleBody)]"
    query = "//*[@roleWhile and @roleStatement and not(@roleBody)]//*[@roleRelational and @roleExpression and @roleBinary and @roleOperator] "
    print(query)
    node = filter_uast(uast, query)
    i = 0
    for n in node:
        i = i + 1
        print('Node :{0}'.format(i))
        is_left_literal = False
        is_right_literal = False

        left_node_pos = None
        right_node_pos = None

        j = 0
        for child in n.children:
            j = j + 1
            print('Iteration {0} for node {1} '.format(j, i))
            #print(child)

            if (bblfsh.role_id("NUMBER")
                    in child.roles) & (bblfsh.role_id("LEFT") in child.roles):
                is_left_literal = True
                left_node_pos = child.start_position.line

            if (bblfsh.role_id("NUMBER")
                    in child.roles) & (bblfsh.role_id("RIGHT") in child.roles):
                is_right_literal = True
                right_node_pos = child.start_position.line

        if is_left_literal & is_right_literal:
            findings.append({
                "msg": "Number literals found in while condition",
                "left literal at line": left_node_pos,
                "right literal at line": right_node_pos
            })

    return findings
Exemple #5
0
    def notify_review_event(self, request, context):
        print("got review request {}".format(request))

        comments = []

        # client connection to DataServe
        with create_channel(data_srv_addr,
                            interceptors=[
                                LogUnaryClientInterceptor(log_fn),
                                LogStreamClientInterceptor(log_fn),
                            ]) as channel:
            stub = DataStub(channel)

            # Add some log fields that will be available to the data server
            # using `context.add_log_fields`.
            context.add_log_fields({
                "some-string-key": "some-value",
                "some-int-key": 1,
            })

            changes = stub.get_changes(
                context,
                pb.ChangesRequest(head=request.commit_revision.head,
                                  base=request.commit_revision.base,
                                  want_contents=False,
                                  want_uast=True,
                                  exclude_vendored=True))

            for change in changes:
                if not change.HasField("head"):
                    continue

                print("analyzing '{}' in {}".format(change.head.path,
                                                    change.head.language))
                fns = list(filter_uast(change.head.uast, "//*[@roleFunction]"))
                text = "language: {}, functions: {}".format(
                    change.head.language, len(fns))
                comments.append(
                    pb.Comment(file=change.head.path, line=0, text=text))

        return pb.EventResponse(analyzer_version=version, comments=comments)
Exemple #6
0
import bblfsh
import sys
import os

from bblfsh import filter as filter_uast
if __name__ == "__main__":

    #Initializing the babelfish client
    client = bblfsh.BblfshClient("0.0.0.0:9432")
    response = client.parse(sys.argv[1])

    node = filter_uast(response.uast, "//*[@roleFile]")
    #node = filter_uast(node,"//[@role='Left']")
    #node_list = list(node)

    print("Node type : ", type(node))

    i = 0
    for n in node:
        i = i + 1
        #while_nodes = filter_uast(n,"//*[@roleWhile and @roleStatement]")
        #if len(list(while_nodes)) > 0 :
        #	print("While loop exists!!")

        if os.path.exists("uast.txt"):
            os.remove("uast.txt")

        with open("uast.txt", "a") as f:
            f.write("{}\n".format(n))

    print("out of loop: number of iterations :", str(i))