diff --git a/devops/scripts/benchmarks/main.py b/devops/scripts/benchmarks/main.py index 397632e138978..4321e228c0b34 100755 --- a/devops/scripts/benchmarks/main.py +++ b/devops/scripts/benchmarks/main.py @@ -356,7 +356,12 @@ def validate_and_parse_env_args(env_args): ) parser.add_argument( "--save", - type=str, + type=lambda save: Validate.save_name( + save, + throw=argparse.ArgumentTypeError( + "Specified save name is not within characters [a-zA-Z0-9_-]." + ), + ), help="Save the results for comparison under a specified name.", ) parser.add_argument( diff --git a/devops/scripts/benchmarks/utils/validate.py b/devops/scripts/benchmarks/utils/validate.py index b0a2658865562..afdd914eecf63 100644 --- a/devops/scripts/benchmarks/utils/validate.py +++ b/devops/scripts/benchmarks/utils/validate.py @@ -8,7 +8,7 @@ def validate_on_re(val: str, regex: re.Pattern, throw: Exception = None): If `throw` argument is not None: return val as-is if val matches regex, otherwise raise error defined by throw. """ - is_matching: bool = re.compile(regex).match(val) is not None + is_matching: bool = re.compile(regex).match(val.strip()) is not None if throw is None: return is_matching @@ -28,6 +28,16 @@ def runner_name(runner_name: str, throw: Exception = None): """ return validate_on_re(runner_name, r"^[a-zA-Z0-9_]+$", throw=throw) + @staticmethod + def save_name(save: str, throw: Exception = None): + """ + Returns True if save is within [a-zA-Z0-9_-]. + + If throw argument is specified: return save as is if save satisfies + aforementioned regex, otherwise raise error defined by throw. + """ + return validate_on_re(save, r"^[a-zA-Z0-9_-]+$", throw=throw) + @staticmethod def timestamp(t: str, throw: Exception = None): """