File indexing completed on 2024-05-05 04:52:35

0001 #!/usr/bin/env python3
0002 # SPDX-FileCopyrightText: 2024 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 # SPDX-FileCopyrightText: 2022 Julius Künzel <jk.kdedev@smartlab.uber.space>
0004 # SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 import sys
0007 import subprocess
0008 import importlib.metadata
0009 
0010 def print_help():
0011     print("""
0012     THIS SCRIPT IS PART OF KDENLIVE (www.kdenlive.org)
0013 
0014     Usage: python3 checkpackages.py [mode] [packages]
0015 
0016     Where [packages] is a list of python package names separated by blank space
0017 
0018     And [mode] one of the following:
0019 
0020     --help     print this help
0021     --install  install missing packages
0022     --upgrade  upgrade the packages
0023     --details  show details about the packages like eg. version
0024     --check    show which of the given packages are not yet installed
0025     """)
0026 
0027 
0028 if '--help' in sys.argv:
0029     print_help()
0030     sys.exit()
0031 
0032 required = set()
0033 missing = set()
0034 
0035 for arg in sys.argv[1:]:
0036     if not arg.startswith("--"):
0037         required.add(arg)
0038 
0039 if len(required) == 0:
0040     print_help()
0041     sys.exit("Error: You need to provide at least one package name")
0042 
0043 installed = {pkg.name for pkg in importlib.metadata.distributions()}
0044 missing = required - installed
0045 
0046 if '--check' in sys.argv:
0047     if len(missing) > 0:
0048         print("Missing pachages: ", missing)
0049 elif '--install' in sys.argv and len(sys.argv) > 1:
0050     # install missing modules
0051     if len(missing) > 0:
0052         print("Installing missing packages: ", missing)
0053         python = sys.executable
0054         subprocess.check_call([python, '-m', 'pip', 'install', *missing])
0055 elif '--upgrade' in sys.argv:
0056     # update modules
0057     print("Updating packages: ", required)
0058     python = sys.executable
0059     subprocess.check_call([python, '-m', 'pip', 'install', '--upgrade', *required])
0060 elif '--details' in sys.argv:
0061     # check modules version
0062     python = sys.executable
0063     subprocess.check_call([python, '-m', 'pip', 'show', *required])
0064 else:
0065     print_help()
0066     sys.exit("Error: You need to provide a mode")