pythonlib: Look for assets in earlier releases #134 0.4.9

This commit is contained in:
daijro 2024-12-13 01:01:20 -06:00
parent 9a9e61f660
commit e3d3dcd6e7
4 changed files with 112 additions and 43 deletions

View file

@ -6,6 +6,14 @@ class UnsupportedVersion(Exception):
...
class MissingRelease(Exception):
"""
Raised when a required GitHub release asset is missing.
"""
...
class UnsupportedArchitecture(Exception):
"""
Raised when the architecture is not supported.

View file

@ -5,11 +5,12 @@ from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union, cast
import numpy as np
from language_tags import tags
from camoufox.pkgman import LOCAL_DATA, rprint, webdl
from camoufox.pkgman import LOCAL_DATA, GitHubDownloader, rprint, webdl
from camoufox.warnings import LeakWarning
from .exceptions import (
InvalidLocale,
MissingRelease,
NotInstalledGeoIPExtra,
UnknownIPLocation,
UnknownLanguage,
@ -189,7 +190,22 @@ Helpers to fetch geolocation, timezone, and locale data given an IP.
"""
MMDB_FILE = LOCAL_DATA / 'GeoLite2-City.mmdb'
MMDB_URL = 'https://github.com/P3TERX/GeoLite.mmdb/releases/latest/download/GeoLite2-City.mmdb'
MMDB_REPO = "P3TERX/GeoLite.mmdb"
class MaxMindDownloader(GitHubDownloader):
"""
MaxMind database downloader from a GitHub repository.
"""
def check_asset(self, asset: Dict) -> Optional[str]:
# Check for the first -City.mmdb file
if asset['name'].endswith('-City.mmdb'):
return asset['browser_download_url']
return None
def missing_asset_error(self) -> None:
raise MissingRelease('Failed to find GeoIP database release asset')
def geoip_allowed() -> None:
@ -208,9 +224,11 @@ def download_mmdb() -> None:
"""
geoip_allowed()
asset_url = MaxMindDownloader(MMDB_REPO).get_asset()
with open(MMDB_FILE, 'wb') as f:
webdl(
MMDB_URL,
asset_url,
desc='Downloading GeoIP database',
buffer=f,
)

View file

@ -23,6 +23,7 @@ from yaml import CLoader, load
from .__version__ import CONSTRAINTS
from .exceptions import (
CamoufoxNotInstalled,
MissingRelease,
UnsupportedArchitecture,
UnsupportedOS,
UnsupportedVersion,
@ -136,12 +137,59 @@ class Version:
VERSION_MIN, VERSION_MAX = Version.build_minmax()
class CamoufoxFetcher:
class GitHubDownloader:
"""
Manages fetching and installing GitHub releases.
"""
def __init__(self, github_repo: str) -> None:
self.github_repo = github_repo
self.api_url = f"https://api.github.com/repos/{github_repo}/releases"
def check_asset(self, asset: Dict) -> Any:
"""
Compare the asset to determine if it's the desired asset.
Args:
asset: Asset information from GitHub API
Returns:
Any: Data to be returned if this is the desired asset, or None/False if not
"""
return asset.get('browser_download_url')
def missing_asset_error(self) -> None:
"""
Raise a MissingRelease exception if no release is found.
"""
raise MissingRelease(f"Could not find a release asset in {self.github_repo}.")
def get_asset(self) -> Any:
"""
Fetch the latest release from the GitHub API.
Gets the first asset that returns a truthy value from check_asset.
"""
resp = requests.get(self.api_url, timeout=20)
resp.raise_for_status()
releases = resp.json()
for release in releases:
for asset in release['assets']:
if data := self.check_asset(asset):
return data
self.missing_asset_error()
class CamoufoxFetcher(GitHubDownloader):
"""
Handles fetching and installing the latest version of Camoufox.
"""
def __init__(self) -> None:
super().__init__("daijro/camoufox")
self.arch = self.get_platform_arch()
self._version_obj: Optional[Version] = None
self.pattern: re.Pattern = re.compile(
@ -150,6 +198,37 @@ class CamoufoxFetcher:
self.fetch_latest()
def check_asset(self, asset: Dict) -> Optional[Tuple[Version, str]]:
"""
Finds the latest release from a GitHub releases API response that
supports the Camoufox version constraints, the OS, and architecture.
Returns:
Optional[Tuple[Version, str]]: The version and URL of a release
"""
# Search through releases for the first supported version
match = self.pattern.match(asset['name'])
if not match:
return None
# Check if the version is supported
version = Version(release=match['release'], version=match['version'])
if not version.is_supported():
return None
# Asset was found. Return data
return version, asset['browser_download_url']
def missing_asset_error(self) -> None:
"""
Raise a MissingRelease exception if no release is found.
"""
raise MissingRelease(
f"No matching release found for {OS_NAME} {self.arch} in the "
f"supported range: ({CONSTRAINTS.as_range()}). "
"Please update the Python library."
)
@staticmethod
def get_platform_arch() -> str:
"""
@ -175,30 +254,6 @@ class CamoufoxFetcher:
return arch
def find_release(self, releases: List[Dict]) -> Optional[Tuple[Version, str]]:
"""
Finds the latest release from a GitHub releases API response that
supports the Camoufox version constraints, the OS, and architecture.
Returns:
Optional[Tuple[Version, str]]: The version and URL of a release
"""
# Search through releases for the first supported version
for release in releases:
for asset in release['assets']:
match = self.pattern.match(asset['name'])
if not match:
continue
# Check if the version is supported
version = Version(release=match['release'], version=match['version'])
if not version.is_supported():
continue
# Asset was found. Return data
return version, asset['browser_download_url']
return None
def fetch_latest(self) -> None:
"""
Fetch the URL of the latest camoufox release for the current platform.
@ -208,20 +263,7 @@ class CamoufoxFetcher:
requests.RequestException: If there's an error fetching release data
ValueError: If no matching release is found for the current platform
"""
api_url = "https://api.github.com/repos/daijro/camoufox/releases"
resp = requests.get(api_url, timeout=20)
resp.raise_for_status()
# Find a release that fits the constraints
releases = resp.json()
release_data = self.find_release(releases)
if release_data is None:
raise UnsupportedVersion(
f"No matching release found for {OS_NAME} {self.arch} in the "
f"supported range: ({CONSTRAINTS.as_range()}). "
"Please update the Python library."
)
release_data = self.get_asset()
# Set the version and URL
self._version_obj, self._url = release_data
@ -232,6 +274,7 @@ class CamoufoxFetcher:
Download a file from the given URL and return it as BytesIO.
Args:
file (DownloadBuffer): The buffer to download to
url (str): The URL to download the file from
Returns:

View file

@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "camoufox"
version = "0.4.8"
version = "0.4.9"
description = "Wrapper around Playwright to help launch Camoufox"
authors = ["daijro <daijro.dev@gmail.com>"]
license = "MIT"