aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside-tools/deploy/python_helper.py
blob: 3831b7dde1bc21b1ddadd9a4101bec064a80b5df (plain)
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
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

import sys
import os
import logging
from importlib import util
from pathlib import Path

from . import Nuitka, run_command, Config


class PythonExecutable:
    """
    Wrapper class around Python executable
    """

    def __init__(self, python_path=None, create_venv=False, dry_run=False):
        self.exe = python_path if python_path else Path(sys.executable)
        self.dry_run = dry_run
        if create_venv:
            self.__create_venv()
        self.nuitka = Nuitka(nuitka=[self.exe, "-m", "nuitka"])

    @property
    def exe(self):
        return Path(self._exe)

    @exe.setter
    def exe(self, exe):
        self._exe = exe

    @staticmethod
    def is_venv():
        venv = os.environ.get("VIRTUAL_ENV")
        return True if venv else False

    def __create_venv(self):
        self.install("virtualenv")
        if not self.is_venv():
            run_command(
                command=[self.exe, "-m", "venv", Path.cwd() / "deployment" / "venv"],
                dry_run=self.dry_run,
            )
            venv_path = Path(os.environ["VIRTUAL_ENV"])
            if sys.platform == "win32":
                self.exe = venv_path / "Scripts" / "python.exe"
            elif sys.platform in ["linux", "darwin"]:
                self.exe = venv_path / "bin" / "python"
        else:
            logging.info("[DEPLOY]: You are already in virtual environment!")

    def install(self, packages: list = None):
        if packages:
            for package in packages:
                if not self.is_installed(package=package):
                    logging.info(f"[DEPLOY]: Installing package: {package}")
                    run_command(
                        command=[self.exe, "-m", "pip", "install", package],
                        dry_run=self.dry_run,
                    )
                else:
                    logging.info(f"[DEPLOY]: Upgrading package: {package}")
                    run_command(
                        command=[self.exe, "-m", "pip", "install", "--upgrade", package],
                        dry_run=self.dry_run,
                    )

    def is_installed(self, package):
        return bool(util.find_spec(package))

    def create_executable(self, source_file: Path, extra_args: str, config: Config):
        if config.qml_files:
            logging.info(f"[DEPLOY]: Included QML files: {config.qml_files}")

        self.nuitka.create_executable(
            source_file=source_file,
            extra_args=extra_args,
            qml_files=config.qml_files,
            dry_run=self.dry_run,
        )