Added option --fetch

Signed-off-by: Dimitris Zlatanidis <d.zlatanidis@gmail.com>
This commit is contained in:
Dimitris Zlatanidis 2024-06-11 20:45:28 +03:00
parent 31d6c0d52f
commit 6d614efc9c
5 changed files with 103 additions and 15 deletions

View file

@ -1,10 +1,10 @@
## slpkg - ChangeLog
### 5.1.1 - 09/06/2024
### 5.1.1 - 11/06/2024
- Added:
* Added an 'installed' tag for installed packages in the search command
* Added option --fetch to see the fastest and the slower mirror
### 5.1.0 - 06/06/2024

View file

@ -1,4 +1,4 @@
.TH slpkg 1 "Orestiada, Hellas" "slpkg 5.1.0" dslackw
.TH slpkg 1 "Orestiada, Hellas" "slpkg 5.1.1" dslackw
.SH NAME
.P
slpkg \- Package manager utility for Slackware.
@ -123,6 +123,14 @@ Skip the installed packages when you try to reinstall them.
Note: This option affects only the dependencies. (to be used with: -i, install)
.RE
.P
.B -F, --fetch
.RS
If the sources of your repository are not optimized for your location, it may take longer to download
packages than usual, leading to a poor user experience. It is recommended to fetch the fastest mirror
to see the score of the fastest and the slower mirror.
(to be used with: -I, repo-info)
.RE
.P
.B -E, --full-reverse
.RS
Full reverse dependency. Works only with -e, dependees command and show the requires too.

View file

@ -77,6 +77,8 @@ class Menu(Configs): # pylint: disable=[R0902]
self.flag_short_repository: str = '-o'
self.flag_directory: str = '--directory'
self.flag_short_directory: str = '-z'
self.flag_fetch: str = '--fetch'
self.flag_short_fetch: str = '-F'
self.flag_searches: tuple = (
self.flag_short_search,
@ -110,6 +112,8 @@ class Menu(Configs): # pylint: disable=[R0902]
self.flag_short_repository,
self.flag_directory,
self.flag_short_directory,
self.flag_fetch,
self.flag_short_fetch
)
self.commands: dict = {
@ -144,7 +148,9 @@ class Menu(Configs): # pylint: disable=[R0902]
],
'repo-info': [
self.flag_repository,
self.flag_short_repository
self.flag_short_repository,
self.flag_fetch,
self.flag_short_fetch
],
'configs': [],
'clean-tmp': [],

View file

@ -4,6 +4,8 @@
import shutil
from pathlib import Path
import time
import requests
from slpkg.configs import Configs
from slpkg.load_data import LoadData
@ -23,28 +25,78 @@ class RepoInfo(Configs): # pylint: disable=[R0902]
self.utils = Utilities()
self.repos = Repositories()
self.columns, self.rows = shutil.get_terminal_size()
self.name_alignment: int = self.columns - 61
self.name_alignment: int = self.columns - 61
self.name_alignment = max(self.name_alignment, 1)
self.mirror_alignment: int = self.columns - 32
self.mirror_alignment = max(self.mirror_alignment, 1)
self.enabled: int = 0
self.total_packages: int = 0
self.repo_data: dict = {}
self.dates: dict = {}
self.mirros_score: dict = {}
self.option_for_repository: bool = self.utils.is_option(
('-o', '--repository'), flags)
self.option_for_fetch: bool = self.utils.is_option(
('-F', '--fetch'), flags)
def info(self) -> None:
"""Print information about repositories."""
self.load_repo_data()
if self.option_for_fetch:
self.view_the_score_title()
self.view_the_title()
if self.option_for_repository:
self.view_the_repository_information()
if self.option_for_repository:
mirror: str = self.repos.repositories[self.repository]['mirror_changelog']
self.enabled += 1
self.check_mirror_speed(self.repository, mirror)
self.view_summary_of_repository()
else:
for repo, data in self.repos.repositories.items():
if data['enable']:
mirror: str = data['mirror_changelog']
self.enabled += 1
self.check_mirror_speed(repo, mirror)
self.view_summary_of_all_repositories()
else:
self.view_the_repositories_information()
self.load_repo_data()
self.view_the_title()
if self.option_for_repository:
self.view_the_repository_information()
else:
self.view_the_repositories_information()
def check_mirror_speed(self, repo: str, url: str) -> None:
"""Check mirrors speed.
Args:
repo: Name of the repository.
url: The repository mirror.
"""
try:
start_time: float = time.time() # Record the start time
response = requests.get(url) # pylint: disable=[W3101]
end_time: float = time.time() # Record the end time
url_length = 45 + (self.columns - 80)
if url_length < len(url):
url: str = f'{url[:url_length]}...'
if response.status_code == 200:
response_time = int((end_time - start_time) * 1000) # Convert to milliseconds
self.mirros_score[repo] = response_time
print(f"{self.cyan}{repo:<19}{self.endc}{url:<{self.mirror_alignment}}{self.yellow}"
f"{response_time:>9} ms{self.endc}")
else:
print(f"{self.red}{repo:<19}{self.endc}{url:<{self.mirror_alignment}}{self.red}"
f"{response.status_code:>12}{self.endc}")
except requests.RequestException as e:
print(f"{repo:<19}{url:<{self.mirror_alignment}}{e:>12}")
def load_repo_data(self) -> None:
"""Load repository data."""
@ -66,6 +118,14 @@ class RepoInfo(Configs): # pylint: disable=[R0902]
return self.utils.read_json_file(repo_info_json)
return {}
def view_the_score_title(self) -> None:
"""Print the title."""
title: str = 'Fetching mirrors, please wait...'
print(f'{title}\n')
print('=' * (self.columns - 1))
print(f"{'Name:':<19}{'Mirror:':<{self.mirror_alignment}}{'Score:':>12}")
print('=' * (self.columns - 1))
def view_the_title(self) -> None:
"""Print the title."""
title: str = 'repositories information:'.title()
@ -140,10 +200,22 @@ class RepoInfo(Configs): # pylint: disable=[R0902]
def view_summary_of_repository(self) -> None:
"""Print the repository summary."""
print('=' * (self.columns - 1))
print(f"{self.grey}Total {self.total_packages} packages available from the '{self.repository}' repository.\n")
if self.option_for_fetch:
print(f"{self.grey}Score {int(self.mirros_score[self.repository])} ms for repository "
f"'{self.repository}'.\n")
else:
print(f"{self.grey}Total {self.total_packages} packages available from the "
f"'{self.repository}' repository.\n")
def view_summary_of_all_repositories(self) -> None:
"""Print the total summary of repositories."""
print('=' * (self.columns - 1))
print(f"{self.grey}Total of {self.enabled}/{len(self.repos.repositories)} "
f"repositories are enabled with {self.total_packages} packages available.\n")
if self.option_for_fetch:
slower_mirror: str = max(self.mirros_score, key=lambda key: self.mirros_score[key])
fastest_mirror: str = min(self.mirros_score, key=lambda key: self.mirros_score[key])
print(f"{self.grey}Fastest mirror is '{fastest_mirror}' and "
f"slower mirror is '{slower_mirror}'.\n")
else:
print(f"{self.grey}Total of {self.enabled}/{len(self.repos.repositories)} "
f"repositories are enabled with {self.total_packages} packages available.\n")

View file

@ -49,7 +49,7 @@ class Usage(Configs):
f' slpkg [{self.cyan}COMMAND{self.endc}] [-d, download, -f, find, -w, view <packages>]\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [-s, search, -e, dependees, -t, tracking <packages>]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-y, --yes, -c, --check, -O, --resolve-off]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-r, --reinstall, -k, --skip-installed]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-r, --reinstall, -k, --skip-installed, -F, --fetch]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-E, --full-reverse, -S, --search, -B, --progress-bar]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-p, --pkg-version, -P, --parallel, -m, --no-case]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [-o, --repository=NAME, -z, --directory=PATH]\n'
@ -101,6 +101,8 @@ class Usage(Configs):
f' {self.yellow}-r, --reinstall{self.endc} Upgrade packages of the same version.\n'
f' {self.yellow}-k, --skip-installed{self.endc} Skip installed packages during the building\n'
f'{"":>28}or installation progress.\n'
f' {self.yellow}-F, --fetch{self.endc} Fetch the fastest and slower mirror.\n'
f'{"":>28}To be used with repo-info command.\n'
f' {self.yellow}-E, --full-reverse{self.endc} Display the full reverse dependency.\n'
f' {self.yellow}-S, --search{self.endc} Search and load packages using the dialog.\n'
f' {self.yellow}-B, --progress-bar{self.endc} Display static progress bar instead of\n'