1+ import importlib .util
2+ import logging
13import os
24import subprocess
5+ import sys
36from collections .abc import Iterable
47from pathlib import Path
58from typing import Optional
811from mkdocs .config .defaults import MkDocsConfig
912from mkdocs .plugins import BasePlugin
1013
14+ logger = logging .getLogger ("flet.docs.examples_gallery" )
15+
1116
1217def _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+
2046def _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