| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import time |
| 5 |
|
| 6 |
from setup import Command, is_ci |
| 7 |
|
| 8 |
|
| 9 |
class BrowserDeps(Command): |
| 10 |
description = 'Download the binary dependencies needed for browser automation' |
| 11 |
usage_help = ( |
| 12 |
'The Camoufox browser and the browserforge fingerprint data files are normally downloaded on demand,' |
| 13 |
' the first time browser automation is used. This command downloads them ahead of time, which is what' |
| 14 |
' CI does so that the browser automation tests are not skipped.' |
| 15 |
) |
| 16 |
|
| 17 |
def add_options(self, parser): |
| 18 |
parser.add_option('--allow-prerelease', default=False, action='store_true', help='Use pre-release builds of the Camoufox browser') |
| 19 |
parser.add_option('--num-attempts', type=int, default=5 if is_ci else 1, help='Number of times to retry the download before giving up') |
| 20 |
|
| 21 |
def download(self, func, *a, **kw): |
| 22 |
# Downloading hundreds of megabytes from GitHub is flaky often enough to be worth retrying |
| 23 |
for attempt in range(self.num_attempts): |
| 24 |
try: |
| 25 |
return func(*a, **kw) |
| 26 |
except Exception as err: |
| 27 |
# A 403 means a rate limit or an authorization failure, neither of |
| 28 |
# which will resolve itself in the few seconds we would wait |
| 29 |
if getattr(err, 'code', -1) == 403: |
| 30 |
raise |
| 31 |
if attempt >= self.num_attempts - 1: |
| 32 |
raise |
| 33 |
self.info(f'Download failed with error: {err}, retrying...') |
| 34 |
time.sleep(2 * (attempt + 1)) |
| 35 |
|
| 36 |
def run(self, opts): |
| 37 |
from calibre.web.automate.download_deps import browserforge_data, camoufox_binary |
| 38 |
|
| 39 |
self.num_attempts = max(1, opts.num_attempts) |
| 40 |
self.info('Camoufox browser:', self.download(camoufox_binary, allow_prerelease=opts.allow_prerelease)) |
| 41 |
# browserforge_data() falls back to the data files bundled with apify_fingerprint_datapoints, so retrying it is pointless |
| 42 |
self.info('browserforge data:', browserforge_data(patch_browserforge=False)) |