Skip to content

Commit 30407ae

Browse files
committed
Add pip availability check and bootstrap in examples_gallery.py
1 parent 54fbe72 commit 30407ae

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

sdk/python/packages/flet/src/flet/docs_plugins/examples_gallery.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import importlib.util
2+
import logging
13
import os
24
import subprocess
5+
import sys
36
from collections.abc import Iterable
47
from pathlib import Path
58
from typing import Optional
@@ -8,6 +11,8 @@
811
from mkdocs.config.defaults import MkDocsConfig
912
from mkdocs.plugins import BasePlugin
1013

14+
logger = logging.getLogger("flet.docs.examples_gallery")
15+
1116

1217
def _is_relative_to(path: Path, base: Path) -> bool:
1318
try:
@@ -17,6 +22,27 @@ def _is_relative_to(path: Path, base: Path) -> bool:
1722
return False
1823

1924

25+
def _has_pip() -> bool:
26+
try:
27+
subprocess.run(
28+
[sys.executable, "-m", "pip", "--version"],
29+
check=True,
30+
stdout=subprocess.DEVNULL,
31+
stderr=subprocess.DEVNULL,
32+
)
33+
return True
34+
except (subprocess.CalledProcessError, FileNotFoundError):
35+
return importlib.util.find_spec("pip") is not None
36+
37+
38+
def _ensure_pip_available() -> None:
39+
"""Bootstrap pip into the active virtualenv when it's missing."""
40+
if _has_pip():
41+
return
42+
logger.info("Installing pip in the current environment for `flet publish`.")
43+
subprocess.run([sys.executable, "-m", "ensurepip", "--upgrade"], check=True)
44+
45+
2046
def _latest_mtime(root: Path, ignored: Optional[Iterable[Path]] = None) -> float:
2147
"""Return the newest mtime under ``root`` ignoring any ``ignored`` directories."""
2248
root = root.resolve()
@@ -125,4 +151,5 @@ def on_pre_build(self, config: MkDocsConfig) -> None:
125151
extra_env = self.config.get("env") or {}
126152
env.update({k: str(v) for k, v in extra_env.items()})
127153

154+
_ensure_pip_available()
128155
subprocess.run(cmd, cwd=docs_dir, check=True, env=env)

0 commit comments

Comments
 (0)