2024-01-06 05:27:06 +03:00
|
|
|
# Copyright (c) 2016-2024 Martin Donath <martin.donath@squidfunk.com>
|
2022-12-18 17:41:18 +03:00
|
|
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to
|
|
|
|
# deal in the Software without restriction, including without limitation the
|
|
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
|
2024-03-01 00:01:36 +03:00
|
|
|
import glob
|
2023-01-02 12:55:24 +03:00
|
|
|
import json
|
2022-12-18 17:41:18 +03:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import platform
|
2024-03-01 00:07:49 +03:00
|
|
|
import regex
|
2022-12-18 19:23:12 +03:00
|
|
|
import requests
|
2024-02-29 23:55:54 +03:00
|
|
|
import site
|
2022-12-18 17:41:18 +03:00
|
|
|
import sys
|
2024-03-01 00:05:44 +03:00
|
|
|
import yaml
|
2024-03-01 00:07:49 +03:00
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
from colorama import Fore, Style
|
2023-08-10 14:45:51 +03:00
|
|
|
from importlib.metadata import distributions, version
|
2022-12-18 17:41:18 +03:00
|
|
|
from io import BytesIO
|
2023-08-10 16:32:46 +03:00
|
|
|
from markdown.extensions.toc import slugify
|
2024-03-01 00:01:36 +03:00
|
|
|
from mkdocs.config.defaults import MkDocsConfig
|
2022-12-18 17:41:18 +03:00
|
|
|
from mkdocs.plugins import BasePlugin, event_priority
|
2024-03-01 00:05:44 +03:00
|
|
|
from mkdocs.utils import get_yaml_loader
|
2022-12-18 17:41:18 +03:00
|
|
|
from zipfile import ZipFile, ZIP_DEFLATED
|
|
|
|
|
2023-08-21 18:45:32 +03:00
|
|
|
from .config import InfoConfig
|
2024-03-12 05:55:15 +03:00
|
|
|
from .patterns import get_exclusion_patterns
|
2023-07-05 18:39:33 +03:00
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
2023-08-21 18:45:32 +03:00
|
|
|
# Classes
|
2022-12-18 17:41:18 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Info plugin
|
2023-07-05 18:39:33 +03:00
|
|
|
class InfoPlugin(BasePlugin[InfoConfig]):
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2023-07-24 14:29:16 +03:00
|
|
|
# Initialize plugin
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2023-08-10 16:32:46 +03:00
|
|
|
# Initialize incremental builds
|
2023-07-24 14:29:16 +03:00
|
|
|
self.is_serve = False
|
|
|
|
|
2024-02-29 23:55:54 +03:00
|
|
|
# Initialize empty members
|
|
|
|
self.exclusion_patterns = []
|
2024-03-12 05:55:15 +03:00
|
|
|
self.excluded_entries = []
|
2024-02-29 23:55:54 +03:00
|
|
|
|
2023-08-10 16:32:46 +03:00
|
|
|
# Determine whether we're serving the site
|
2023-01-19 22:56:15 +03:00
|
|
|
def on_startup(self, *, command, dirty):
|
2023-08-10 16:32:46 +03:00
|
|
|
self.is_serve = command == "serve"
|
2023-01-28 14:21:04 +03:00
|
|
|
|
2023-08-10 16:32:46 +03:00
|
|
|
# Create a self-contained example (run earliest) - determine all files that
|
|
|
|
# are visible to MkDocs and are used to build the site, create an archive
|
|
|
|
# that contains all of them, and print a summary of the archive contents.
|
2023-12-07 12:48:42 +03:00
|
|
|
# The author must attach this archive to the bug report.
|
2023-01-28 14:21:04 +03:00
|
|
|
@event_priority(100)
|
|
|
|
def on_config(self, config):
|
2023-01-19 22:56:15 +03:00
|
|
|
if not self.config.enabled:
|
|
|
|
return
|
|
|
|
|
|
|
|
# By default, the plugin is disabled when the documentation is served,
|
|
|
|
# but not when it is built. This should nicely align with the expected
|
|
|
|
# user experience when creating reproductions.
|
2023-01-28 14:21:04 +03:00
|
|
|
if not self.config.enabled_on_serve and self.is_serve:
|
2022-12-18 17:41:18 +03:00
|
|
|
return
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Resolve latest version
|
|
|
|
url = "https://github.com/squidfunk/mkdocs-material/releases/latest"
|
|
|
|
res = requests.get(url, allow_redirects = False)
|
|
|
|
|
|
|
|
# Check if we're running the latest version
|
2023-08-10 14:45:51 +03:00
|
|
|
_, current = res.headers.get("location").rsplit("/", 1)
|
|
|
|
present = version("mkdocs-material")
|
|
|
|
if not present.startswith(current):
|
2023-01-01 19:39:04 +03:00
|
|
|
log.error("Please upgrade to the latest version.")
|
2023-08-10 14:45:51 +03:00
|
|
|
self._help_on_versions_and_exit(present, current)
|
2022-12-18 19:23:12 +03:00
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
# Exit if archive creation is disabled
|
|
|
|
if not self.config.archive:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Print message that we're creating a bug report
|
|
|
|
log.info("Started archive creation for bug report")
|
2022-12-18 17:41:18 +03:00
|
|
|
|
|
|
|
# Check that there are no overrides in place - we need to use a little
|
|
|
|
# hack to detect whether the custom_dir setting was used without parsing
|
|
|
|
# mkdocs.yml again - we check at which position the directory provided
|
|
|
|
# by the theme resides, and if it's not the first one, abort.
|
2024-03-01 00:05:44 +03:00
|
|
|
if config.theme.custom_dir:
|
2022-12-18 17:41:18 +03:00
|
|
|
log.error("Please remove 'custom_dir' setting.")
|
2022-12-18 19:23:12 +03:00
|
|
|
self._help_on_customizations_and_exit()
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Check that there are no hooks in place - hooks can alter the behavior
|
|
|
|
# of MkDocs in unpredictable ways, which is why they must be considered
|
|
|
|
# being customizations. Thus, we can't offer support for debugging and
|
|
|
|
# must abort here.
|
2022-12-18 17:41:18 +03:00
|
|
|
if config.hooks:
|
|
|
|
log.error("Please remove 'hooks' setting.")
|
2022-12-18 19:23:12 +03:00
|
|
|
self._help_on_customizations_and_exit()
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Assure all paths that will be validated are absolute. Convert possible
|
|
|
|
# relative config_file_path to absolute. Its absolute directory path is
|
|
|
|
# being later used to resolve other paths.
|
2024-03-01 00:05:44 +03:00
|
|
|
config.config_file_path = _convert_to_abs(config.config_file_path)
|
|
|
|
config_file_parent = os.path.dirname(config.config_file_path)
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Convert relative custom_dir path to absolute. The Theme.custom_dir
|
|
|
|
# property cannot be set, therefore a helper variable is used.
|
|
|
|
if config.theme.custom_dir:
|
|
|
|
abs_custom_dir = _convert_to_abs(
|
|
|
|
config.theme.custom_dir,
|
2024-03-01 00:05:44 +03:00
|
|
|
abs_prefix = config_file_parent
|
|
|
|
)
|
2024-03-01 00:07:49 +03:00
|
|
|
else:
|
|
|
|
abs_custom_dir = ""
|
2024-03-01 00:01:36 +03:00
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Extract the absolute path to projects plugin's directory to explicitly
|
|
|
|
# support path validation and dynamic exclusion for the plugin
|
2024-03-01 00:01:36 +03:00
|
|
|
projects_plugin = config.plugins.get("material/projects")
|
|
|
|
if projects_plugin:
|
2024-03-01 00:05:44 +03:00
|
|
|
abs_projects_dir = _convert_to_abs(
|
|
|
|
projects_plugin.config.projects_dir,
|
|
|
|
abs_prefix = config_file_parent
|
2024-03-01 00:01:36 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
abs_projects_dir = ""
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# MkDocs removes the INHERIT configuration key during load, and doesn't
|
|
|
|
# expose the information in any way, as the parent configuration is
|
|
|
|
# merged into one. To validate that the INHERIT config file will be
|
|
|
|
# included in the ZIP file the current config file must be loaded again
|
|
|
|
# without parsing. Each file can have their own INHERIT key, so a list
|
|
|
|
# of configurations is supported. The INHERIT path is converted during
|
|
|
|
# load to absolute.
|
2024-03-01 00:05:44 +03:00
|
|
|
loaded_configs = _load_yaml(config.config_file_path)
|
|
|
|
if not isinstance(loaded_configs, list):
|
|
|
|
loaded_configs = [loaded_configs]
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# We need to make sure the user put every file in the current working
|
|
|
|
# directory. To assure the reproduction inside the ZIP file can be run,
|
|
|
|
# validate that the MkDocs paths are children of the current root.
|
2024-03-01 00:05:44 +03:00
|
|
|
paths_to_validate = [
|
|
|
|
config.config_file_path,
|
|
|
|
config.docs_dir,
|
2024-03-01 00:07:49 +03:00
|
|
|
abs_custom_dir,
|
2024-03-01 00:05:44 +03:00
|
|
|
abs_projects_dir,
|
|
|
|
*[cfg.get("INHERIT", "") for cfg in loaded_configs]
|
|
|
|
]
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Convert relative hook paths to absolute path
|
2024-03-01 00:05:44 +03:00
|
|
|
for hook in config.hooks:
|
|
|
|
path = _convert_to_abs(hook, abs_prefix = config_file_parent)
|
|
|
|
paths_to_validate.append(path)
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Remove valid paths from the list
|
2024-03-01 00:05:44 +03:00
|
|
|
for path in list(paths_to_validate):
|
|
|
|
if not path or path.startswith(os.getcwd()):
|
|
|
|
paths_to_validate.remove(path)
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Report the invalid paths to the user
|
2024-03-01 00:05:44 +03:00
|
|
|
if paths_to_validate:
|
|
|
|
log.error(f"One or more paths aren't children of root")
|
|
|
|
self._help_on_not_in_cwd(paths_to_validate)
|
|
|
|
|
2023-12-07 12:48:42 +03:00
|
|
|
# Create in-memory archive and prompt author for a short descriptive
|
2023-08-10 16:32:46 +03:00
|
|
|
# name for the archive, which is also used as the directory name. Note
|
|
|
|
# that the name is slugified for better readability and stripped of any
|
2023-12-07 12:48:42 +03:00
|
|
|
# file extension that the author might have entered.
|
2022-12-18 17:41:18 +03:00
|
|
|
archive = BytesIO()
|
2023-08-10 16:32:46 +03:00
|
|
|
example = input("\nPlease name your bug report (2-4 words): ")
|
|
|
|
example, _ = os.path.splitext(example)
|
2023-08-26 13:34:51 +03:00
|
|
|
example = "-".join([present, slugify(example, "-")])
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
# Get local copy of the exclusion patterns
|
|
|
|
self.exclusion_patterns = get_exclusion_patterns()
|
|
|
|
self.excluded_entries = []
|
2024-02-29 23:55:54 +03:00
|
|
|
|
|
|
|
# Exclude the site_dir at project root
|
|
|
|
if config.site_dir.startswith(os.getcwd()):
|
|
|
|
self.exclusion_patterns.append(_resolve_pattern(config.site_dir))
|
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
# Exclude the Virtual Environment directory. site.getsitepackages() has
|
|
|
|
# inconsistent results across operating systems, and relies on the
|
|
|
|
# PREFIXES that will contain the absolute path to the activated venv.
|
|
|
|
for path in site.PREFIXES:
|
2024-02-29 23:55:54 +03:00
|
|
|
if path.startswith(os.getcwd()):
|
|
|
|
self.exclusion_patterns.append(_resolve_pattern(path))
|
|
|
|
|
2024-03-01 00:01:36 +03:00
|
|
|
# Exclude site_dir for projects
|
|
|
|
if projects_plugin:
|
|
|
|
for path in glob.iglob(
|
|
|
|
pathname = projects_plugin.config.projects_config_files,
|
|
|
|
root_dir = abs_projects_dir,
|
|
|
|
recursive = True
|
|
|
|
):
|
|
|
|
current_config_file = os.path.join(abs_projects_dir, path)
|
|
|
|
project_config = _get_project_config(current_config_file)
|
|
|
|
pattern = _resolve_pattern(project_config.site_dir)
|
|
|
|
self.exclusion_patterns.append(pattern)
|
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# Create self-contained example from project
|
2023-08-10 16:32:46 +03:00
|
|
|
files: list[str] = []
|
2022-12-18 17:41:18 +03:00
|
|
|
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
|
2024-02-16 21:48:19 +03:00
|
|
|
for abs_root, dirnames, filenames in os.walk(os.getcwd()):
|
2024-03-12 05:55:15 +03:00
|
|
|
# Set and print progress indicator
|
|
|
|
indicator = f"Processing: {abs_root}"
|
|
|
|
print(indicator, end="\r", flush=True)
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Prune the folders in-place to prevent their processing
|
2024-02-29 23:55:54 +03:00
|
|
|
for name in list(dirnames):
|
2024-03-01 00:07:49 +03:00
|
|
|
# Resolve the absolute directory path
|
2024-02-29 23:55:54 +03:00
|
|
|
path = os.path.join(abs_root, name)
|
2024-03-01 00:07:49 +03:00
|
|
|
|
|
|
|
# Exclude the directory and all subdirectories
|
2024-03-12 05:55:15 +03:00
|
|
|
if self._is_excluded(path):
|
2024-02-29 23:55:54 +03:00
|
|
|
dirnames.remove(name)
|
2024-03-01 00:07:49 +03:00
|
|
|
|
|
|
|
# Write files to the in-memory archive
|
2024-02-16 21:48:19 +03:00
|
|
|
for name in filenames:
|
2024-03-01 00:07:49 +03:00
|
|
|
# Resolve the absolute file path
|
2024-02-16 21:48:19 +03:00
|
|
|
path = os.path.join(abs_root, name)
|
2024-03-01 00:07:49 +03:00
|
|
|
|
|
|
|
# Exclude the file
|
2024-03-12 05:55:15 +03:00
|
|
|
if self._is_excluded(path):
|
2024-02-29 23:55:54 +03:00
|
|
|
continue
|
2024-03-01 00:07:49 +03:00
|
|
|
|
|
|
|
# Resolve the relative path to create a matching structure
|
2024-02-16 21:48:19 +03:00
|
|
|
path = os.path.relpath(path, os.path.curdir)
|
2023-08-10 16:32:46 +03:00
|
|
|
f.write(path, os.path.join(example, path))
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
# Clear the line for the next indicator
|
|
|
|
print(" " * len(indicator), end="\r", flush=True)
|
|
|
|
|
2023-01-02 20:30:47 +03:00
|
|
|
# Add information on installed packages
|
2022-12-18 17:41:18 +03:00
|
|
|
f.writestr(
|
2023-08-10 16:32:46 +03:00
|
|
|
os.path.join(example, "requirements.lock.txt"),
|
2023-01-02 20:30:47 +03:00
|
|
|
"\n".join(sorted([
|
2023-08-10 16:32:46 +03:00
|
|
|
"==".join([package.name, package.version])
|
|
|
|
for package in distributions()
|
2023-01-02 20:30:47 +03:00
|
|
|
]))
|
2022-12-18 17:41:18 +03:00
|
|
|
)
|
|
|
|
|
2023-08-23 16:15:11 +03:00
|
|
|
# Add information on platform
|
2022-12-18 19:49:56 +03:00
|
|
|
f.writestr(
|
2023-08-10 16:32:46 +03:00
|
|
|
os.path.join(example, "platform.json"),
|
2023-01-02 12:55:24 +03:00
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"system": platform.platform(),
|
2024-02-29 23:39:31 +03:00
|
|
|
"architecture": platform.architecture(),
|
|
|
|
"python": platform.python_version(),
|
2024-03-12 05:55:15 +03:00
|
|
|
"cwd": os.getcwd(),
|
2024-02-29 23:39:31 +03:00
|
|
|
"command": " ".join([
|
|
|
|
sys.argv[0].rsplit(os.sep, 1)[-1],
|
|
|
|
*sys.argv[1:]
|
|
|
|
]),
|
2024-03-12 05:55:15 +03:00
|
|
|
"env:$PYTHONPATH": os.getenv("PYTHONPATH", ""),
|
|
|
|
"sys.path": sys.path,
|
|
|
|
"excluded_entries": self.excluded_entries
|
2023-01-02 12:55:24 +03:00
|
|
|
},
|
|
|
|
default = str,
|
|
|
|
indent = 2
|
|
|
|
)
|
2022-12-18 19:49:56 +03:00
|
|
|
)
|
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# Retrieve list of processed files
|
|
|
|
for a in f.filelist:
|
|
|
|
files.append("".join([
|
|
|
|
Fore.LIGHTBLACK_EX, a.filename, " ",
|
2022-12-18 19:23:12 +03:00
|
|
|
_size(a.compress_size)
|
2022-12-18 17:41:18 +03:00
|
|
|
]))
|
|
|
|
|
|
|
|
# Finally, write archive to disk
|
|
|
|
buffer = archive.getbuffer()
|
2023-08-10 16:32:46 +03:00
|
|
|
with open(f"{example}.zip", "wb") as f:
|
2022-12-18 17:41:18 +03:00
|
|
|
f.write(archive.getvalue())
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Print summary
|
2022-12-18 17:41:18 +03:00
|
|
|
log.info("Archive successfully created:")
|
|
|
|
print(Style.NORMAL)
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Print archive file names
|
2022-12-18 17:41:18 +03:00
|
|
|
files.sort()
|
|
|
|
for file in files:
|
|
|
|
print(f" {file}")
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Print archive name
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
print("".join([
|
|
|
|
" ", f.name, " ",
|
|
|
|
_size(buffer.nbytes, 10)
|
|
|
|
]))
|
|
|
|
|
|
|
|
# Print warning when file size is excessively large
|
2022-12-18 17:41:18 +03:00
|
|
|
print(Style.RESET_ALL)
|
2022-12-18 20:08:19 +03:00
|
|
|
if buffer.nbytes > 1000000:
|
2022-12-18 19:23:12 +03:00
|
|
|
log.warning("Archive exceeds recommended maximum size of 1 MB")
|
|
|
|
|
2023-08-10 16:32:46 +03:00
|
|
|
# Aaaaaand done
|
2022-12-18 17:41:18 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2022-12-18 19:23:12 +03:00
|
|
|
# Print help on versions and exit
|
|
|
|
def _help_on_versions_and_exit(self, have, need):
|
|
|
|
print(Fore.RED)
|
2023-01-01 19:39:04 +03:00
|
|
|
print(" When reporting issues, please first upgrade to the latest")
|
2022-12-18 19:23:12 +03:00
|
|
|
print(" version of Material for MkDocs, as the problem might already")
|
|
|
|
print(" be fixed in the latest version. This helps reduce duplicate")
|
2023-01-01 19:39:04 +03:00
|
|
|
print(" efforts and saves us maintainers time.")
|
2022-12-18 19:23:12 +03:00
|
|
|
print(Style.NORMAL)
|
|
|
|
print(f" Please update from {have} to {need}.")
|
|
|
|
print(Style.RESET_ALL)
|
2023-01-02 12:55:24 +03:00
|
|
|
print(f" pip install --upgrade --force-reinstall mkdocs-material")
|
2022-12-18 19:23:12 +03:00
|
|
|
print(Style.NORMAL)
|
|
|
|
|
|
|
|
# Exit, unless explicitly told not to
|
|
|
|
if self.config.archive_stop_on_violation:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Print help on customizations and exit
|
|
|
|
def _help_on_customizations_and_exit(self):
|
2022-12-18 17:41:18 +03:00
|
|
|
print(Fore.RED)
|
|
|
|
print(" When reporting issues, you must remove all customizations")
|
|
|
|
print(" and check if the problem persists. If not, the problem is")
|
|
|
|
print(" caused by your overrides. Please understand that we can't")
|
2022-12-18 19:23:12 +03:00
|
|
|
print(" help you debug your customizations. Please remove:")
|
2022-12-18 17:41:18 +03:00
|
|
|
print(Style.NORMAL)
|
|
|
|
print(" - theme.custom_dir")
|
|
|
|
print(" - hooks")
|
|
|
|
print(Fore.YELLOW)
|
|
|
|
print(" Additionally, please remove all third-party JavaScript or")
|
|
|
|
print(" CSS not explicitly mentioned in our documentation:")
|
|
|
|
print(Style.NORMAL)
|
|
|
|
print(" - extra_css")
|
|
|
|
print(" - extra_javascript")
|
2024-03-01 00:07:49 +03:00
|
|
|
print(Fore.YELLOW)
|
|
|
|
print(" If you're using customizations from the theme's documentation")
|
|
|
|
print(" and you want to report a bug specific to those customizations")
|
|
|
|
print(" then set the 'archive_stop_on_violation: false' option in the")
|
|
|
|
print(" info plugin config.")
|
2022-12-18 17:41:18 +03:00
|
|
|
print(Style.RESET_ALL)
|
2022-12-18 19:23:12 +03:00
|
|
|
|
|
|
|
# Exit, unless explicitly told not to
|
|
|
|
if self.config.archive_stop_on_violation:
|
|
|
|
sys.exit(1)
|
2022-12-18 17:41:18 +03:00
|
|
|
|
2024-03-01 00:05:44 +03:00
|
|
|
# Print help on not in current working directory and exit
|
2024-03-01 00:07:49 +03:00
|
|
|
def _help_on_not_in_cwd(self, outside_root):
|
2024-03-01 00:05:44 +03:00
|
|
|
print(Fore.RED)
|
|
|
|
print(" The current working (root) directory:\n")
|
|
|
|
print(f" {os.getcwd()}\n")
|
|
|
|
print(" is not a parent of the following paths:")
|
|
|
|
print(Style.NORMAL)
|
2024-03-01 00:07:49 +03:00
|
|
|
for path in outside_root:
|
2024-03-01 00:05:44 +03:00
|
|
|
print(f" {path}")
|
2024-03-12 05:55:15 +03:00
|
|
|
print("\n To assure that all project files are found please adjust")
|
2024-03-01 00:07:49 +03:00
|
|
|
print(" your config or file structure and put everything within the")
|
2024-03-12 05:55:15 +03:00
|
|
|
print(" root directory of the project.")
|
|
|
|
print("\n Please also make sure `mkdocs build` is run in the actual")
|
2024-03-01 00:07:49 +03:00
|
|
|
print(" root directory of the project.")
|
2024-03-01 00:05:44 +03:00
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
# Exit, unless explicitly told not to
|
|
|
|
if self.config.archive_stop_on_violation:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
# Check if path is excluded and should be omitted from the zip. Use pattern
|
|
|
|
# matching for files and folders, and lookahead specific files in folders to
|
|
|
|
# skip them. Side effect: Save excluded paths to save them in the zip file.
|
|
|
|
def _is_excluded(self, abspath: str) -> bool:
|
|
|
|
|
|
|
|
# Resolve the path into POSIX format to match the patterns
|
|
|
|
pattern_path = _resolve_pattern(abspath, return_path = True)
|
|
|
|
|
2024-02-29 23:55:54 +03:00
|
|
|
for pattern in self.exclusion_patterns:
|
2024-03-12 05:55:15 +03:00
|
|
|
if regex.search(pattern, pattern_path):
|
|
|
|
log.debug(f"Excluded pattern '{pattern}': {abspath}")
|
|
|
|
self.excluded_entries.append(f"{pattern} - {pattern_path}")
|
2024-02-29 23:55:54 +03:00
|
|
|
return True
|
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
# File exclusion should be limited to pattern matching
|
|
|
|
if os.path.isfile(abspath):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Projects, which don't use the projects plugin for multi-language
|
|
|
|
# support could have separate build folders for each config file or
|
|
|
|
# language. Therefore, we exclude them with the assumption a site_dir
|
|
|
|
# contains the sitemap file. Example of such a setup: https://t.ly/DLQcy
|
|
|
|
sitemap_gz = os.path.join(abspath, "sitemap.xml.gz")
|
|
|
|
if os.path.exists(sitemap_gz):
|
|
|
|
log.debug(f"Excluded site_dir: {abspath}")
|
|
|
|
self.excluded_entries.append(f"sitemap.xml.gz - {pattern_path}")
|
|
|
|
return True
|
|
|
|
|
2024-02-29 23:55:54 +03:00
|
|
|
return False
|
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Helper functions
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Print human-readable size
|
2022-12-18 19:23:12 +03:00
|
|
|
def _size(value, factor = 1):
|
|
|
|
color = Fore.GREEN
|
|
|
|
if value > 100000 * factor: color = Fore.RED
|
|
|
|
elif value > 25000 * factor: color = Fore.YELLOW
|
2022-12-18 17:41:18 +03:00
|
|
|
for unit in ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"]:
|
|
|
|
if abs(value) < 1000.0:
|
2022-12-18 19:23:12 +03:00
|
|
|
return f"{color}{value:3.1f} {unit}"
|
2022-12-18 17:41:18 +03:00
|
|
|
value /= 1000.0
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Get the absolute path with set prefix. To validate if a file is inside the
|
|
|
|
# current working directory it needs to be absolute, so that it is possible to
|
2024-03-01 00:05:44 +03:00
|
|
|
# check the prefix.
|
|
|
|
def _convert_to_abs(path: str, abs_prefix: str = None) -> str:
|
|
|
|
if os.path.isabs(path): return path
|
|
|
|
if abs_prefix is None: abs_prefix = os.getcwd()
|
|
|
|
return os.path.normpath(os.path.join(abs_prefix, path))
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Get the loaded config, or a list with all loaded configs. MkDocs removes the
|
|
|
|
# INHERIT configuration key during load, and doesn't expose the information in
|
|
|
|
# any way, as the parent configuration is merged into one. The INHERIT path is
|
|
|
|
# needed for validation. This custom YAML loader replicates MkDocs' loading
|
|
|
|
# logic. Side effect: It converts the INHERIT path to absolute.
|
2024-03-01 00:05:44 +03:00
|
|
|
def _load_yaml(abs_src_path: str):
|
|
|
|
|
|
|
|
with open(abs_src_path, "r", encoding ="utf-8-sig") as file:
|
|
|
|
source = file.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = yaml.load(source, Loader = get_yaml_loader()) or {}
|
|
|
|
except yaml.YAMLError:
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
if "INHERIT" in result:
|
|
|
|
relpath = result.get('INHERIT')
|
|
|
|
parent_path = os.path.dirname(abs_src_path)
|
|
|
|
abspath = _convert_to_abs(relpath, abs_prefix = parent_path)
|
|
|
|
if os.path.exists(abspath):
|
|
|
|
result["INHERIT"] = abspath
|
|
|
|
log.debug(f"Loading inherited configuration file: {abspath}")
|
|
|
|
parent = _load_yaml(abspath)
|
|
|
|
if isinstance(parent, list):
|
|
|
|
result = [result, *parent]
|
|
|
|
elif isinstance(parent, dict):
|
|
|
|
result = [result, parent]
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Get a normalized POSIX path for the pattern matching with removed current
|
|
|
|
# working directory prefix. Directory paths end with a '/' to allow more control
|
2024-03-12 05:55:15 +03:00
|
|
|
# in the pattern creation for files and directories. The patterns are matched
|
|
|
|
# using the search function, so they are prefixed with ^ for specificity.
|
|
|
|
def _resolve_pattern(abspath: str, return_path: bool = False):
|
|
|
|
path = abspath.replace(os.getcwd(), "", 1)
|
|
|
|
path = path.replace(os.sep, "/").rstrip("/")
|
2024-02-29 23:55:54 +03:00
|
|
|
|
|
|
|
if not path:
|
|
|
|
return "/"
|
|
|
|
|
|
|
|
# Check abspath, as the file needs to exist
|
|
|
|
if not os.path.isfile(abspath):
|
2024-03-12 05:55:15 +03:00
|
|
|
path = path + "/"
|
2024-02-29 23:55:54 +03:00
|
|
|
|
2024-03-12 05:55:15 +03:00
|
|
|
return path if return_path else f"^{path}"
|
2024-02-29 23:55:54 +03:00
|
|
|
|
2024-03-01 00:07:49 +03:00
|
|
|
# Get project configuration with resolved absolute paths for validation
|
2024-03-01 00:01:36 +03:00
|
|
|
def _get_project_config(project_config_file: str):
|
|
|
|
with open(project_config_file, encoding="utf-8") as file:
|
|
|
|
config = MkDocsConfig(config_file_path = project_config_file)
|
|
|
|
config.load_file(file)
|
|
|
|
|
|
|
|
# MkDocs transforms site_dir to absolute path during validation
|
|
|
|
config.validate()
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Data
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Set up logging
|
2023-07-05 18:39:33 +03:00
|
|
|
log = logging.getLogger("mkdocs.material.info")
|