Skip to content

Commit

Permalink
feat: add new command install-deps to install required playwright bro…
Browse files Browse the repository at this point in the history
…wsers
  • Loading branch information
netomi committed Jan 23, 2024
1 parent 1588e62 commit c7a355d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [0.4.0] - unreleased

### Added

- Added operation `install-deps` in order to install required runtime dependencies (firefox browser).

### Changed

- Improve coercing of organization-level settings for repository settings. ([#161](https://github.com/eclipse-csi/otterdog/issues/161))
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ The documentation is available at [otterdog.readthedocs.io](https://otterdog.rea
### Building Steps

* Create a virtual python environment and install necessary python dependencies using poetry:

```console
$ make init
```

* Testing build

```console
$ ./otterdog.sh -h
```
Expand Down
22 changes: 21 additions & 1 deletion otterdog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,25 @@ def web_login(organizations: list[str]):
_execute_operation(organizations, WebLoginOperation())


@cli.command(short_help="Installs required dependencies.")
def install_deps():
"""
Installs required dependencies.
"""

import subprocess

process = subprocess.Popen(args=["playwright", "install", "firefox"], stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), b""):
sys.stdout.buffer.write(c)
sys.stdout.flush()

status = process.wait()

if status != 0:
print_error(f"could not install required dependencies: {status}")


def _execute_operation(organizations: list[str], operation: Operation):
printer = IndentingPrinter(sys.stdout)
printer.println()
Expand All @@ -508,7 +527,8 @@ def _execute_operation(organizations: list[str], operation: Operation):
sys.exit(exit_code)

except Exception as e:
traceback.print_exception(e)
if is_debug_enabled():
traceback.print_exception(e)

print_error(str(e))
sys.exit(2)
Expand Down
35 changes: 29 additions & 6 deletions otterdog/providers/github/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from typing import Any

from importlib_resources import files
from playwright.sync_api import Error, Page, sync_playwright
from playwright.sync_api import Error as PlaywrightError
from playwright.sync_api import Page, sync_playwright

from otterdog import resources, utils
from otterdog.credentials import Credentials
Expand All @@ -36,7 +37,14 @@ def get_org_settings(self, org_id: str, included_keys: set[str]) -> dict[str, An
utils.print_debug("retrieving settings via web interface")

with sync_playwright() as playwright:
browser = playwright.firefox.launch()
try:
browser = playwright.firefox.launch()
except Exception as e:
tb = e.__traceback__
raise RuntimeError(
"unable to launch browser, make sure you have installed required dependencies using: "
"'otterdog install-deps'"
).with_traceback(tb) from None

page = browser.new_page()
page.set_default_timeout(self._DEFAULT_TIMEOUT)
Expand Down Expand Up @@ -132,7 +140,14 @@ def update_org_settings(self, org_id: str, data: dict[str, Any]) -> None:
utils.print_debug("updating settings via web interface")

with sync_playwright() as playwright:
browser = playwright.firefox.launch()
try:
browser = playwright.firefox.launch()
except Exception as e:
tb = e.__traceback__
raise RuntimeError(
"unable to launch browser, make sure you have installed required dependencies using: "
"'otterdog install-deps'"
).with_traceback(tb) from None

page = browser.new_page()
page.set_default_timeout(self._DEFAULT_TIMEOUT)
Expand Down Expand Up @@ -221,7 +236,15 @@ def open_browser_with_logged_in_user(self, org_id: str) -> None:
utils.print_debug("opening browser window")

with sync_playwright() as playwright:
browser = playwright.firefox.launch(headless=False)
try:
browser = playwright.firefox.launch(headless=False)
except Exception as e:
tb = e.__traceback__
raise RuntimeError(
"unable to launch browser, make sure you have installed required dependencies using: "
"'otterdog install-deps'"
).with_traceback(tb) from None

context = browser.new_context(no_viewport=True)

page = context.new_page()
Expand Down Expand Up @@ -255,7 +278,7 @@ def _logged_in_as(page: Page) -> str:

try:
actor = page.eval_on_selector('meta[name="octolytics-actor-login"]', "element => element.content")
except Error:
except PlaywrightError:
actor = None

return actor
Expand Down Expand Up @@ -293,7 +316,7 @@ def _login(self, page: Page) -> None:
confirm_button.click()

page.type("#app_totp", self.credentials.totp)
except Error as e:
except PlaywrightError as e:
raise RuntimeError(f"could not log in to web UI: {str(e)}")

def _logout(self, page: Page) -> None:
Expand Down

0 comments on commit c7a355d

Please sign in to comment.