From 9a8772da8f2f146594bbf0bdee0336120b1619e4 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Tue, 15 Jun 2021 17:09:49 -0300 Subject: [PATCH] improvement: replacing subprocess commands to detected installed CLIs by Python faster native calls (shutil.which) --- CHANGELOG.md | 3 +++ bauh/gems/appimage/controller.py | 5 ++--- bauh/gems/arch/controller.py | 5 ++--- bauh/gems/arch/git.py | 4 ++-- bauh/gems/arch/pacman.py | 10 ++++------ bauh/gems/arch/rebuild_detector.py | 3 ++- bauh/gems/arch/worker.py | 5 ++--- bauh/gems/snap/snap.py | 5 +++-- bauh/gems/web/nativefier.py | 4 ++-- bauh/gems/web/npm.py | 5 ++--- bauh/view/core/controller.py | 4 ++-- bauh/view/core/downloader.py | 7 ++++--- bauh/view/core/timeshift.py | 6 ++++-- bauh/view/qt/systray.py | 7 ++----- 14 files changed, 36 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf5e8f29..1b970f99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [0.9.17] +### Improvements +- general: replacing subprocess commands to detected installed CLIs by Python faster native calls (**shutil.which**) + ### Fixes - Arch - skipping the package version epoch when checking the updates requirements diff --git a/bauh/gems/appimage/controller.py b/bauh/gems/appimage/controller.py index c3f13a01..b789c6de 100644 --- a/bauh/gems/appimage/controller.py +++ b/bauh/gems/appimage/controller.py @@ -606,9 +606,8 @@ class AppImageManager(SoftwareManager): def set_enabled(self, enabled: bool): self.enabled = enabled - def _is_sqlite3_available(self): - res = run_cmd('which sqlite3') - return res and not res.strip().startswith('which ') + def _is_sqlite3_available(self) -> bool: + return bool(shutil.which('sqlite3')) def can_work(self) -> bool: return self._is_sqlite3_available() and self.file_downloader.can_work() diff --git a/bauh/gems/arch/controller.py b/bauh/gems/arch/controller.py index 9340d72d..04f0d8fd 100644 --- a/bauh/gems/arch/controller.py +++ b/bauh/gems/arch/controller.py @@ -2649,9 +2649,8 @@ class ArchManager(SoftwareManager): return res - def _is_wget_available(self): - res = run_cmd('which wget') - return res and not res.strip().startswith('which ') + def _is_wget_available(self) -> bool: + return bool(shutil.which('wget')) def is_enabled(self) -> bool: return self.enabled diff --git a/bauh/gems/arch/git.py b/bauh/gems/arch/git.py index 5ae7de0b..086e8112 100644 --- a/bauh/gems/arch/git.py +++ b/bauh/gems/arch/git.py @@ -1,3 +1,4 @@ +import shutil from datetime import datetime from typing import List, Tuple, Optional @@ -6,8 +7,7 @@ from bauh.commons.system import new_subprocess, SimpleProcess def is_installed() -> bool: - code, _ = system.execute(cmd='which git', output=False) - return code == 0 + return bool(shutil.which('git')) def list_commits(proj_dir: str) -> List[dict]: diff --git a/bauh/gems/arch/pacman.py b/bauh/gems/arch/pacman.py index 29c639e4..cd528dfa 100644 --- a/bauh/gems/arch/pacman.py +++ b/bauh/gems/arch/pacman.py @@ -1,6 +1,7 @@ import logging import os import re +import shutil from threading import Thread from typing import List, Set, Tuple, Dict, Iterable, Optional @@ -26,8 +27,7 @@ RE_DESKTOP_FILES = re.compile(r'\n?([\w\-_]+)\s+(/usr/share/.+\.desktop)') def is_available() -> bool: - res = run_cmd('which pacman', print_error=False) - return res and not res.strip().startswith('which ') + return bool(shutil.which('pacman')) def get_repositories(pkgs: Iterable[str]) -> dict: @@ -463,8 +463,7 @@ def get_databases() -> Set[str]: def can_refresh_mirrors() -> bool: - output = run_cmd('which pacman-mirrors', print_error=False) - return True if output else False + return is_mirrors_available() def refresh_mirrors(root_password: str) -> SimpleProcess: @@ -497,8 +496,7 @@ def get_current_mirror_countries() -> List[str]: def is_mirrors_available() -> bool: - code, _ = system.execute(cmd='which pacman-mirrors', output=False) - return code == 0 + return bool(shutil.which('pacman-mirrors')) def map_update_sizes(pkgs: List[str]) -> Dict[str, int]: # bytes: diff --git a/bauh/gems/arch/rebuild_detector.py b/bauh/gems/arch/rebuild_detector.py index 96d2cce8..23c45e60 100644 --- a/bauh/gems/arch/rebuild_detector.py +++ b/bauh/gems/arch/rebuild_detector.py @@ -1,4 +1,5 @@ import os +import shutil from pathlib import Path from typing import Set @@ -7,7 +8,7 @@ from bauh.gems.arch import IGNORED_REBUILD_CHECK_FILE def is_installed() -> bool: - return system.execute(cmd='which checkrebuild', output=False)[0] == 0 + return bool(shutil.which('checkrebuild')) def list_required_rebuild() -> Set[str]: diff --git a/bauh/gems/arch/worker.py b/bauh/gems/arch/worker.py index e91e5c16..841c71e2 100644 --- a/bauh/gems/arch/worker.py +++ b/bauh/gems/arch/worker.py @@ -2,6 +2,7 @@ import glob import logging import os import re +import shutil import time import traceback from datetime import datetime, timedelta @@ -13,7 +14,6 @@ import requests from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.handler import TaskManager -from bauh.commons import system from bauh.commons.boot import CreateConfigFile from bauh.commons.html import bold from bauh.commons.system import new_root_subprocess, ProcessHandler @@ -259,8 +259,7 @@ class ArchCompilationOptimizer(Thread): self.taskman.register_task(self.task_id, self.i18n['arch.task.optimizing'].format(bold('makepkg.conf')), get_icon_path()) def _is_ccache_installed(self) -> bool: - code, _ = system.execute(cmd='which ccache', output=False) - return code == 0 + return bool(shutil.which('ccache')) def optimize(self): ti = time.time() diff --git a/bauh/gems/snap/snap.py b/bauh/gems/snap/snap.py index 01fe60fe..d6e5cc5b 100644 --- a/bauh/gems/snap/snap.py +++ b/bauh/gems/snap/snap.py @@ -1,15 +1,16 @@ import os +import shutil import subprocess from io import StringIO from typing import Tuple, Optional -from bauh.commons.system import run_cmd, SimpleProcess +from bauh.commons.system import SimpleProcess BASE_CMD = 'snap' def is_installed() -> bool: - return bool(run_cmd('which {}'.format(BASE_CMD), print_error=False)) + return bool(shutil.which(BASE_CMD)) def uninstall_and_stream(app_name: str, root_password: str) -> SimpleProcess: diff --git a/bauh/gems/web/nativefier.py b/bauh/gems/web/nativefier.py index 8b50142a..3a807202 100644 --- a/bauh/gems/web/nativefier.py +++ b/bauh/gems/web/nativefier.py @@ -1,3 +1,4 @@ +import shutil from typing import List, Optional from bauh.commons.system import SimpleProcess, run_cmd @@ -18,8 +19,7 @@ def install(url: str, name: str, output_dir: str, electron_version: Optional[str def is_available() -> bool: - res = run_cmd('which nativefier', print_error=False) - return res and not res.strip().startswith('which ') + return bool(shutil.which('nativefier')) def get_version() -> str: diff --git a/bauh/gems/web/npm.py b/bauh/gems/web/npm.py index 9721b78b..28e437a6 100644 --- a/bauh/gems/web/npm.py +++ b/bauh/gems/web/npm.py @@ -1,6 +1,5 @@ -from bauh.commons.system import run_cmd +import shutil def is_available() -> bool: - res = run_cmd('which npm', print_error=False) - return res and not res.strip().startswith('which ') + return bool(shutil.which('npm')) diff --git a/bauh/view/core/controller.py b/bauh/view/core/controller.py index bc4b4753..ac0e2159 100755 --- a/bauh/view/core/controller.py +++ b/bauh/view/core/controller.py @@ -1,5 +1,6 @@ import re import re +import shutil import time import traceback from subprocess import Popen, STDOUT @@ -16,7 +17,6 @@ from bauh.api.abstract.view import ViewComponent, TabGroupComponent, MessageType from bauh.api.exception import NoInternetException from bauh.commons.boot import CreateConfigFile from bauh.commons.html import bold -from bauh.commons.system import run_cmd from bauh.view.core.config import CoreConfigManager from bauh.view.core.settings import GenericSettingsManager from bauh.view.core.update import check_for_update @@ -71,7 +71,7 @@ class GenericSoftwareManager(SoftwareManager): refresh=False): self.is_backups_action_available} def _is_timeshift_launcher_available(self) -> bool: - return bool(run_cmd('which timeshift-launcher', print_error=False)) + return bool(shutil.which('timeshift-launcher')) def is_backups_action_available(self, app_config: dict) -> bool: return bool(app_config['backup']['enabled']) and self._is_timeshift_launcher_available() diff --git a/bauh/view/core/downloader.py b/bauh/view/core/downloader.py index cede24fc..07df914d 100644 --- a/bauh/view/core/downloader.py +++ b/bauh/view/core/downloader.py @@ -1,6 +1,7 @@ import logging import os import re +import shutil import time import traceback from math import floor @@ -29,15 +30,15 @@ class AdaptableFileDownloader(FileDownloader): @staticmethod def is_aria2c_available() -> bool: - return bool(run_cmd('which aria2c', print_error=False)) + return bool(shutil.which('aria2c')) @staticmethod def is_axel_available() -> bool: - return bool(run_cmd('which axel', print_error=False)) + return bool(shutil.which('axel')) @staticmethod def is_wget_available() -> bool: - return bool(run_cmd('which wget', print_error=False)) + return bool(shutil.which('wget')) def _get_aria2c_process(self, url: str, output_path: str, cwd: str, root_password: str, threads: int) -> SimpleProcess: cmd = ['aria2c', url, diff --git a/bauh/view/core/timeshift.py b/bauh/view/core/timeshift.py index 2cb21648..2a808c5a 100644 --- a/bauh/view/core/timeshift.py +++ b/bauh/view/core/timeshift.py @@ -1,8 +1,10 @@ -from bauh.commons.system import run_cmd, SimpleProcess +import shutil + +from bauh.commons.system import SimpleProcess def is_available() -> bool: - return bool(run_cmd('which timeshift', print_error=False)) + return bool(shutil.which('timeshift')) def delete_all_snapshots(root_password: str) -> SimpleProcess: diff --git a/bauh/view/qt/systray.py b/bauh/view/qt/systray.py index 7d58e283..5014ee6c 100755 --- a/bauh/view/qt/systray.py +++ b/bauh/view/qt/systray.py @@ -1,6 +1,7 @@ import json import logging import os +import shutil import sys import traceback from io import StringIO @@ -15,7 +16,6 @@ from PyQt5.QtWidgets import QSystemTrayIcon, QMenu from bauh import __app_name__, ROOT_DIR from bauh.api.abstract.model import PackageUpdate from bauh.api.http import HttpClient -from bauh.commons import system from bauh.commons.system import run_cmd from bauh.context import generate_i18n from bauh.view.core.tray_client import TRAY_CHECK_FILE @@ -40,11 +40,8 @@ def get_cli_path() -> str: if os.path.exists(cli_path): return cli_path else: - cli_path = system.run_cmd('which bauh-cli', print_error=False) + return shutil.which('bauh-cli') - if cli_path: - return cli_path.strip() - def list_updates(logger: logging.Logger) -> List[PackageUpdate]: cli_path = get_cli_path()