Techdebt: Replace string-format with f-strings (for b* dirs) (#5657)
This commit is contained in:
parent
222621fe94
commit
e7850246ee
@ -609,7 +609,7 @@ class Job(threading.Thread, BaseModel, DockerModel, ManagedState):
|
|||||||
)
|
)
|
||||||
for m in self._get_container_property("mountPoints", [])
|
for m in self._get_container_property("mountPoints", [])
|
||||||
]
|
]
|
||||||
name = "{0}-{1}".format(self.job_name, self.job_id)
|
name = f"{self.job_name}-{self.job_id}"
|
||||||
|
|
||||||
self.advance()
|
self.advance()
|
||||||
while self.status == "PENDING":
|
while self.status == "PENDING":
|
||||||
@ -729,9 +729,7 @@ class Job(threading.Thread, BaseModel, DockerModel, ManagedState):
|
|||||||
|
|
||||||
# Send to cloudwatch
|
# Send to cloudwatch
|
||||||
log_group = "/aws/batch/job"
|
log_group = "/aws/batch/job"
|
||||||
stream_name = "{0}/default/{1}".format(
|
stream_name = f"{self.job_definition.name}/default/{self.job_id}"
|
||||||
self.job_definition.name, self.job_id
|
|
||||||
)
|
|
||||||
self.log_stream_name = stream_name
|
self.log_stream_name = stream_name
|
||||||
self._log_backend.ensure_log_group(log_group, None)
|
self._log_backend.ensure_log_group(log_group, None)
|
||||||
self._log_backend.create_log_stream(log_group, stream_name)
|
self._log_backend.create_log_stream(log_group, stream_name)
|
||||||
@ -745,20 +743,14 @@ class Job(threading.Thread, BaseModel, DockerModel, ManagedState):
|
|||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Failed to run AWS Batch container {0}. Error {1}".format(
|
f"Failed to run AWS Batch container {self.name}. Error {err}"
|
||||||
self.name, err
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self._mark_stopped(success=False)
|
self._mark_stopped(success=False)
|
||||||
container.kill()
|
container.kill()
|
||||||
finally:
|
finally:
|
||||||
container.remove()
|
container.remove()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.error(
|
logger.error(f"Failed to run AWS Batch container {self.name}. Error {err}")
|
||||||
"Failed to run AWS Batch container {0}. Error {1}".format(
|
|
||||||
self.name, err
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self._mark_stopped(success=False)
|
self._mark_stopped(success=False)
|
||||||
|
|
||||||
def _mark_stopped(self, success: bool = True) -> None:
|
def _mark_stopped(self, success: bool = True) -> None:
|
||||||
@ -806,9 +798,7 @@ class Job(threading.Thread, BaseModel, DockerModel, ManagedState):
|
|||||||
successful_dependencies.add(dependent_id)
|
successful_dependencies.add(dependent_id)
|
||||||
if dependent_job.status == "FAILED":
|
if dependent_job.status == "FAILED":
|
||||||
logger.error(
|
logger.error(
|
||||||
"Terminating job {0} due to failed dependency {1}".format(
|
f"Terminating job {self.name} due to failed dependency {dependent_job.name}"
|
||||||
self.name, dependent_job.name
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self._mark_stopped(success=False)
|
self._mark_stopped(success=False)
|
||||||
return False
|
return False
|
||||||
@ -1045,9 +1035,7 @@ class BatchBackend(BaseBackend):
|
|||||||
|
|
||||||
if self.get_compute_environment_by_name(compute_environment_name) is not None:
|
if self.get_compute_environment_by_name(compute_environment_name) is not None:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"A compute environment already exists with the name {0}".format(
|
f"A compute environment already exists with the name {compute_environment_name}"
|
||||||
compute_environment_name
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Look for IAM role
|
# Look for IAM role
|
||||||
@ -1055,24 +1043,22 @@ class BatchBackend(BaseBackend):
|
|||||||
self.iam_backend.get_role_by_arn(service_role)
|
self.iam_backend.get_role_by_arn(service_role)
|
||||||
except IAMNotFoundException:
|
except IAMNotFoundException:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"Could not find IAM role {0}".format(service_role)
|
f"Could not find IAM role {service_role}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if _type not in ("MANAGED", "UNMANAGED"):
|
if _type not in ("MANAGED", "UNMANAGED"):
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"type {0} must be one of MANAGED | UNMANAGED".format(service_role)
|
f"type {_type} must be one of MANAGED | UNMANAGED"
|
||||||
)
|
)
|
||||||
|
|
||||||
if state is not None and state not in ("ENABLED", "DISABLED"):
|
if state is not None and state not in ("ENABLED", "DISABLED"):
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"state {0} must be one of ENABLED | DISABLED".format(state)
|
f"state {state} must be one of ENABLED | DISABLED"
|
||||||
)
|
)
|
||||||
|
|
||||||
if compute_resources is None and _type == "MANAGED":
|
if compute_resources is None and _type == "MANAGED":
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"computeResources must be specified when creating a {0} environment".format(
|
f"computeResources must be specified when creating a {state} environment"
|
||||||
state
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
elif compute_resources is not None:
|
elif compute_resources is not None:
|
||||||
self._validate_compute_resources(compute_resources)
|
self._validate_compute_resources(compute_resources)
|
||||||
@ -1144,7 +1130,7 @@ class BatchBackend(BaseBackend):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"could not find instanceRole {0}".format(cr["instanceRole"])
|
f"could not find instanceRole {cr['instanceRole']}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if int(cr["minvCpus"]) < 0:
|
if int(cr["minvCpus"]) < 0:
|
||||||
@ -1166,13 +1152,13 @@ class BatchBackend(BaseBackend):
|
|||||||
and instance_type not in EC2_INSTANCE_FAMILIES
|
and instance_type not in EC2_INSTANCE_FAMILIES
|
||||||
):
|
):
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"Instance type {0} does not exist".format(instance_type)
|
f"Instance type {instance_type} does not exist"
|
||||||
)
|
)
|
||||||
|
|
||||||
for sec_id in cr["securityGroupIds"]:
|
for sec_id in cr["securityGroupIds"]:
|
||||||
if self.ec2_backend.get_security_group_from_id(sec_id) is None:
|
if self.ec2_backend.get_security_group_from_id(sec_id) is None:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"security group {0} does not exist".format(sec_id)
|
f"security group {sec_id} does not exist"
|
||||||
)
|
)
|
||||||
if len(cr["securityGroupIds"]) == 0:
|
if len(cr["securityGroupIds"]) == 0:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
@ -1184,7 +1170,7 @@ class BatchBackend(BaseBackend):
|
|||||||
self.ec2_backend.get_subnet(subnet_id)
|
self.ec2_backend.get_subnet(subnet_id)
|
||||||
except InvalidSubnetIdError:
|
except InvalidSubnetIdError:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"subnet {0} does not exist".format(subnet_id)
|
f"subnet {subnet_id} does not exist"
|
||||||
)
|
)
|
||||||
if len(cr["subnets"]) == 0:
|
if len(cr["subnets"]) == 0:
|
||||||
raise InvalidParameterValueException("At least 1 subnet must be provided")
|
raise InvalidParameterValueException("At least 1 subnet must be provided")
|
||||||
@ -1289,7 +1275,7 @@ class BatchBackend(BaseBackend):
|
|||||||
self.iam_backend.get_role_by_arn(service_role)
|
self.iam_backend.get_role_by_arn(service_role)
|
||||||
except IAMNotFoundException:
|
except IAMNotFoundException:
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"Could not find IAM role {0}".format(service_role)
|
f"Could not find IAM role {service_role}"
|
||||||
)
|
)
|
||||||
|
|
||||||
compute_env.service_role = service_role
|
compute_env.service_role = service_role
|
||||||
@ -1297,7 +1283,7 @@ class BatchBackend(BaseBackend):
|
|||||||
if state is not None:
|
if state is not None:
|
||||||
if state not in ("ENABLED", "DISABLED"):
|
if state not in ("ENABLED", "DISABLED"):
|
||||||
raise InvalidParameterValueException(
|
raise InvalidParameterValueException(
|
||||||
"state {0} must be one of ENABLED | DISABLED".format(state)
|
f"state {state} must be one of ENABLED | DISABLED"
|
||||||
)
|
)
|
||||||
|
|
||||||
compute_env.state = state
|
compute_env.state = state
|
||||||
@ -1324,14 +1310,12 @@ class BatchBackend(BaseBackend):
|
|||||||
(compute_env_order, "computeEnvironmentOrder"),
|
(compute_env_order, "computeEnvironmentOrder"),
|
||||||
):
|
):
|
||||||
if variable is None:
|
if variable is None:
|
||||||
raise ClientException("{0} must be provided".format(var_name))
|
raise ClientException(f"{var_name} must be provided")
|
||||||
|
|
||||||
if state not in ("ENABLED", "DISABLED"):
|
if state not in ("ENABLED", "DISABLED"):
|
||||||
raise ClientException(
|
raise ClientException(f"state {state} must be one of ENABLED | DISABLED")
|
||||||
"state {0} must be one of ENABLED | DISABLED".format(state)
|
|
||||||
)
|
|
||||||
if self.get_job_queue_by_name(queue_name) is not None:
|
if self.get_job_queue_by_name(queue_name) is not None:
|
||||||
raise ClientException("Job queue {0} already exists".format(queue_name))
|
raise ClientException(f"Job queue {queue_name} already exists")
|
||||||
|
|
||||||
if len(compute_env_order) == 0:
|
if len(compute_env_order) == 0:
|
||||||
raise ClientException("At least 1 compute environment must be provided")
|
raise ClientException("At least 1 compute environment must be provided")
|
||||||
@ -1346,9 +1330,7 @@ class BatchBackend(BaseBackend):
|
|||||||
for arn in ordered_compute_environments:
|
for arn in ordered_compute_environments:
|
||||||
env = self.get_compute_environment_by_arn(arn)
|
env = self.get_compute_environment_by_arn(arn)
|
||||||
if env is None:
|
if env is None:
|
||||||
raise ClientException(
|
raise ClientException(f"Compute environment {arn} does not exist")
|
||||||
"Compute environment {0} does not exist".format(arn)
|
|
||||||
)
|
|
||||||
env_objects.append(env)
|
env_objects.append(env)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise ClientException("computeEnvironmentOrder is malformed")
|
raise ClientException("computeEnvironmentOrder is malformed")
|
||||||
@ -1399,12 +1381,12 @@ class BatchBackend(BaseBackend):
|
|||||||
|
|
||||||
job_queue = self.get_job_queue(queue_name)
|
job_queue = self.get_job_queue(queue_name)
|
||||||
if job_queue is None:
|
if job_queue is None:
|
||||||
raise ClientException("Job queue {0} does not exist".format(queue_name))
|
raise ClientException(f"Job queue {queue_name} does not exist")
|
||||||
|
|
||||||
if state is not None:
|
if state is not None:
|
||||||
if state not in ("ENABLED", "DISABLED"):
|
if state not in ("ENABLED", "DISABLED"):
|
||||||
raise ClientException(
|
raise ClientException(
|
||||||
"state {0} must be one of ENABLED | DISABLED".format(state)
|
f"state {state} must be one of ENABLED | DISABLED"
|
||||||
)
|
)
|
||||||
|
|
||||||
job_queue.state = state
|
job_queue.state = state
|
||||||
@ -1424,7 +1406,7 @@ class BatchBackend(BaseBackend):
|
|||||||
env = self.get_compute_environment_by_arn(arn)
|
env = self.get_compute_environment_by_arn(arn)
|
||||||
if env is None:
|
if env is None:
|
||||||
raise ClientException(
|
raise ClientException(
|
||||||
"Compute environment {0} does not exist".format(arn)
|
f"Compute environment {arn} does not exist"
|
||||||
)
|
)
|
||||||
env_objects.append(env)
|
env_objects.append(env)
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -1544,13 +1526,11 @@ class BatchBackend(BaseBackend):
|
|||||||
# Look for job definition
|
# Look for job definition
|
||||||
job_def = self.get_job_definition(job_def_id)
|
job_def = self.get_job_definition(job_def_id)
|
||||||
if job_def is None:
|
if job_def is None:
|
||||||
raise ClientException(
|
raise ClientException(f"Job definition {job_def_id} does not exist")
|
||||||
"Job definition {0} does not exist".format(job_def_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
queue = self.get_job_queue(job_queue)
|
queue = self.get_job_queue(job_queue)
|
||||||
if queue is None:
|
if queue is None:
|
||||||
raise ClientException("Job queue {0} does not exist".format(job_queue))
|
raise ClientException(f"Job queue {job_queue} does not exist")
|
||||||
|
|
||||||
job = Job(
|
job = Job(
|
||||||
job_name,
|
job_name,
|
||||||
@ -1593,7 +1573,7 @@ class BatchBackend(BaseBackend):
|
|||||||
|
|
||||||
job_queue = self.get_job_queue(job_queue_name)
|
job_queue = self.get_job_queue(job_queue_name)
|
||||||
if job_queue is None:
|
if job_queue is None:
|
||||||
raise ClientException("Job queue {0} does not exist".format(job_queue))
|
raise ClientException(f"Job queue {job_queue_name} does not exist")
|
||||||
|
|
||||||
if job_status is not None and job_status not in (
|
if job_status is not None and job_status not in (
|
||||||
"SUBMITTED",
|
"SUBMITTED",
|
||||||
|
@ -2,21 +2,17 @@ from typing import Any, Dict
|
|||||||
|
|
||||||
|
|
||||||
def make_arn_for_compute_env(account_id: str, name: str, region_name: str) -> str:
|
def make_arn_for_compute_env(account_id: str, name: str, region_name: str) -> str:
|
||||||
return "arn:aws:batch:{0}:{1}:compute-environment/{2}".format(
|
return f"arn:aws:batch:{region_name}:{account_id}:compute-environment/{name}"
|
||||||
region_name, account_id, name
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def make_arn_for_job_queue(account_id: str, name: str, region_name: str) -> str:
|
def make_arn_for_job_queue(account_id: str, name: str, region_name: str) -> str:
|
||||||
return "arn:aws:batch:{0}:{1}:job-queue/{2}".format(region_name, account_id, name)
|
return f"arn:aws:batch:{region_name}:{account_id}:job-queue/{name}"
|
||||||
|
|
||||||
|
|
||||||
def make_arn_for_task_def(
|
def make_arn_for_task_def(
|
||||||
account_id: str, name: str, revision: int, region_name: str
|
account_id: str, name: str, revision: int, region_name: str
|
||||||
) -> str:
|
) -> str:
|
||||||
return "arn:aws:batch:{0}:{1}:job-definition/{2}:{3}".format(
|
return f"arn:aws:batch:{region_name}:{account_id}:job-definition/{name}:{revision}"
|
||||||
region_name, account_id, name, revision
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def lowercase_first_key(some_dict: Dict[str, Any]) -> Dict[str, Any]:
|
def lowercase_first_key(some_dict: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
@ -59,13 +59,11 @@ class BatchSimpleBackend(BaseBackend):
|
|||||||
# Look for job definition
|
# Look for job definition
|
||||||
job_def = self.get_job_definition(job_def_id)
|
job_def = self.get_job_definition(job_def_id)
|
||||||
if job_def is None:
|
if job_def is None:
|
||||||
raise ClientException(
|
raise ClientException(f"Job definition {job_def_id} does not exist")
|
||||||
"Job definition {0} does not exist".format(job_def_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
queue = self.get_job_queue(job_queue)
|
queue = self.get_job_queue(job_queue)
|
||||||
if queue is None:
|
if queue is None:
|
||||||
raise ClientException("Job queue {0} does not exist".format(job_queue))
|
raise ClientException(f"Job queue {job_queue} does not exist")
|
||||||
|
|
||||||
job = Job(
|
job = Job(
|
||||||
job_name,
|
job_name,
|
||||||
|
Loading…
Reference in New Issue
Block a user