[+] 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/launch.json
python/build
.*.kate-swp

77
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,
])

@ -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:

@ -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(

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