Skip to content

Commit 5338552

Browse files
kamilkrzyskowsquidfunk
authored andcommitted
Fixed handling of inconsistent drive letter case
1 parent 6d4f756 commit 5338552

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

material/plugins/info/plugin.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,22 @@ def on_config(self, config):
151151
if not isinstance(loaded_configs, list):
152152
loaded_configs = [loaded_configs]
153153

154+
# It can happen that the drive letter case is inconsistent on Windows.
155+
# Therefore, assure first character to be uppercase for the following
156+
# checks. See: https://t.ly/9t1SU
157+
site_prefixes = list(map(capitalize, site.PREFIXES))
158+
cwd = capitalize(os.getcwd())
159+
154160
# We need to make sure the user put every file in the current working
155161
# directory. To assure the reproduction inside the ZIP file can be run,
156162
# validate that the MkDocs paths are children of the current root.
157-
paths_to_validate = [
163+
paths_to_validate = list(map(capitalize, [
158164
config.config_file_path,
159165
config.docs_dir,
160166
abs_custom_dir,
161167
abs_projects_dir,
162168
*[cfg.get("INHERIT", "") for cfg in loaded_configs]
163-
]
169+
]))
164170

165171
# Convert relative hook paths to absolute path
166172
for hook in config.hooks:
@@ -169,7 +175,7 @@ def on_config(self, config):
169175

170176
# Remove valid paths from the list
171177
for path in list(paths_to_validate):
172-
if not path or path.startswith(os.getcwd()):
178+
if not path or path.startswith(cwd):
173179
paths_to_validate.remove(path)
174180

175181
# Report the invalid paths to the user
@@ -191,14 +197,14 @@ def on_config(self, config):
191197
self.excluded_entries = []
192198

193199
# Exclude the site_dir at project root
194-
if config.site_dir.startswith(os.getcwd()):
200+
if capitalize(config.site_dir).startswith(cwd):
195201
self.exclusion_patterns.append(_resolve_pattern(config.site_dir))
196202

197203
# Exclude the Virtual Environment directory. site.getsitepackages() has
198204
# inconsistent results across operating systems, and relies on the
199205
# PREFIXES that will contain the absolute path to the activated venv.
200-
for path in site.PREFIXES:
201-
if path.startswith(os.getcwd()):
206+
for path in site_prefixes:
207+
if path.startswith(cwd):
202208
self.exclusion_patterns.append(_resolve_pattern(path))
203209

204210
# Guess other Virtual Environment paths in case we forget to activate
@@ -209,9 +215,9 @@ def on_config(self, config):
209215
if filename.lower() != "pyvenv.cfg":
210216
continue
211217

212-
path = abs_root[0].upper() + abs_root[1:]
218+
path = capitalize(abs_root)
213219

214-
if path not in site.PREFIXES:
220+
if path not in site_prefixes:
215221
print(f"Possible inactive venv: {path}")
216222
self.exclusion_patterns.append(_resolve_pattern(path))
217223

@@ -509,7 +515,7 @@ def _load_yaml(abs_src_path: str):
509515
# in the pattern creation for files and directories. The patterns are matched
510516
# using the search function, so they are prefixed with ^ for specificity.
511517
def _resolve_pattern(abspath: str, return_path: bool = False):
512-
path = abspath.replace(os.getcwd(), "", 1)
518+
path = capitalize(abspath).replace(capitalize(os.getcwd()), "", 1)
513519
path = path.replace(os.sep, "/").rstrip("/")
514520

515521
if not path:
@@ -543,6 +549,11 @@ def _is_dotpath(path: str, log_warning: bool = False) -> bool:
543549
return True
544550
return False
545551

552+
# It can happen that the drive letter case is inconsistent on Windows.
553+
# Capitalize the first character keeping the rest the same for comparison.
554+
# See: https://t.ly/9t1SU
555+
def capitalize(path: str):
556+
return path[0].upper() + path[1:] if path else path
546557

547558
# -----------------------------------------------------------------------------
548559
# Data

src/plugins/info/plugin.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,22 @@ def on_config(self, config):
151151
if not isinstance(loaded_configs, list):
152152
loaded_configs = [loaded_configs]
153153

154+
# It can happen that the drive letter case is inconsistent on Windows.
155+
# Therefore, assure first character to be uppercase for the following
156+
# checks. See: https://t.ly/9t1SU
157+
site_prefixes = list(map(capitalize, site.PREFIXES))
158+
cwd = capitalize(os.getcwd())
159+
154160
# We need to make sure the user put every file in the current working
155161
# directory. To assure the reproduction inside the ZIP file can be run,
156162
# validate that the MkDocs paths are children of the current root.
157-
paths_to_validate = [
163+
paths_to_validate = list(map(capitalize, [
158164
config.config_file_path,
159165
config.docs_dir,
160166
abs_custom_dir,
161167
abs_projects_dir,
162168
*[cfg.get("INHERIT", "") for cfg in loaded_configs]
163-
]
169+
]))
164170

165171
# Convert relative hook paths to absolute path
166172
for hook in config.hooks:
@@ -169,7 +175,7 @@ def on_config(self, config):
169175

170176
# Remove valid paths from the list
171177
for path in list(paths_to_validate):
172-
if not path or path.startswith(os.getcwd()):
178+
if not path or path.startswith(cwd):
173179
paths_to_validate.remove(path)
174180

175181
# Report the invalid paths to the user
@@ -191,14 +197,14 @@ def on_config(self, config):
191197
self.excluded_entries = []
192198

193199
# Exclude the site_dir at project root
194-
if config.site_dir.startswith(os.getcwd()):
200+
if capitalize(config.site_dir).startswith(cwd):
195201
self.exclusion_patterns.append(_resolve_pattern(config.site_dir))
196202

197203
# Exclude the Virtual Environment directory. site.getsitepackages() has
198204
# inconsistent results across operating systems, and relies on the
199205
# PREFIXES that will contain the absolute path to the activated venv.
200-
for path in site.PREFIXES:
201-
if path.startswith(os.getcwd()):
206+
for path in site_prefixes:
207+
if path.startswith(cwd):
202208
self.exclusion_patterns.append(_resolve_pattern(path))
203209

204210
# Guess other Virtual Environment paths in case we forget to activate
@@ -209,9 +215,9 @@ def on_config(self, config):
209215
if filename.lower() != "pyvenv.cfg":
210216
continue
211217

212-
path = abs_root[0].upper() + abs_root[1:]
218+
path = capitalize(abs_root)
213219

214-
if path not in site.PREFIXES:
220+
if path not in site_prefixes:
215221
print(f"Possible inactive venv: {path}")
216222
self.exclusion_patterns.append(_resolve_pattern(path))
217223

@@ -509,7 +515,7 @@ def _load_yaml(abs_src_path: str):
509515
# in the pattern creation for files and directories. The patterns are matched
510516
# using the search function, so they are prefixed with ^ for specificity.
511517
def _resolve_pattern(abspath: str, return_path: bool = False):
512-
path = abspath.replace(os.getcwd(), "", 1)
518+
path = capitalize(abspath).replace(capitalize(os.getcwd()), "", 1)
513519
path = path.replace(os.sep, "/").rstrip("/")
514520

515521
if not path:
@@ -543,6 +549,11 @@ def _is_dotpath(path: str, log_warning: bool = False) -> bool:
543549
return True
544550
return False
545551

552+
# It can happen that the drive letter case is inconsistent on Windows.
553+
# Capitalize the first character keeping the rest the same for comparison.
554+
# See: https://t.ly/9t1SU
555+
def capitalize(path: str):
556+
return path[0].upper() + path[1:] if path else path
546557

547558
# -----------------------------------------------------------------------------
548559
# Data

0 commit comments

Comments
 (0)