2023-01-02 13:08:58 +03:00
|
|
|
# Copyright (c) 2016-2023 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.
|
|
|
|
|
2023-01-02 12:55:24 +03:00
|
|
|
import json
|
2022-12-18 17:41:18 +03:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import platform
|
2022-12-18 19:23:12 +03:00
|
|
|
import requests
|
2022-12-18 17:41:18 +03:00
|
|
|
import sys
|
|
|
|
|
|
|
|
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
|
2022-12-18 17:41:18 +03:00
|
|
|
from mkdocs import utils
|
|
|
|
from mkdocs.plugins import BasePlugin, event_priority
|
|
|
|
from mkdocs.structure.files import get_files
|
|
|
|
from zipfile import ZipFile, ZIP_DEFLATED
|
|
|
|
|
2023-07-05 18:39:33 +03:00
|
|
|
from material.plugins.info.config import InfoConfig
|
|
|
|
|
2022-12-18 17:41:18 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Class
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
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.
|
|
|
|
# The user 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
|
|
|
|
|
|
|
# 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.
|
|
|
|
base = utils.get_theme_dir(config.theme.name)
|
|
|
|
if config.theme.dirs.index(base):
|
|
|
|
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
|
|
|
|
2023-08-10 16:32:46 +03:00
|
|
|
# Create in-memory archive and prompt user to enter a short descriptive
|
|
|
|
# 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
|
|
|
|
# file extension that the user 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)
|
|
|
|
example = slugify(example, "-")
|
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:
|
|
|
|
for path in ["mkdocs.yml", "requirements.txt"]:
|
|
|
|
if os.path.isfile(path):
|
2023-08-10 16:32:46 +03:00
|
|
|
f.write(path, os.path.join(example, path))
|
2022-12-18 17:41:18 +03:00
|
|
|
|
|
|
|
# Append all files visible to MkDocs
|
|
|
|
for file in get_files(config):
|
|
|
|
path = os.path.relpath(file.abs_src_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
|
|
|
|
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-01-02 12:55:24 +03:00
|
|
|
# Add information in 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(),
|
|
|
|
"python": platform.python_version()
|
|
|
|
},
|
|
|
|
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")
|
|
|
|
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
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Data
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Set up logging
|
2023-07-05 18:39:33 +03:00
|
|
|
log = logging.getLogger("mkdocs.material.info")
|