[+] update .p43

1. partially improve crypto;
  2. fix m.py;
This commit is contained in:
Siarhei Siniak 2025-03-03 12:53:58 +03:00
parent 1fb4e4efc5
commit 62063a1448
5 changed files with 96 additions and 30 deletions

1
.gitignore vendored

@ -13,3 +13,4 @@ d2/book1/books
.vscode/* .vscode/*
!.vscode/launch.json !.vscode/launch.json
python/build python/build
.*.kate-swp

73
m.py

@ -17,6 +17,9 @@ logger = logging.getLogger(__name__)
class PyProject: class PyProject:
dependencies: dict[str, list[str]] dependencies: dict[str, list[str]]
early_features: Optional[list[str]] = None 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( def pyproject_load(
d: pathlib.Path, d: pathlib.Path,
@ -65,6 +68,26 @@ def pyproject_load(
if 'early_features' in content['tool'][tool_name]: if 'early_features' in content['tool'][tool_name]:
res.early_features = content['tool'][tool_name]['early_features'] 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 return res
@dataclasses.dataclass @dataclasses.dataclass
@ -92,8 +115,21 @@ def env_bootstrap(
bootstrap_settings: BootstrapSettings, bootstrap_settings: BootstrapSettings,
pyproject: PyProject, pyproject: PyProject,
) -> None: ) -> 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([ subprocess.check_call([
'uv', 'venv', '--seed', 'uv', 'venv',
*pip_find_links_args,
# '--seed',
'--offline',
str(bootstrap_settings.env_path) str(bootstrap_settings.env_path)
]) ])
@ -101,31 +137,36 @@ def env_bootstrap(
'uv', 'uv',
'pip', 'pip',
'install', 'install',
*pip_find_links_args,
'-p', '-p',
bootstrap_settings.python_path, bootstrap_settings.python_path,
'uv', '--offline',
'uv', 'pip',
]) ])
subprocess.check_call([ subprocess.check_call([
bootstrap_settings.python_path, bootstrap_settings.python_path,
'-m', '-m',
'uv', 'pip', 'install', 'uv', 'pip', 'install',
*pip_find_links_args,
'--offline',
'build', 'setuptools', 'meson-python', 'pybind11', 'build', 'setuptools', 'meson-python', 'pybind11',
]) ])
early_wheels = glob.glob( # early_wheels = glob.glob(
str( # str(
pathlib.Path(__file__).parent / 'deps' / 'dist' / 'early' / '*.whl' # pathlib.Path(__file__).parent / 'deps' / 'dist' / 'early' / '*.whl'
) # )
) # )
if len(early_wheels) > 0: # if len(early_wheels) > 0:
subprocess.check_call([ # subprocess.check_call([
bootstrap_settings.python_path, # bootstrap_settings.python_path,
'-m', # '-m',
'uv', 'pip', 'install', # 'uv', 'pip', 'install',
*early_wheels, # '--offline',
]) # *early_wheels,
# ])
if pyproject.early_features: if pyproject.early_features:
early_dependencies = sum([ early_dependencies = sum([
@ -136,11 +177,15 @@ def env_bootstrap(
logger.info(dict( logger.info(dict(
early_dependencies=early_dependencies, early_dependencies=early_dependencies,
)) ))
if len(early_dependencies) > 0: if len(early_dependencies) > 0:
subprocess.check_call([ subprocess.check_call([
bootstrap_settings.python_path, bootstrap_settings.python_path,
'-m', '-m',
'uv', 'pip', 'install', 'uv', 'pip', 'install',
*pip_find_links_args,
# '-f', str(pathlib.Path(__file__).parent / 'deps' / 'dist'),
'--offline',
*early_dependencies, *early_dependencies,
]) ])

@ -1,14 +1,31 @@
from typing import (Literal,) import base64
import os
from typing import (Literal, overload,)
class PasswordUtils: class PasswordUtils:
@overload
@classmethod @classmethod
def encrypt( def encrypt(
cls, cls,
key: str, secret: str,
mode: Literal['bytes', 'base64'], mode: Literal['base64'],
) -> tuple[str, str]: ) -> tuple[str, str]: ...
import os
@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 from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
salt = os.urandom(16) salt = os.urandom(16)
@ -22,15 +39,18 @@ class PasswordUtils:
p=1, p=1,
) )
key = kdf.derive(key.encode('utf-8')) hashed_secret = kdf.derive(secret.encode('utf-8'))
if mode == 'bytes': if mode == 'bytes':
return (salt, key) return (salt, hashed_secret)
elif mode == 'base64': elif mode == 'base64':
return ':'.join([ res_tuple = tuple((
base64.b64encode(o, width=0).decode('utf-8') 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 # # verify
# kdf = Scrypt( # kdf = Scrypt(

@ -11,12 +11,12 @@ dependencies = [
] ]
[project.optional-dependencies] [project.optional-dependencies]
early = [ crypto = [
'numpy',
'cryptography', 'cryptography',
] ]
crypto = [ early = [
'numpy',
'cryptography', 'cryptography',
] ]