From 62063a144828d4771fc86a2000b6c42f6774ab99 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Mon, 3 Mar 2025 12:53:58 +0300 Subject: [PATCH] [+] update .p43 1. partially improve crypto; 2. fix m.py; --- .gitignore | 1 + m.py | 77 +++++++++++++++---- .../pr34/commands_typed/cli_bootstrap.py | 2 +- .../fxreader/pr34/commands_typed/crypto.py | 40 +++++++--- python/pyproject.toml | 6 +- 5 files changed, 96 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 126a54f..21808db 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ d2/book1/books .vscode/* !.vscode/launch.json python/build +.*.kate-swp diff --git a/m.py b/m.py index fed3334..d4a3cb7 100755 --- a/m.py +++ b/m.py @@ -17,7 +17,10 @@ logger = logging.getLogger(__name__) class PyProject: dependencies: dict[str, list[str]] early_features: Optional[list[str]] = None - + pip_find_links: Optional[list[pathlib.Path]] = None + runtime_libdirs: Optional[list[pathlib.Path]] = None + runtime_preload: Optional[list[pathlib.Path]] = None + def pyproject_load( d: pathlib.Path, ) -> PyProject: @@ -42,7 +45,7 @@ def pyproject_load( assert isinstance(v, list) assert isinstance(k, str) - dependencies[k] = v + dependencies[k] = v res = PyProject( @@ -65,6 +68,26 @@ def pyproject_load( if 'early_features' in content['tool'][tool_name]: res.early_features = content['tool'][tool_name]['early_features'] + if 'pip_find_links' in content['tool'][tool_name]: + res.pip_find_links = [ + d.parent / pathlib.Path(o) + for o in content['tool'][tool_name]['pip_find_links'] + ] + + if 'runtime_libdirs' in content['tool'][tool_name]: + res.runtime_libdirs = [ + d.parent / pathlib.Path(o) + # pathlib.Path(o) + for o in content['tool'][tool_name]['runtime_libdirs'] + ] + + if 'runtime_preload' in content['tool'][tool_name]: + res.runtime_preload = [ + d.parent / pathlib.Path(o) + # pathlib.Path(o) + for o in content['tool'][tool_name]['runtime_preload'] + ] + return res @dataclasses.dataclass @@ -92,8 +115,21 @@ def env_bootstrap( bootstrap_settings: BootstrapSettings, pyproject: PyProject, ) -> None: + pip_find_links : list[pathlib.Path] = [] + + if not pyproject.pip_find_links is None: + pip_find_links.extend(pyproject.pip_find_links) + + pip_find_links_args = sum([ + ['-f', str(o),] + for o in pip_find_links + ], []) + subprocess.check_call([ - 'uv', 'venv', '--seed', + 'uv', 'venv', + *pip_find_links_args, + # '--seed', + '--offline', str(bootstrap_settings.env_path) ]) @@ -101,31 +137,36 @@ def env_bootstrap( 'uv', 'pip', 'install', + *pip_find_links_args, '-p', bootstrap_settings.python_path, - 'uv', + '--offline', + 'uv', 'pip', ]) subprocess.check_call([ bootstrap_settings.python_path, '-m', 'uv', 'pip', 'install', + *pip_find_links_args, + '--offline', 'build', 'setuptools', 'meson-python', 'pybind11', ]) - early_wheels = glob.glob( - str( - pathlib.Path(__file__).parent / 'deps' / 'dist' / 'early' / '*.whl' - ) - ) + # early_wheels = glob.glob( + # str( + # pathlib.Path(__file__).parent / 'deps' / 'dist' / 'early' / '*.whl' + # ) + # ) - if len(early_wheels) > 0: - subprocess.check_call([ - bootstrap_settings.python_path, - '-m', - 'uv', 'pip', 'install', - *early_wheels, - ]) + # if len(early_wheels) > 0: + # subprocess.check_call([ + # bootstrap_settings.python_path, + # '-m', + # 'uv', 'pip', 'install', + # '--offline', + # *early_wheels, + # ]) if pyproject.early_features: early_dependencies = sum([ @@ -136,11 +177,15 @@ def env_bootstrap( logger.info(dict( early_dependencies=early_dependencies, )) + if len(early_dependencies) > 0: subprocess.check_call([ bootstrap_settings.python_path, '-m', 'uv', 'pip', 'install', + *pip_find_links_args, + # '-f', str(pathlib.Path(__file__).parent / 'deps' / 'dist'), + '--offline', *early_dependencies, ]) diff --git a/python/online/fxreader/pr34/commands_typed/cli_bootstrap.py b/python/online/fxreader/pr34/commands_typed/cli_bootstrap.py index ab596c4..b34ee7c 100644 --- a/python/online/fxreader/pr34/commands_typed/cli_bootstrap.py +++ b/python/online/fxreader/pr34/commands_typed/cli_bootstrap.py @@ -20,7 +20,7 @@ class PyProject: pip_find_links: Optional[list[pathlib.Path]] = None runtime_libdirs: Optional[list[pathlib.Path]] = None runtime_preload: Optional[list[pathlib.Path]] = None - + def pyproject_load( d: pathlib.Path, ) -> PyProject: diff --git a/python/online/fxreader/pr34/commands_typed/crypto.py b/python/online/fxreader/pr34/commands_typed/crypto.py index 68832ef..721d81d 100644 --- a/python/online/fxreader/pr34/commands_typed/crypto.py +++ b/python/online/fxreader/pr34/commands_typed/crypto.py @@ -1,14 +1,31 @@ -from typing import (Literal,) +import base64 +import os + +from typing import (Literal, overload,) class PasswordUtils: + @overload @classmethod def encrypt( cls, - key: str, - mode: Literal['bytes', 'base64'], - ) -> tuple[str, str]: - import os + secret: str, + mode: Literal['base64'], + ) -> tuple[str, str]: ... + @overload + @classmethod + def encrypt( + cls, + secret: str, + mode: Literal['bytes'], + ) -> tuple[bytes, bytes]: ... + + @classmethod + def encrypt( + cls, + secret: str, + mode: Literal['bytes', 'base64'], + ) -> tuple[str, str] | tuple[bytes, bytes]: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt salt = os.urandom(16) @@ -22,15 +39,18 @@ class PasswordUtils: p=1, ) - key = kdf.derive(key.encode('utf-8')) + hashed_secret = kdf.derive(secret.encode('utf-8')) if mode == 'bytes': - return (salt, key) + return (salt, hashed_secret) elif mode == 'base64': - return ':'.join([ + res_tuple = tuple(( base64.b64encode(o, width=0).decode('utf-8') - for o in [salt, key,] - ]) + for o in (salt, hashed_secret,) + )) + return (res_tuple[0], res_tuple[1]) + else: + raise NotImplementedError # # verify # kdf = Scrypt( diff --git a/python/pyproject.toml b/python/pyproject.toml index 53430da..e4b9367 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -11,12 +11,12 @@ dependencies = [ ] [project.optional-dependencies] -early = [ - 'numpy', +crypto = [ 'cryptography', ] -crypto = [ +early = [ + 'numpy', 'cryptography', ]