Skip to content

Commit

Permalink
ensure file existence checks are case-sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Walavouchey committed Nov 22, 2023
1 parent de289fa commit d226ae5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions wikitools/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fnmatch
import os
import typing
import pathlib


class ChangeDirectory:
Expand All @@ -21,6 +22,24 @@ def normalised(path: str) -> str:
return normalised


def exists(path: pathlib.Path):
"""
Case-sensitive file existence check
File paths are case-insensitive on some operating systems like Windows, but we rely on file existence checks to take casing into account
"""

if os.name == 'nt':
try:
path_string = path.as_posix()
# windows disallows two files that differ only in casing, so there are no special considerations for that
return normalised(path_string) == normalised(os.path.relpath(os.path.realpath(path_string, strict=True)))
except OSError:
return False
else:
return path.exists()


def is_newspost(path: str) -> bool:
return (
normalised(os.path.dirname(path)).startswith("news") and
Expand Down
8 changes: 4 additions & 4 deletions wikitools/link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import typing

from wikitools import redirect_parser, reference_parser, errors, link_parser, article_parser
from wikitools import console
from wikitools import console, file_utils


def check_link(
Expand Down Expand Up @@ -35,7 +35,7 @@ def check_link(
repo_target = pathlib.Path(f"news/{year}/{target.name}")
location = '/' + repo_target.as_posix()

if not repo_target.exists():
if not file_utils.exists(repo_target):
# news posts don't have redirects
return errors.LinkNotFoundError(link, reference, location)
else:
Expand Down Expand Up @@ -67,15 +67,15 @@ def check_link(

target = pathlib.Path(location[1:]) # strip leading slash
# no article? could be a redirect
if not target.exists():
if not file_utils.exists(target):
redirect_source = target.relative_to('wiki').as_posix()
try:
redirect_destination, redirect_line_no = redirects[redirect_source.lower()]
except KeyError:
return errors.LinkNotFoundError(link, reference, location)

target = pathlib.Path('wiki') / redirect_destination
if not target.exists():
if not file_utils.exists(target):
return errors.BrokenRedirectError(link, redirect_source, redirect_line_no, redirect_destination)

# link to an article in general, article exists -> good
Expand Down

0 comments on commit d226ae5

Please sign in to comment.