From 79f14b7e7e2e8f025e28513f99225138f6b18e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Mon, 21 Feb 2022 11:52:46 +0100 Subject: [PATCH] chore: fix various linting warnings in f-strings and docs --- docs/k8s.rst | 6 +++--- tutor/commands/compose.py | 15 ++++++--------- tutor/utils.py | 29 ++++++++++------------------- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/docs/k8s.rst b/docs/k8s.rst index abdd603..7864ceb 100644 --- a/docs/k8s.rst +++ b/docs/k8s.rst @@ -20,7 +20,7 @@ Tutor was tested with server version 1.14.1 and client 1.14.3. Memory ~~~~~~ -In the following, we assume you have access to a working Kubernetes cluster. `kubectl` should use your cluster configuration by default. To launch a cluster locally, you may try out Minikube. Just follow the `official installation instructions `_. +In the following, we assume you have access to a working Kubernetes cluster. ``kubectl`` should use your cluster configuration by default. To launch a cluster locally, you may try out Minikube. Just follow the `official installation instructions `__. The Kubernetes cluster should have at least 4Gb of RAM on each node. When running Minikube, the virtual machine should have that much allocated memory. See below for an example with VirtualBox: @@ -102,7 +102,7 @@ As with the :ref:`local installation `, there are multiple commands to ru tutor k8s -h -In particular, the `tutor k8s start` command restarts and reconfigures all services by running ``kubectl apply``. That means that you can delete containers, deployments or just any other kind of resources, and Tutor will re-create them automatically. You should just beware of not deleting any persistent data stored in persistent volume claims. For instance, to restart from a "blank slate", run:: +In particular, the ``tutor k8s start`` command restarts and reconfigures all services by running ``kubectl apply``. That means that you can delete containers, deployments or just any other kind of resources, and Tutor will re-create them automatically. You should just beware of not deleting any persistent data stored in persistent volume claims. For instance, to restart from a "blank slate", run:: tutor k8s stop tutor k8s start @@ -133,6 +133,6 @@ Some Tutor plugins and customization procedures require that the "openedx" image Updating docker images ~~~~~~~~~~~~~~~~~~~~~~ -Kubernetes does not provide a single command for updating docker images out of the box. A `commonly used trick `_ is to modify an innocuous label on all resources:: +Kubernetes does not provide a single command for updating docker images out of the box. A `commonly used trick `__ is to modify an innocuous label on all resources:: kubectl patch -k "$(tutor config printroot)/env" --patch "{\"spec\": {\"template\": {\"metadata\": {\"labels\": {\"date\": \"`date +'%Y%m%d-%H%M%S'`\"}}}}}" diff --git a/tutor/commands/compose.py b/tutor/commands/compose.py index f7c220e..ebc2652 100644 --- a/tutor/commands/compose.py +++ b/tutor/commands/compose.py @@ -44,7 +44,7 @@ class ComposeJobRunner(jobs.BaseComposeJobRunner): run_command += ["run", "--rm"] if not utils.is_a_tty(): run_command += ["-T"] - job_service_name = "{}-job".format(service) + job_service_name = f"{service}-job" return self.docker_compose( *run_command, job_service_name, @@ -224,9 +224,8 @@ def bindmount_command(context: BaseComposeContext, service: str, path: str) -> N config = tutor_config.load(context.root) host_path = bindmounts.create(context.job_runner(config), service, path) fmt.echo_info( - "Bind-mount volume created at {}. You can now use it in all `local` and `dev` commands with the `--volume={}` option.".format( - host_path, path - ) + f"Bind-mount volume created at {host_path}. You can now use it in all `local` and `dev` " + f"commands with the `--volume={path}` option." ) @@ -286,12 +285,10 @@ def dc_command(context: BaseComposeContext, command: str, args: List[str]) -> No host_bind_path = bindmounts.get_path(context.root, volume_arg) if not os.path.exists(host_bind_path): raise TutorError( - ( - "Bind-mount volume directory {} does not exist. It must first be created" - " with the '{}' command." - ).format(host_bind_path, bindmount_command.name) + f"Bind-mount volume directory {host_bind_path} does not exist. It must first be created " + f"with the '{bindmount_command.name}' command." ) - volume_arg = "{}:{}".format(host_bind_path, volume_arg) + volume_arg = f"{host_bind_path}:{volume_arg}" volume_args += ["--volume", volume_arg] context.job_runner(config).docker_compose(command, *volume_args, *non_volume_args) diff --git a/tutor/utils.py b/tutor/utils.py index 4a59778..6175e6a 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -45,15 +45,11 @@ def ensure_file_directory_exists(path: str) -> None: directory = os.path.dirname(path) if os.path.isfile(directory): raise exceptions.TutorError( - "Attempting to create a directory, but a file with the same name already exists: {}".format( - directory - ) + f"Attempting to create a directory, but a file with the same name already exists: {directory}" ) if os.path.isdir(path): raise exceptions.TutorError( - "Attempting to write to a file, but a directory with the same name already exists: {}".format( - directory - ) + f"Attempting to write to a file, but a directory with the same name already exists: {directory}" ) if not os.path.exists(directory): os.makedirs(directory) @@ -123,7 +119,7 @@ def long_to_base64(n: int) -> str: return _bytes bys = long2intarr(n) - data = struct.pack("%sB" % len(bys), *bys) + data = struct.pack(f"{len(bys)}B", *bys) if not data: data = b"\x00" s = base64.urlsafe_b64encode(data).rstrip(b"=") @@ -202,24 +198,21 @@ def execute(*command: str) -> int: except Exception as e: p.kill() p.wait() - raise exceptions.TutorError( - "Command failed: {}".format(" ".join(command)) - ) from e + raise exceptions.TutorError(f"Command failed: {' '.join(command)}") from e if result > 0: raise exceptions.TutorError( - "Command failed with status {}: {}".format(result, " ".join(command)) + f"Command failed with status {result}: {' '.join(command)}" ) return result def check_output(*command: str) -> bytes: - click.echo(fmt.command(" ".join(command))) + literal_command = " ".join(command) + click.echo(fmt.command(literal_command)) try: return subprocess.check_output(command) except Exception as e: - raise exceptions.TutorError( - "Command failed: {}".format(" ".join(command)) - ) from e + raise exceptions.TutorError(f"Command failed: {literal_command}") from e def check_macos_docker_memory() -> None: @@ -237,7 +230,7 @@ def check_macos_docker_memory() -> None: ) try: - with open(settings_path) as fp: + with open(settings_path, encoding="utf-8") as fp: data = json.load(fp) memory_mib = int(data["memoryMiB"]) except OSError as e: @@ -264,7 +257,5 @@ def check_macos_docker_memory() -> None: if memory_mib < 4096: raise exceptions.TutorError( - "Docker is configured to allocate {} MiB RAM, less than the recommended {} MiB".format( - memory_mib, 4096 - ) + f"Docker is configured to allocate {memory_mib} MiB RAM, less than the recommended {4096} MiB" )