| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| import getpass |
| import glob |
| import json |
| import logging |
| import os |
| import platform |
| import re |
| import requests |
| import site |
| import sys |
| import yaml |
|
|
| from colorama import Fore, Style |
| from importlib.metadata import distributions, version |
| from io import BytesIO |
| from markdown.extensions.toc import slugify |
| from mkdocs.config.defaults import MkDocsConfig |
| from mkdocs.plugins import BasePlugin, event_priority |
| from mkdocs.utils.yaml import get_yaml_loader |
| from zipfile import ZipFile, ZIP_DEFLATED |
|
|
| from .config import InfoConfig |
| from .patterns import get_exclusion_patterns |
|
|
| |
| |
| |
|
|
| |
| class InfoPlugin(BasePlugin[InfoConfig]): |
|
|
| |
| def __init__(self, *args, **kwargs): |
| super().__init__(*args, **kwargs) |
|
|
| |
| self.is_serve = False |
|
|
| |
| self.exclusion_patterns = [] |
| self.excluded_entries = [] |
|
|
| |
| def on_startup(self, *, command, dirty): |
| self.is_serve = command == "serve" |
|
|
| |
| |
| |
| |
| @event_priority(100) |
| def on_config(self, config): |
| if not self.config.enabled: |
| return |
|
|
| |
| |
| |
| if not self.config.enabled_on_serve and self.is_serve: |
| return |
|
|
| |
| url = "https://github.com/squidfunk/mkdocs-material/releases/latest" |
| res = requests.get(url, allow_redirects = False) |
|
|
| |
| _, current = res.headers.get("location").rsplit("/", 1) |
| present = version("mkdocs-material") |
| if not present.startswith(current): |
| log.error("Please upgrade to the latest version.") |
| self._help_on_versions_and_exit(present, current) |
|
|
| |
| if not self.config.archive: |
| sys.exit(1) |
|
|
| |
| log.info("Started archive creation for bug report") |
|
|
| |
| |
| |
| |
| if config.theme.custom_dir: |
| log.error("Please remove 'custom_dir' setting.") |
| self._help_on_customizations_and_exit() |
|
|
| |
| |
| |
| |
| if config.hooks: |
| log.error("Please remove 'hooks' setting.") |
| self._help_on_customizations_and_exit() |
|
|
| |
| |
| |
| config.config_file_path = _convert_to_abs(config.config_file_path) |
| config_file_parent = os.path.dirname(config.config_file_path) |
|
|
| |
| |
| if config.theme.custom_dir: |
| abs_custom_dir = _convert_to_abs( |
| config.theme.custom_dir, |
| abs_prefix = config_file_parent |
| ) |
| else: |
| abs_custom_dir = "" |
|
|
| |
| |
| projects_plugin = config.plugins.get("material/projects") |
| if projects_plugin: |
| abs_projects_dir = _convert_to_abs( |
| projects_plugin.config.projects_dir, |
| abs_prefix = config_file_parent |
| ) |
| else: |
| abs_projects_dir = "" |
|
|
| |
| |
| |
| |
| |
| |
| |
| loaded_configs = _load_yaml(config.config_file_path) |
| if not isinstance(loaded_configs, list): |
| loaded_configs = [loaded_configs] |
|
|
| |
| |
| |
| site_prefixes = list(map(capitalize, site.PREFIXES)) |
| cwd = capitalize(os.getcwd()) |
|
|
| |
| |
| |
| paths_to_validate = list(map(capitalize, [ |
| config.config_file_path, |
| config.docs_dir, |
| abs_custom_dir, |
| abs_projects_dir, |
| *[cfg.get("INHERIT", "") for cfg in loaded_configs] |
| ])) |
|
|
| |
| for hook in config.hooks: |
| path = _convert_to_abs(hook, abs_prefix = config_file_parent) |
| paths_to_validate.append(path) |
|
|
| |
| for path in list(paths_to_validate): |
| if not path or path.startswith(cwd): |
| paths_to_validate.remove(path) |
|
|
| |
| if paths_to_validate: |
| log.error("One or more paths aren't children of root") |
| self._help_on_not_in_cwd(paths_to_validate) |
|
|
| |
| |
| |
| |
| archive = BytesIO() |
| example = input("\nPlease name your bug report (2-4 words): ") |
| example, _ = os.path.splitext(example) |
| example = "-".join([present, slugify(example, "-")]) |
|
|
| |
| self.exclusion_patterns = get_exclusion_patterns() |
| self.excluded_entries = [] |
|
|
| |
| if capitalize(config.site_dir).startswith(cwd): |
| self.exclusion_patterns.append(_resolve_pattern(config.site_dir)) |
|
|
| |
| |
| |
| for path in site_prefixes: |
| if path.startswith(cwd): |
| self.exclusion_patterns.append(_resolve_pattern(path)) |
|
|
| |
| |
| |
| for abs_root, dirnames, filenames in os.walk(os.getcwd()): |
| for filename in filenames: |
| if filename.lower() != "pyvenv.cfg": |
| continue |
|
|
| path = capitalize(abs_root) |
|
|
| if path not in site_prefixes: |
| print(f"Possible inactive venv: {path}") |
| self.exclusion_patterns.append(_resolve_pattern(path)) |
|
|
| |
| 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) |
|
|
| |
| contains_dotpath: bool = False |
|
|
| |
| files: list[str] = [] |
| with ZipFile(archive, "a", ZIP_DEFLATED, False) as f: |
| for abs_root, dirnames, filenames in os.walk(os.getcwd()): |
| |
| indicator = f"Processing: {abs_root}" |
| print(indicator, end="\r", flush=True) |
|
|
| |
| for name in list(dirnames): |
| |
| path = os.path.join(abs_root, name) |
|
|
| |
| if self._is_excluded(path): |
| dirnames.remove(name) |
| continue |
|
|
| |
| if _is_dotpath(path, log_warning = True): |
| contains_dotpath = True |
|
|
| |
| for name in filenames: |
| |
| path = os.path.join(abs_root, name) |
|
|
| |
| if self._is_excluded(path): |
| continue |
|
|
| |
| if _is_dotpath(path, log_warning = True): |
| contains_dotpath = True |
|
|
| |
| path = os.path.relpath(path, os.path.curdir) |
| f.write(path, os.path.join(example, path)) |
|
|
| |
| print(" " * len(indicator), end="\r", flush=True) |
|
|
| |
| f.writestr( |
| os.path.join(example, "requirements.lock.txt"), |
| "\n".join(sorted([ |
| "==".join([package.name, package.version]) |
| for package in distributions() |
| ])) |
| ) |
|
|
| |
| try: |
| username = getpass.getuser() |
| except Exception: |
| username = "USERNAME" |
|
|
| |
| f.writestr( |
| os.path.join(example, "platform.json"), |
| json.dumps( |
| { |
| "system": platform.platform(), |
| "architecture": platform.architecture(), |
| "python": platform.python_version(), |
| "cwd": os.getcwd(), |
| "command": " ".join([ |
| sys.argv[0].rsplit(os.sep, 1)[-1], |
| *sys.argv[1:] |
| ]), |
| "env:$PYTHONPATH": os.getenv("PYTHONPATH", ""), |
| "env:$VIRTUAL_ENV": os.getenv("VIRTUAL_ENV", ""), |
| "sys.path": sys.path, |
| "excluded_entries": self.excluded_entries |
| }, |
| default = str, |
| indent = 2 |
| ).replace(username, "USERNAME") |
| ) |
|
|
| |
| for a in f.filelist: |
| |
| color = (Fore.LIGHTYELLOW_EX if "/." in a.filename |
| else Fore.LIGHTBLACK_EX) |
| files.append("".join([ |
| color, a.filename, " ", |
| _size(a.compress_size) |
| ])) |
|
|
| |
| buffer = archive.getbuffer() |
| with open(f"{example}.zip", "wb") as f: |
| f.write(archive.getvalue()) |
|
|
| |
| log.info("Archive successfully created:") |
| print(Style.NORMAL) |
|
|
| |
| files.sort() |
| for file in files: |
| print(f" {file}") |
|
|
| |
| print(Style.RESET_ALL) |
| print("".join([ |
| " ", f.name, " ", |
| _size(buffer.nbytes, 10) |
| ])) |
|
|
| |
| print(Style.RESET_ALL) |
| if buffer.nbytes > 1000000: |
| log.warning("Archive exceeds recommended maximum size of 1 MB") |
|
|
| |
| if contains_dotpath: |
| log.warning( |
| "Archive contains dotpaths, which could contain sensitive " |
| "information.\nPlease review them at the bottom of the list " |
| "and share only necessary data to reproduce the issue." |
| ) |
|
|
| |
| sys.exit(1) |
|
|
| |
|
|
| |
| def _help_on_versions_and_exit(self, have, need): |
| print(Fore.RED) |
| print(" When reporting issues, please first upgrade to the latest") |
| print(" version of Material for MkDocs, as the problem might already") |
| print(" be fixed in the latest version. This helps reduce duplicate") |
| print(" efforts and saves us maintainers time.") |
| print(Style.NORMAL) |
| print(f" Please update from {have} to {need}.") |
| print(Style.RESET_ALL) |
| print(f" pip install --upgrade --force-reinstall mkdocs-material") |
| print(Style.NORMAL) |
|
|
| |
| if self.config.archive_stop_on_violation: |
| sys.exit(1) |
|
|
| |
| def _help_on_customizations_and_exit(self): |
| 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") |
| print(" help you debug your customizations. Please remove:") |
| 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") |
| 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.") |
| print(Style.RESET_ALL) |
|
|
| |
| if self.config.archive_stop_on_violation: |
| sys.exit(1) |
|
|
| |
| def _help_on_not_in_cwd(self, outside_root): |
| 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) |
| for path in outside_root: |
| print(f" {path}") |
| print("\n To assure that all project files are found please adjust") |
| print(" your config or file structure and put everything within the") |
| print(" root directory of the project.") |
| print("\n Please also make sure `mkdocs build` is run in the actual") |
| print(" root directory of the project.") |
| print(Style.RESET_ALL) |
|
|
| |
| if self.config.archive_stop_on_violation: |
| sys.exit(1) |
|
|
| |
| |
| |
| def _is_excluded(self, abspath: str) -> bool: |
|
|
| |
| pattern_path = _resolve_pattern(abspath, return_path = True) |
| for pattern in self.exclusion_patterns: |
| if re.search(pattern, pattern_path): |
| log.debug(f"Excluded pattern '{pattern}': {abspath}") |
| self.excluded_entries.append(f"{pattern} - {pattern_path}") |
| return True |
|
|
| |
| if os.path.isfile(abspath): |
| return False |
|
|
| |
| |
| |
| |
| 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 |
|
|
| return False |
|
|
| |
| |
| |
|
|
| |
| def _size(value, factor = 1): |
| color = Fore.GREEN |
| if value > 100000 * factor: color = Fore.RED |
| elif value > 25000 * factor: color = Fore.YELLOW |
| for unit in ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"]: |
| if abs(value) < 1000.0: |
| return f"{color}{value:3.1f} {unit}" |
| value /= 1000.0 |
|
|
| |
| |
| |
| 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)) |
|
|
| |
| |
| |
| |
| |
| def _load_yaml(abs_src_path: str): |
|
|
| with open(abs_src_path, 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 |
|
|
| |
| |
| |
| |
| def _resolve_pattern(abspath: str, return_path: bool = False): |
| path = capitalize(abspath).replace(capitalize(os.getcwd()), "", 1) |
| path = path.replace(os.sep, "/").rstrip("/") |
|
|
| if not path: |
| return "/" |
|
|
| |
| if not os.path.isfile(abspath): |
| path = path + "/" |
|
|
| return path if return_path else f"^{path}" |
|
|
| |
| def _get_project_config(project_config_file: str): |
| with open(project_config_file, encoding="utf-8-sig") as file: |
| config = MkDocsConfig(config_file_path = project_config_file) |
| config.load_file(file) |
|
|
| |
| config.validate() |
|
|
| return config |
|
|
| |
| |
| def _is_dotpath(path: str, log_warning: bool = False) -> bool: |
| posix_path = _resolve_pattern(path, return_path = True) |
| name = posix_path.rstrip("/").rsplit("/", 1)[-1] |
| if name.startswith("."): |
| if log_warning: |
| log.warning(f"The following .dotpath will be included: {path}") |
| return True |
| return False |
|
|
| |
| |
| |
| def capitalize(path: str): |
| return path[0].upper() + path[1:] if path else path |
|
|
| |
| |
| |
|
|
| |
| log = logging.getLogger("mkdocs.material.info") |
|
|