def test(name, protocol=None):
            url = ''.join([protocol + "://" if protocol else "", name])

            sprotocol, shost, sport, sname = split_product_url(url)
            self.assertEqual(sprotocol, protocol if protocol else "http")
            self.assertEqual(shost, "localhost")
            self.assertEqual(sport, 8001)
            self.assertEqual(sname, name)
        def test(name, protocol=None):
            url = ''.join([protocol + "://" if protocol else "", name])

            sprotocol, shost, sport, sname = split_product_url(url)
            self.assertEqual(sprotocol, expected_protocol(protocol, None))
            self.assertEqual(shost, "localhost")
            self.assertEqual(sport, expected_port(protocol, None))
            self.assertEqual(sname, name)
        def test(host, port, name, protocol=None):
            url = ''.join([protocol + "://" if protocol else "",
                           host, ":", str(port), "/", name])

            sprotocol, shost, sport, sname = split_product_url(url)
            self.assertEqual(sprotocol, expected_protocol(protocol, port))
            self.assertEqual(shost, host)
            self.assertEqual(sport, expected_port(protocol, port))
            self.assertEqual(sname, name)
示例#4
0
def setup_auth_client_from_url(product_url, session_token=None):
    """
    Setup a Thrift authentication client to the server pointed by the given
    product URL.
    """
    try:
        protocol, host, port, _ = split_product_url(product_url)
        return setup_auth_client(protocol, host, port, session_token)
    except:
        LOG.error("Malformed product URL was provided.")
        sys.exit(2)  # 2 for argument error.
示例#5
0
def setup_auth_client_from_url(product_url, session_token=None):
    """
    Setup a Thrift authentication client to the server pointed by the given
    product URL.
    """
    try:
        protocol, host, port, _ = split_product_url(product_url)
        return setup_auth_client(protocol, host, port, session_token)
    except ValueError:
        LOG.error("Malformed product URL was provided. A valid product URL "
                  "looks like this: 'http://my.server.com:80/ProductName'.")
        sys.exit(2)  # 2 for argument error.
示例#6
0
def setup_client(product_url, product_client=False):
    """
    Setup the Thrift Product or Service client and
    check API version and authentication needs.
    """

    try:
        protocol, host, port, product_name = split_product_url(product_url)
    except ValueError:
        LOG.error("Malformed product URL was provided. A valid product URL "
                  "looks like this: 'http://my.server.com:80/ProductName'.")
        sys.exit(2)  # 2 for argument error.

    _, session_token = setup_auth_client(protocol, host, port)

    # Check if the product exists.
    client = setup_product_client(protocol, host, port, product_name=None)
    product = client.getProducts(product_name, None)
    product_error_str = None
    if not product:
        product_error_str = "It does not exist."
    elif len(product) != 1:
        product_error_str = "Multiple products can be found with the given " \
                            "name."
    else:
        if product[0].endpoint != product_name:
            # Only a "substring" match was found. We explicitly reject it
            # on the command-line!
            product_error_str = "It does not exist."

        elif not product[0].accessible:
            product_error_str = "You do not have access."

        elif product[0].databaseStatus != shared.ttypes.DBStatus.OK:
            product_error_str = "The database has issues, or the connection " \
                                "is badly configured."

    if product_error_str:
        LOG.error("The given product '%s' can not be used! %s", product_name,
                  product_error_str)
        sys.exit(1)

    if product_client:
        LOG.debug("returning product client")
        return client
    else:
        LOG.debug("returning service client")
        # Service client was requested, setup
        # and return it.
        return setup_service_client(protocol, host, port, product_name,
                                    session_token)
    def testBadProductNames(self):
        """
        Parser throws on bad product URLs?
        """

        with self.assertRaises(ValueError):
            split_product_url("123notaproductname")

        with self.assertRaises(ValueError):
            split_product_url("localhost//containsExtraChar")

        with self.assertRaises(ValueError):
            split_product_url("in:valid:format/product")

        with self.assertRaises(ValueError):
            split_product_url("localhost:12PortIsANumber34/Foo")
示例#8
0
def setup_client(product_url):
    """
    Setup the Thrift client and check API version and authentication needs.
    """

    try:
        protocol, host, port, product_name = split_product_url(product_url)
    except:
        LOG.error("Malformed product URL was provided. A valid product URL "
                  "looks like this: 'http://my.server.com:80/ProductName'.")
        sys.exit(2)  # 2 for argument error.

    _, session_token = setup_auth_client(protocol, host, port)

    # Check if the product exists.
    product_client = setup_product_client(protocol,
                                          host,
                                          port,
                                          product_name=None)
    product = product_client.getProducts(product_name, None)
    product_error_str = None
    if not (product and len(product) == 1):
        product_error_str = "It does not exist."
    else:
        if product[0].endpoint != product_name:
            # Only a "substring" match was found. We explicitly reject it
            # on the command-line!
            product_error_str = "It does not exist."
        elif not product[0].connected:
            product_error_str = "The database has issues, or the connection " \
                                "is badly configured."
        elif not product[0].accessible:
            product_error_str = "You do not have access."

    if product_error_str:
        LOG.error("The given product '{0}' can not be used! {1}".format(
            product_name, product_error_str))
        sys.exit(1)

    client = thrift_helper.ThriftClientHelper(
        protocol, host, port,
        '/' + product_name + '/v' + CLIENT_API + '/CodeCheckerService',
        session_token)

    return client
示例#9
0
def main(args):
    """
    Store the defect results in the specified input list as bug reports in the
    database.
    """
    logger.setup_logger(args.verbose if 'verbose' in args else None)

    if not host_check.check_zlib():
        raise Exception("zlib is not available on the system!")

    # To ensure the help message prints the default folder properly,
    # the 'default' for 'args.input' is a string, not a list.
    # But we need lists for the foreach here to work.
    if isinstance(args.input, str):
        args.input = [args.input]

    if 'name' not in args:
        LOG.debug("Generating name for analysis...")
        generated = __get_run_name(args.input)
        if generated:
            setattr(args, 'name', generated)
        else:
            LOG.error("No suitable name was found in the inputs for the "
                      "analysis run. Please specify one by passing argument "
                      "--name run_name in the invocation.")
            sys.exit(2)  # argparse returns error code 2 for bad invocations.

    LOG.info("Storing analysis results for run '" + args.name + "'")

    if 'force' in args:
        LOG.info("argument --force was specified: the run with name '" +
                 args.name + "' will be deleted.")

    protocol, host, port, product_name = split_product_url(args.product_url)

    # Before any transmission happens, check if we have the PRODUCT_STORE
    # permission to prevent a possibly long ZIP operation only to get an
    # error later on.
    product_client = libclient.setup_product_client(protocol, host, port,
                                                    product_name)
    product_id = product_client.getCurrentProduct().id

    auth_client, _ = libclient.setup_auth_client(protocol, host, port)
    has_perm = libclient.check_permission(auth_client,
                                          Permission.PRODUCT_STORE,
                                          {'productID': product_id})
    if not has_perm:
        LOG.error("You are not authorised to store analysis results in "
                  "product '{0}'".format(product_name))
        sys.exit(1)

    # Setup connection to the remote server.
    client = libclient.setup_client(args.product_url, product_client=False)

    LOG.debug("Initializing client connecting to {0}:{1}/{2} done.".format(
        host, port, product_name))

    _, zip_file = tempfile.mkstemp('.zip')
    LOG.debug("Will write mass store ZIP to '{0}'...".format(zip_file))

    try:
        assemble_zip(args.input, zip_file, client)

        if os.stat(zip_file).st_size > MAX_UPLOAD_SIZE:
            LOG.error("The result list to upload is too big (max: {}).".format(
                sizeof_fmt(MAX_UPLOAD_SIZE)))
            sys.exit(1)

        with open(zip_file, 'rb') as zf:
            b64zip = base64.b64encode(zf.read())

        context = generic_package_context.get_context()

        trim_path_prefixes = args.trim_path_prefix if \
            'trim_path_prefix' in args else None

        client.massStoreRun(args.name, args.tag if 'tag' in args else None,
                            str(context.version), b64zip, 'force' in args,
                            trim_path_prefixes)

        LOG.info("Storage finished successfully.")
    except RequestFailed as reqfail:
        if reqfail.errorCode == ErrorCode.SOURCE_FILE:
            header = ['File', 'Line', 'Checker name']
            table = twodim_to_str('table', header,
                                  [c.split('|') for c in reqfail.extraInfo])
            LOG.warning("Setting the review statuses for some reports failed "
                        "because of non valid source code comments: "
                        "{0}\n {1}".format(reqfail.message, table))
        sys.exit(1)
    except Exception as ex:
        LOG.info("Storage failed: " + str(ex))
        sys.exit(1)
    finally:
        os.remove(zip_file)
示例#10
0
def main(args):
    """
    Store the defect results in the specified input list as bug reports in the
    database.
    """

    if not host_check.check_zlib():
        raise Exception("zlib is not available on the system!")

    # To ensure the help message prints the default folder properly,
    # the 'default' for 'args.input' is a string, not a list.
    # But we need lists for the foreach here to work.
    if isinstance(args.input, str):
        args.input = [args.input]

    if 'name' not in args:
        LOG.debug("Generating name for analysis...")
        generated = __get_run_name(args.input)
        if generated:
            setattr(args, 'name', generated)
        else:
            LOG.error("No suitable name was found in the inputs for the "
                      "analysis run. Please specify one by passing argument "
                      "--name run_name in the invocation.")
            sys.exit(2)  # argparse returns error code 2 for bad invocations.

    LOG.info("Storing analysis results for run '" + args.name + "'")

    if 'force' in args:
        LOG.info("argument --force was specified: the run with name '" +
                 args.name + "' will be deleted.")

    protocol, host, port, product_name = split_product_url(args.product_url)

    # Before any transmission happens, check if we have the PRODUCT_STORE
    # permission to prevent a possibly long ZIP operation only to get an
    # error later on.
    product_client = libclient.setup_product_client(protocol,
                                                    host, port, product_name)
    product_id = product_client.getCurrentProduct().id

    auth_client, _ = libclient.setup_auth_client(protocol, host, port)
    has_perm = libclient.check_permission(
        auth_client, Permission.PRODUCT_STORE, {'productID': product_id})
    if not has_perm:
        LOG.error("You are not authorised to store analysis results in "
                  "product '{0}'".format(product_name))
        sys.exit(1)

    # Setup connection to the remote server.
    client = libclient.setup_client(args.product_url)

    LOG.debug("Initializing client connecting to {0}:{1}/{2} done."
              .format(host, port, product_name))

    _, zip_file = tempfile.mkstemp('.zip')
    LOG.debug("Will write mass store ZIP to '{0}'...".format(zip_file))

    try:
        assemble_zip(args.input, zip_file, client)
        with open(zip_file, 'rb') as zf:
            b64zip = base64.b64encode(zf.read())

        context = generic_package_context.get_context()

        client.massStoreRun(args.name,
                            args.tag if 'tag' in args else None,
                            str(context.version),
                            b64zip,
                            'force' in args)

        LOG.info("Storage finished successfully.")
    except Exception as ex:
        LOG.info("Storage failed: " + str(ex))
        sys.exit(1)
    finally:
        os.remove(zip_file)
    def testBadProductNames(self):
        """
        Parser throws on bad product URLs?
        """

        with self.assertRaises(ValueError):
            split_product_url("123notaproductname")

        with self.assertRaises(ValueError):
            split_product_url("localhost//containsExtraChar")

        with self.assertRaises(ValueError):
            split_product_url("in:valid:format/product")

        with self.assertRaises(ValueError):
            split_product_url("localhost:12PortIsANumber34/Foo")

        with self.assertRaises(ValueError):
            split_product_url("codechecker://codecheker.com/Baz")

        with self.assertRaises(ValueError):
            # Make sure we don't understand "https://foobar.baz:1234" as
            # HTTPS, localhost, 443, and "foobar.baz:1234" product name.
            split_product_url("https://foobar.bar:1234")