Skip to content

Commit

Permalink
fix: click.Path(resolve_path=True) doesn't always work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Mar 11, 2023
1 parent 24bdfcd commit cce697a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lektor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lektor.cli_utils import extraflag
from lektor.cli_utils import pass_context
from lektor.cli_utils import pruneflag
from lektor.cli_utils import ResolvedPath
from lektor.cli_utils import validate_language
from lektor.compat import importlib_metadata as metadata
from lektor.project import Project
Expand Down Expand Up @@ -50,7 +51,7 @@ def cli(ctx, project=None, language=None):
@click.option(
"-O",
"--output-path",
type=click.Path(resolve_path=True),
type=ResolvedPath(),
default=None,
help="The output path.",
)
Expand Down Expand Up @@ -165,7 +166,7 @@ def _build():
@click.option(
"-O",
"--output-path",
type=click.Path(resolve_path=True),
type=ResolvedPath(),
default=None,
help="The output path.",
)
Expand Down Expand Up @@ -205,7 +206,7 @@ def clean_cmd(ctx, output_path, verbosity, extra_flags):
@click.option(
"-O",
"--output-path",
type=click.Path(resolve_path=True),
type=ResolvedPath(),
default=None,
help="The output path.",
)
Expand Down Expand Up @@ -312,7 +313,7 @@ def deploy_cmd(ctx, server, output_path, extra_flags, **credentials):
@click.option(
"-O",
"--output-path",
type=click.Path(resolve_path=True),
type=ResolvedPath(),
default=None,
help="The dev server will build into the same folder as "
"the build command by default.",
Expand Down
27 changes: 27 additions & 0 deletions lektor/cli_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# pylint: disable=import-outside-toplevel
from __future__ import annotations

import json
import os
from pathlib import Path
from typing import Any

import click

Expand Down Expand Up @@ -127,3 +131,26 @@ def validate_language(ctx, param, value):
if value is not None and not is_valid_language(value):
raise click.BadParameter('Unsupported language "%s".' % value)
return value


class ResolvedPath(click.Path):
"""A click paramter type for a resolved path.
We could just use ``click.Path(resolve_path=True)`` except that that
fails sometimes under Windows running python <= 3.9.
See https://github.com/pallets/click/issues/2466
"""

def __init__(self, writable=False, file_okay=True):
super().__init__(
resolve_path=True, allow_dash=False, writable=writable, file_okay=file_okay
)

def convert(
self, value: Any, param: click.Parameter | None, ctx: click.Context | None
) -> Any:
abspath = Path(value).absolute()
# fsdecode to ensure that the return value is a str.
# (with click<8.0.3 Path.convert will return Path if passed a Path)
return os.fsdecode(super().convert(abspath, param, ctx))

0 comments on commit cce697a

Please sign in to comment.