Skip to content

Commit

Permalink
MAINT: Make selenium optional and use on CircleCI (mne-tools#12263)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McCloy <[email protected]>
  • Loading branch information
2 people authored and snwnde committed Mar 20, 2024
1 parent a416502 commit 01d558e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
44 changes: 26 additions & 18 deletions doc/sphinxext/contrib_avatars.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os
from pathlib import Path

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException


def generate_contrib_avatars(app, config):
"""Render a template webpage with avatars generated by JS and a GitHub API call."""
root = Path(app.srcdir)
infile = root / "sphinxext" / "_avatar_template.html"
outfile = root / "_templates" / "avatars.html"
try:
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
except WebDriverException:
options = webdriver.FirefoxOptions()
options.add_argument("--headless=new")
driver = webdriver.Firefox(options=options)
driver.get(f"file://{infile}")
wait = WebDriverWait(driver, 20)
wait.until(lambda d: d.find_element(by=By.ID, value="contributor-avatars"))
body = driver.find_element(by=By.TAG_NAME, value="body").get_attribute("innerHTML")
if os.getenv("MNE_ADD_CONTRIBUTOR_IMAGE", "false").lower() != "true":
body = """\
<p>Contributor avators will appear here in full doc builds. Set \
MNE_ADD_CONTRIBUTOR_IMAGE=true in your environment to generate it.</p>"""
else:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException

try:
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
except WebDriverException:
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
driver.get(f"file://{infile}")
wait = WebDriverWait(driver, 20)
wait.until(lambda d: d.find_element(by=By.ID, value="contributor-avatars"))
body = driver.find_element(by=By.TAG_NAME, value="body").get_attribute(
"innerHTML"
)
driver.quit()
with open(outfile, "w") as fid:
fid.write(body)
driver.quit()


def setup(app):
Expand Down
1 change: 1 addition & 0 deletions tools/circleci_bash_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ source tools/get_minimal_commands.sh
echo "export MNE_3D_BACKEND=pyvistaqt" >> $BASH_ENV
echo "export MNE_BROWSER_BACKEND=qt" >> $BASH_ENV
echo "export MNE_BROWSER_PRECOMPUTE=false" >> $BASH_ENV
echo "export MNE_ADD_CONTRIBUTOR_IMAGE=true" >> $BASH_ENV
echo "export PATH=~/.local/bin/:$PATH" >> $BASH_ENV
echo "export DISPLAY=:99" >> $BASH_ENV
echo "source ~/python_env/bin/activate" >> $BASH_ENV
Expand Down

0 comments on commit 01d558e

Please sign in to comment.