From 6ecb45420f50aa2e0aa34df55cc46a2e323e38ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Wed, 11 Dec 2019 19:35:20 -0300 Subject: [PATCH] [wgem] installed size on the info window --- bauh/api/http.py | 10 ++-------- bauh/commons/system.py | 24 ++++++++++++++++++++++++ bauh/gems/web/controller.py | 6 +++++- bauh/gems/web/resources/locale/en | 3 ++- bauh/gems/web/resources/locale/pt | 3 ++- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/bauh/api/http.py b/bauh/api/http.py index 4d447d17..8a7f8f77 100644 --- a/bauh/api/http.py +++ b/bauh/api/http.py @@ -4,7 +4,7 @@ import traceback import requests -SIZE_MULTIPLIERS = ((0.001, 'Kb'), (0.000001, 'Mb'), (0.000000001, 'Gb'), (0.000000000001, 'Tb')) +from bauh.commons import system class HttpClient: @@ -64,10 +64,4 @@ class HttpClient: size = res.headers.get('Content-Length') if size is not None: - size = int(size) - for m in SIZE_MULTIPLIERS: - size_str = str(size * m[0]) - - if len(size_str.split('.')[0]) < 4: - return '{0:.2f}'.format(float(size_str)) + ' ' + m[1] - return str(size) + return system.get_human_size_str(size) diff --git a/bauh/commons/system.py b/bauh/commons/system.py index 6a555fbb..6837bb93 100644 --- a/bauh/commons/system.py +++ b/bauh/commons/system.py @@ -22,6 +22,8 @@ if GLOBAL_PY_LIBS not in PATH: USE_GLOBAL_INTERPRETER = bool(os.getenv('VIRTUAL_ENV')) +SIZE_MULTIPLIERS = ((0.001, 'Kb'), (0.000001, 'Mb'), (0.000000001, 'Gb'), (0.000000000001, 'Tb')) + def gen_env(global_interpreter: bool, lang: str = DEFAULT_LANG) -> dict: res = {} @@ -219,3 +221,25 @@ def new_root_subprocess(cmd: List[str], root_password: str, cwd: str = '.', def notify_user(msg: str, app_name: str, icon_path: str): os.system("notify-send -a {} {} '{}'".format(app_name, "-i {}".format(icon_path) if icon_path else '', msg)) + + +def get_dir_size(start_path='.'): + total_size = 0 + for dirpath, dirnames, filenames in os.walk(start_path): + for f in filenames: + fp = os.path.join(dirpath, f) + + if not os.path.islink(fp): + total_size += os.path.getsize(fp) + + return total_size + + +def get_human_size_str(size) -> str: + int_size = int(size) + for m in SIZE_MULTIPLIERS: + size_str = str(int_size * m[0]) + + if len(size_str.split('.')[0]) < 4: + return '{0:.2f}'.format(float(size_str)) + ' ' + m[1] + return str(int_size) diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index e85d932c..4dfedd90 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -16,7 +16,7 @@ from bauh.api.abstract.handler import ProcessWatcher from bauh.api.abstract.model import SoftwarePackage, PackageAction, PackageSuggestion, PackageUpdate, PackageHistory from bauh.api.abstract.view import MessageType from bauh.commons.html import bold -from bauh.commons.system import ProcessHandler +from bauh.commons.system import ProcessHandler, get_dir_size, get_human_size_str from bauh.gems.web import INSTALLED_PATH, nativefier, DESKTOP_ENTRY_PATH_PATTERN from bauh.gems.web.environment import EnvironmentUpdater from bauh.gems.web.model import WebApplication @@ -150,6 +150,10 @@ class WebApplicationManager(SoftwareManager): info = {'{}_{}'.format(idx + 1, att): getattr(pkg, att) for idx, att in enumerate(('url', 'description', 'version', 'installation_dir', 'desktop_entry'))} info['6_exec_file'] = pkg.get_exec_path() info['7_icon_path'] = pkg.get_disk_icon_path() + + if os.path.exists(pkg.installation_dir): + info['8_size'] = get_human_size_str(get_dir_size(pkg.installation_dir)) + return info def get_history(self, pkg: SoftwarePackage) -> PackageHistory: diff --git a/bauh/gems/web/resources/locale/en b/bauh/gems/web/resources/locale/en index d653e71b..1ec597b5 100644 --- a/bauh/gems/web/resources/locale/en +++ b/bauh/gems/web/resources/locale/en @@ -16,4 +16,5 @@ web.info.3_version=version web.info.4_installation_dir=installation dir web.info.5_desktop_entry=shortcut web.info.6_exec_file=executable -web.info.7_icon_path=icon \ No newline at end of file +web.info.7_icon_path=icon +web.info.8_size=size \ No newline at end of file diff --git a/bauh/gems/web/resources/locale/pt b/bauh/gems/web/resources/locale/pt index a00f96d9..d8b07e32 100644 --- a/bauh/gems/web/resources/locale/pt +++ b/bauh/gems/web/resources/locale/pt @@ -16,4 +16,5 @@ web.info.3_version=versão web.info.4_installation_dir=diretório de instalação web.info.5_desktop_entry=atalho web.info.6_exec_file=executável -web.info.7_icon_path=ícone \ No newline at end of file +web.info.7_icon_path=ícone +web.info.8_size=tamanho \ No newline at end of file