File size: 1,884 Bytes
206c6eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from importlib import invalidate_caches, metadata
from pathlib import Path
import subprocess
import sys


def main() -> None:
    if sys.version_info < (3, 11):
        raise RuntimeError('Для проекта нужен Python 3.11 или новее.')

    import tomllib

    root = Path(__file__).resolve().parents[1]
    config = tomllib.loads((root / 'pyproject.toml').read_text(encoding='utf-8'))
    try:
        metadata.version('packaging')
    except metadata.PackageNotFoundError:
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'packaging>=23'])
        invalidate_caches()
    from packaging.requirements import Requirement

    to_install = []
    to_update = []
    for specification in config['project']['dependencies']:
        requirement = Requirement(specification)
        if requirement.marker and not requirement.marker.evaluate():
            continue
        try:
            installed = metadata.version(requirement.name)
        except metadata.PackageNotFoundError:
            to_install.append(specification)
        else:
            if not requirement.specifier.contains(installed, prereleases=True):
                to_install.append(specification)
                to_update.append(requirement.name)

    if not to_install:
        print('Все зависимости уже установлены в подходящих версиях.')
        return

    print('Устанавливаем:', ', '.join(to_install), flush=True)
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', *to_install])
    print('Зависимости установлены.')
    if to_update:
        print('Обновлены:', ', '.join(to_update))
        print('Если эти библиотеки уже были импортированы, перезапустите ядро.')


if __name__ == '__main__':
    main()