76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
from __future__ import annotations
|
|
from typing import cast
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import Any, List, Dict
|
|
import easymacro as app
|
|
from install_pip_from_wheel import InstallPipFromWheel
|
|
|
|
|
|
class FlatpakInstaller:
|
|
"""class for the PIP install."""
|
|
|
|
def __init__(self, pip_wheel_url: str, lo_identifier: str) -> None:
|
|
self.path_python = app.Paths.python
|
|
app.debug(f"Python path: {self.path_python}")
|
|
self._pip_url = pip_wheel_url
|
|
self._lo_identifier = lo_identifier
|
|
self._site_packages = cast(str, app.get_flatpak_site_packages_dir())
|
|
|
|
def install_pip(self) -> None:
|
|
if self.is_pip_installed():
|
|
app.info("PIP is already installed")
|
|
return
|
|
if self._install_wheel():
|
|
if self.is_pip_installed():
|
|
app.info("PIP was installed successfully")
|
|
else:
|
|
app.error("PIP installation has failed")
|
|
|
|
return
|
|
|
|
def _get_pip_cmd(self, filename: Path) -> List[str]:
|
|
return [str(self.path_python), f"{filename}", "--user"]
|
|
|
|
def _get_env(self) -> Dict[str, str]:
|
|
"""
|
|
Gets Environment used for subprocess.
|
|
"""
|
|
my_env = os.environ.copy()
|
|
py_path = ""
|
|
p_sep = ";" if app.IS_WIN else ":"
|
|
for d in sys.path:
|
|
py_path = py_path + d + p_sep
|
|
my_env["PYTHONPATH"] = py_path
|
|
return my_env
|
|
|
|
def _cmd_pip(self, *args: str) -> List[str]:
|
|
cmd: List[str] = [str(self.path_python), "-m", "pip", *args]
|
|
return cmd
|
|
|
|
def _install_wheel(self) -> bool:
|
|
result = False
|
|
|
|
installer = InstallPipFromWheel(pip_wheel_url=self._pip_url, lo_identifier=self._lo_identifier)
|
|
try:
|
|
installer.install(self._site_packages)
|
|
if self._site_packages not in sys.path:
|
|
sys.path.append(self._site_packages)
|
|
result = True
|
|
except Exception as err:
|
|
app.error(err)
|
|
return result
|
|
return result
|
|
|
|
|
|
def is_pip_installed(self) -> bool:
|
|
"""Check if PIP is installed."""
|
|
# cmd = self._cmd_pip("--version")
|
|
# cmd = '"{}" -m pip -V'.format(self.path_python)
|
|
cmd = [str(self.path_python), "-m", "pip", "-V"]
|
|
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self._get_env())
|
|
return result.returncode == 0
|
|
|