eric7/UI/upgrader.py

Sun, 06 Mar 2022 19:53:19 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 06 Mar 2022 19:53:19 +0100
branch
eric7
changeset 8972
54816b8f740f
parent 8970
188ff9ce8657
child 8994
3bebd76dc6ea
permissions
-rw-r--r--

General
- added capability to upgrade eric from within eric

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Script to upgrade the packages eric depends on.

This process must be performed while eric is closed. The script will upgrade
the requested packages and will restart eric.
"""

import subprocess
import sys
import time


_pyqtPackages = [
    "pyqt6", "pyqt6-sip", "pyqt6-webengine", "pyqt6-charts",
    "pyqt6-qscintilla", "pyqt6-qt6", "pyqt6-webengine-qt6",
    "pyqt6-charts-qt6"
]
_ericPackages = ["eric-ide"]


def doUpgrade(packages):
    """
    Function to upgrade the given packages via pip.
    
    @param packages list of packages to be upgraded
    @type list of str
    @return flag indicating a successful installation
    @rtype bool
    """
    exitCode = subprocess.run(                  # secok
        [sys.executable, "-m", "pip", "install", "--prefer-binary",
         "--upgrade"] + packages
    ).returncode
    ok = (exitCode == 0)
    
    return ok


def startEric(args):
    """
    Function to start eric with the given arguments.
    
    @param args list containing the start arguments
    @type list of str
    """
    args = [sys.executable] + args
    subprocess.Popen(args)


def main():
    """
    Main entry point into the upgrader.
    """
    # wait a few seconds to give eric the chance to fully shut down
    time.sleep(2)
    
    try:
        ddindex = sys.argv.index("--")
    except ValueError:
        # '--' was not found. Start eric with all parameters given.
        ddindex = 1
    
    ericStartArgs = sys.argv[ddindex + 1:]
    if not ericStartArgs:
        # create default start arguments
        ericStartArgs = ["-m", "eric7", "--start-session"]
    
    upgraderArgs = sys.argv[1:ddindex]
    
    # now perform the upgrade and start eric, if it was successful
    if upgraderArgs[0] == "--pyqt":
        ok = doUpgrade(_pyqtPackages)
    elif upgraderArgs[0] == "--eric":
        ok = doUpgrade(_ericPackages)
    elif upgraderArgs[0] == "--ericpyqt":
        ok = doUpgrade(_ericPackages + _pyqtPackages)
    else:
        ok = False
    
    if ok:
        startEric(ericStartArgs)
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == "__main__":
    main()

eric ide

mercurial