File: checkpackages.py

package info (click to toggle)
kdenlive 25.12.0-1
  • links: PTS
  • area: main
  • in suites: forky
  • size: 125,912 kB
  • sloc: cpp: 206,648; xml: 11,857; python: 1,139; ansic: 1,054; javascript: 578; sh: 389; makefile: 15
file content (124 lines) | stat: -rwxr-xr-x 4,256 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Jean-Baptiste Mardelle <jb@kdenlive.org>
# SPDX-FileCopyrightText: 2022 Julius Künzel <julius.kuenzel@kde.org>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

import sys
import os
import subprocess
import importlib.metadata
import importlib.util
from pathlib import Path


def print_help():
    print("""
    THIS SCRIPT IS PART OF KDENLIVE (www.kdenlive.org)

    Usage: python3 checkpackages.py [mode] [packages]

    Where [packages] is a list of python package names separated by blank space

    And [mode] one of the following:

    --help     print this help
    --install  install missing packages
    --upgrade  upgrade the packages
    --details  show details about the packages like eg. version
    --check    show which of the given packages are not yet installed
    """)


if '--help' in sys.argv:
    print_help()
    sys.exit()

required = set()
missing = set()

for arg in sys.argv[1:]:
    if not arg.startswith("--"):
        if arg.endswith(".txt"):
            required.add(arg)
        else:
            required.add(arg.lower())

if len(required) == 0:
    print_help()
    sys.exit("Error: You need to provide at least one package name")

installed = {pkg.metadata['Name'] for pkg in importlib.metadata.distributions()}
normalizedInstalled = set()
for i in installed:
    if i is None:
        continue
    normalizedInstalled.add(i.lower())

missing = required - normalizedInstalled

if '--check' in sys.argv:
    for m in missing:
        print("Missing: ", m)
elif '--install' in sys.argv and len(sys.argv) > 1:
    # install missing modules
    python = sys.executable
    if len(missing) > 0:
        print("Installing missing packages: ", missing)
        tmpFolder = os.path.join(Path.home(), ".cache/pip-kdenlive-tmp-folder")
        print("Using tmp folder: ", tmpFolder)
        os.makedirs(tmpFolder, exist_ok=True)
        my_env = os.environ.copy()
        my_env["TMPDIR"] = tmpFolder
        for m in missing:
            try:
                if m.endswith(".txt"):
                    subprocess.check_call([python, '-m', 'pip', 'install', '-r', m, '--no-cache-dir'], env=my_env)
                else:
                    subprocess.check_call([python, '-m', 'pip', 'install', m, '--no-cache-dir'], env=my_env)
            except:
                print("failed installing ", m)
elif '--force-install' in sys.argv and len(sys.argv) > 1:
    # install missing modules
    python = sys.executable
    if len(missing) > 0:
        print("Installing missing packages: ", missing)
        tmpFolder = os.path.join(Path.home(), ".cache/pip-kdenlive-tmp-folder")
        print("Using tmp folder: ", tmpFolder)
        os.makedirs(tmpFolder, exist_ok=True)
        my_env = os.environ.copy()
        my_env["TMPDIR"] = tmpFolder
        for m in missing:
            try:
                if m.endswith(".txt"):
                    subprocess.check_call([python, '-m', 'pip', 'install', '--force-reinstall', '-r', m, '--no-cache-dir'], env=my_env)
                else:
                    subprocess.check_call([python, '-m', 'pip', 'install', '--force-reinstall', m, '--no-cache-dir'], env=my_env)
            except:
                print("failed installing ", m)
elif '--upgrade' in sys.argv:
    # update modules
    # print("Updating packages: ", required)
    python = sys.executable
    upgradable = normalizedInstalled - required
    for u in upgradable:
        try:
            subprocess.check_call([python, '-m', 'pip', 'install', '--upgrade', u])
        except:
            print("failed upgrading ", u)
    for r in required:
        try:
            if r.endswith(".txt"):
                subprocess.check_call([python, '-m', 'pip', 'install', '--upgrade', '-r', r])
            else:
                subprocess.check_call([python, '-m', 'pip', 'install', '--upgrade', r])
        except:
            print("failed installing ", r)
elif '--details' in sys.argv:
    # check modules version
    python = sys.executable
    for m in missing:
        print(m, "==missing", file=sys.stdout,flush=True)
    subprocess.check_call([python, '-m', 'pip', 'freeze'])
else:
    print_help()
    sys.exit("Error: You need to provide a mode")