Skip to content

Commit

Permalink
Include undetected_chromedriver binary in Docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
smkent committed Jan 28, 2024
1 parent 60eba18 commit 8375843
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ COPY docker/entrypoint /

COPY . /python-build
RUN python3 -m pip install /python-build && rm -rf /python-build
RUN safeway-coupons-init-chromedriver

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/entrypoint"]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ responses = "*"

[tool.poetry.scripts]
safeway-coupons = "safeway_coupons.app:main"
safeway-coupons-init-chromedriver = "safeway_coupons.chrome_driver:init"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
37 changes: 37 additions & 0 deletions safeway_coupons/chrome_driver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import contextlib
import subprocess
import sys
from pathlib import Path
from typing import Iterator

import undetected_chromedriver as uc # type: ignore

CHROMEDRIVER_PATH = (
Path.home()
/ ".local"
/ "share"
/ "undetected_chromedriver"
/ "undetected_chromedriver"
)


class ChromeDriverDoesNotExist(Exception):
pass


@contextlib.contextmanager
def chrome_driver(headless: bool = True) -> Iterator[uc.Chrome]:
Expand All @@ -23,3 +38,25 @@ def chrome_driver(headless: bool = True) -> Iterator[uc.Chrome]:
driver = uc.Chrome(options=options)
yield driver
driver.quit()


def chrome_driver_version() -> str:
if not CHROMEDRIVER_PATH.is_file():
raise ChromeDriverDoesNotExist(

Check warning on line 45 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L44-L45

Added lines #L44 - L45 were not covered by tests
f"Error: {CHROMEDRIVER_PATH} does not exist"
)
cmd = [str(CHROMEDRIVER_PATH), "--version"]
print(f"+ {' '.join(cmd)}", file=sys.stderr)
result = subprocess.run(cmd, capture_output=True)
return result.stdout.decode()

Check warning on line 51 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L48-L51

Added lines #L48 - L51 were not covered by tests


def init() -> None:
with contextlib.suppress(ChromeDriverDoesNotExist):
print(chrome_driver_version())
return
print("Initializing Chrome Driver")
with chrome_driver() as driver:
print("Connect to example.com")
driver.get("https://example.com")
print(chrome_driver_version())

Check warning on line 62 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L55-L62

Added lines #L55 - L62 were not covered by tests

0 comments on commit 8375843

Please sign in to comment.