-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
167 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import IO, TypeVar, Callable | ||
|
||
_approot = Path(__file__).parent.parent | ||
|
||
assert _approot.samefile(os.getcwd()) | ||
|
||
_cachepath = _approot.joinpath('cache') | ||
|
||
F = TypeVar("F", bound=Callable) | ||
|
||
|
||
def copy_signature(_: F) -> Callable[..., F]: | ||
return lambda f: f | ||
|
||
|
||
def path(name: str) -> Path: | ||
return _cachepath.joinpath(name) | ||
|
||
|
||
@copy_signature(open) | ||
def open_file(file: str, *args, **kwargs) -> IO: | ||
if not _cachepath.exists(): | ||
_cachepath.mkdir() | ||
return open(_cachepath.joinpath(file), *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import json | ||
import re | ||
import tarfile | ||
from typing import Optional, TypedDict, NotRequired | ||
|
||
import ar | ||
import io | ||
|
||
|
||
class AppInfo(TypedDict): | ||
id: str | ||
title: str | ||
version: str | ||
type: str | ||
appDescription: NotRequired[str] | ||
|
||
|
||
def get_appinfo(ipk_path: str) -> AppInfo: | ||
with open(ipk_path, 'rb') as f: | ||
archive = ar.Archive(f) | ||
control_file = io.BytesIO(archive.open('control.tar.gz', mode='rb').read()) | ||
with tarfile.open(fileobj=control_file, mode='r:gz') as control: | ||
with control.extractfile(control.getmember('control')) as cf: | ||
package_name = re.compile(r'Package: (.+)\n').match(cf.readline().decode('utf-8')).group(1) | ||
data_file = io.BytesIO(archive.open('data.tar.gz', mode='rb').read()) | ||
with tarfile.open(fileobj=data_file, mode='r:gz') as data: | ||
with data.extractfile(data.getmember(f'usr/palm/applications/{package_name}/appinfo.json')) as af: | ||
return json.load(af) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,71 @@ | ||
import json | ||
import os | ||
import urllib.parse | ||
from datetime import datetime | ||
from email.utils import parsedate_to_datetime | ||
from json import JSONDecodeError | ||
from os import path | ||
from typing import Tuple, TypedDict, Optional | ||
from typing import Tuple, TypedDict, Optional, NotRequired, Literal | ||
from urllib.parse import urljoin | ||
from urllib.request import url2pathname | ||
|
||
import requests | ||
|
||
from repogen import cache | ||
from repogen.common import url_fixup, url_size | ||
|
||
|
||
class PackageHash(TypedDict): | ||
sha256: str | ||
|
||
|
||
class PackageManifest(TypedDict): | ||
id: str | ||
title: str | ||
version: str | ||
type: str | ||
appDescription: Optional[str] | ||
iconUri: str | ||
sourceUrl: NotRequired[str] | ||
rootRequired: NotRequired[bool | Literal['optional']] | ||
ipkUrl: str | ||
ipkHash: PackageHash | ||
ipkSize: int | ||
|
||
|
||
def obtain_manifest(pkgid: str, channel: str, url: str, offline: bool = False) -> Tuple[PackageManifest, datetime]: | ||
if not path.exists('cache'): | ||
os.mkdir('cache') | ||
cache_file = path.join('cache', f'manifest_{pkgid}_{channel}.json') | ||
try: | ||
if offline: | ||
raise requests.exceptions.ConnectionError('Offline') | ||
url = url_fixup(url) | ||
resp = requests.get(url=url, allow_redirects=True) | ||
manifest = resp.json() | ||
manifest['ipkUrl'] = urljoin(url, manifest['ipkUrl']) | ||
manifest['ipkSize'] = url_size(manifest['ipkUrl']) | ||
with open(cache_file, 'w', encoding='utf-8') as f: | ||
json.dump(manifest, f) | ||
last_modified = datetime.now() | ||
if 'last-modified' in resp.headers: | ||
last_modified = parsedate_to_datetime( | ||
resp.headers['last-modified']) | ||
os.utime(cache_file, (last_modified.timestamp(), last_modified.timestamp())) | ||
return manifest, last_modified | ||
except requests.exceptions.RequestException as e: | ||
if path.exists(cache_file): | ||
try: | ||
with open(cache_file, encoding='utf-8') as f: | ||
return json.load(f), datetime.fromtimestamp(os.stat(cache_file).st_mtime) | ||
except IOError or JSONDecodeError: | ||
os.unlink(cache_file) | ||
raise e | ||
def obtain_manifest(pkgid: str, channel: str, uri: str, offline: bool = False) -> Tuple[PackageManifest, datetime]: | ||
parsed = urllib.parse.urlparse(uri) | ||
if parsed.scheme == 'file': | ||
manifest_path = url2pathname(parsed.path) | ||
try: | ||
with open(manifest_path, encoding='utf-8') as f: | ||
return json.load(f), datetime.fromtimestamp(os.stat(manifest_path).st_mtime) | ||
except IOError or JSONDecodeError: | ||
os.unlink(manifest_path) | ||
else: | ||
cache_name = f'manifest_{pkgid}_{channel}.json' | ||
cache_file = cache.path(cache_name) | ||
try: | ||
if offline: | ||
raise requests.exceptions.ConnectionError('Offline') | ||
uri = url_fixup(uri) | ||
resp = requests.get(url=uri, allow_redirects=True) | ||
manifest = resp.json() | ||
manifest['ipkUrl'] = urljoin(uri, manifest['ipkUrl']) | ||
manifest['ipkSize'] = url_size(manifest['ipkUrl']) | ||
with cache.open_file(cache_name, mode='w', encoding='utf-8') as f: | ||
json.dump(manifest, f) | ||
last_modified = datetime.now() | ||
if 'last-modified' in resp.headers: | ||
last_modified = parsedate_to_datetime( | ||
resp.headers['last-modified']) | ||
os.utime(cache_file, (last_modified.timestamp(), last_modified.timestamp())) | ||
return manifest, last_modified | ||
except requests.exceptions.RequestException as e: | ||
if cache_file.exists(): | ||
try: | ||
with cache.open_file(cache_name, encoding='utf-8') as f: | ||
return json.load(f), datetime.fromtimestamp(cache_file.stat().st_mtime) | ||
except IOError or JSONDecodeError: | ||
cache_file.unlink() | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.