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)
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 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.
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")
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 '%s'", 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 %s:%d/%s done.", host, port, product_name) _, zip_file = tempfile.mkstemp('.zip') LOG.debug("Will write mass store ZIP to '%s'...", 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: %s).", sizeof_fmt(MAX_UPLOAD_SIZE)) sys.exit(1) with open(zip_file, 'rb') as zf: b64zip = base64.b64encode(zf.read()) context = webserver_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) # Storing analysis statistics if the server allows them. if client.allowsStoringAnalysisStatistics(): storing_analysis_statistics(client, args.input, args.name) 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: " "%s\n %s", reqfail.message, table) sys.exit(1) except Exception as ex: LOG.info("Storage failed: %s", str(ex)) sys.exit(1) finally: os.remove(zip_file)