1import logging
2import subprocess
3
4
5def install_pacman_package(pkg_name):
6    """ This installs a package in the current MSYS2 environment
7
8    Does not download again if the package is already installed
9    and if the version is the latest available in MSYS2
10    """
11    output = subprocess.run(['pacman', '-S', '--noconfirm', pkg_name],
12                            capture_output=True, text=True)
13    if output.returncode != 0:
14        logging.error(
15            "Error {} while downloading package {}: \n{}".
16            format(output.returncode, pkg_name, output.stderr))
17
18    return output.returncode != 0
19
20
21def get_packages(x86=True, x64=True):
22    deps = [
23        'mingw-w64-{}-SDL2',
24        'mingw-w64-{}-SDL2_ttf',
25        'mingw-w64-{}-SDL2_image',
26        'mingw-w64-{}-SDL2_mixer',
27        'mingw-w64-{}-portmidi',
28        'mingw-w64-{}-libpng',
29        'mingw-w64-{}-libjpeg-turbo',
30        'mingw-w64-{}-libtiff',
31        'mingw-w64-{}-zlib',
32        'mingw-w64-{}-libwebp',
33        'mingw-w64-{}-libvorbis',
34        'mingw-w64-{}-libogg',
35        'mingw-w64-{}-flac',
36        'mingw-w64-{}-libmodplug',
37        'mingw-w64-{}-mpg123',
38        'mingw-w64-{}-opus',
39        'mingw-w64-{}-opusfile',
40        'mingw-w64-{}-freetype'
41    ]
42
43    packages = []
44    if x86:
45        packages.extend([x.format('i686') for x in deps])
46    if x64:
47        packages.extend([x.format('x86_64') for x in deps])
48    return packages
49
50
51def install_prebuilts(x86=True, x64=True):
52    """ For installing prebuilt dependencies.
53    """
54    errors = False
55    print("Installing pre-built dependencies")
56    for pkg in get_packages(x86=x86, x64=x64):
57        print("Installing {}".format(pkg))
58        error = install_pacman_package(pkg)
59        errors = errors or error
60    if errors:
61        raise Exception("Some dependencies could not be installed")
62
63
64def update(x86=True, x64=True):
65    install_prebuilts(x86=x86, x64=x64)
66
67
68if __name__ == '__main__':
69    update()
70