Cleaner command call

This commit is contained in:
zawz 2020-03-03 13:07:21 +01:00
parent e6d5596e05
commit 6ed5429323
6 changed files with 31 additions and 21 deletions

View file

@ -10,22 +10,16 @@
#define AUR_UPDATE_COMMAND "yay -Sau"
#define AUR_UPDATE_COMMAND_NOCONFIRM "yay -Sau --noconfirm"
#define PACMAN_EXT_INFO_COMMAND "pacman -Si --dbpath \"${TMPDIR:-/tmp}/checkup-db-${USER}/\" --logfile /dev/null "
#define PACMAN_LOCAL_INFO_COMMAND "pacman -Qi "
#define PACMAN_EXT_SIZE_CUT_COMMAND " |grep -E 'Download Size|Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec"
#define PACMAN_LOCAL_SIZE_CUT_COMMAND " |grep 'Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec|tr -d '\n'"
#define PACMAN_EXT_SIZE_COMMAND "pacman -Si --dbpath \"${TMPDIR:-/tmp}/checkup-db-${USER}/\" --logfile /dev/null %s |grep -E 'Download Size|Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec"
#define PACMAN_LOCAL_SIZE_COMMAND "pacman -Qi %s |grep 'Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec|tr -d '\n'"
// apt/dpkg
#define APT_FETCH_COMMAND "sudo apt update >/dev/null 2>&1 || return $?\napt list --upgradable 2>/dev/null | tail -n +2 | awk -F \"/\" '{print $1\" \"$2}' | tr -d ']' | awk '{print $1\" \"$7\" -> \"$3}'"
#define APT_UPDATE_COMMAND "sudo apt update && sudo apt upgrade"
#define APT_UPDATE_COMMAND_NOCONFIRM "sudo apt update && sudo apt -y upgrade"
#define APT_EXT_INFO_COMMAND "apt show "
#define APT_LOCAL_INFO_COMMAND "dpkg -s "
#define APT_EXT_SIZE_CUT_COMMAND " 2>/dev/null| grep -E 'Installed-Size:|Download-Size:' | cut -d' ' -f2- | tr -d ', B' | tr 'k' 'K' | numfmt --from=iec | awk '{s=$0;getline;s=$0\"\\n\"s;print s}'"
#define APT_LOCAL_SIZE_CUT_COMMAND " 2>/dev/null| grep 'Installed-Size:' | cut -d' ' -f2 | xargs echo '1024 *' | bc"
#define APT_EXT_SIZE_COMMAND "apt show %s 2>/dev/null| grep -E 'Installed-Size:|Download-Size:' | cut -d' ' -f2- | tr -d ', B' | tr 'k' 'K' | numfmt --from=iec | awk '{s=$0;getline;s=$0\"\\n\"s;print s}'"
#define APT_LOCAL_SIZE_COMMAND "dpkg -s %s 2>/dev/null| grep 'Installed-Size:' | cut -d' ' -f2 | xargs echo '1024 *' | bc"

View file

@ -6,6 +6,6 @@
//functions
int fetch_update(repo_update* r, const std::string& name, const std::string& command);
int import_sizes(repo_update* ru, const char* ext_info_command, const char* loc_info_command, const char* ext_cut_command, const char* loc_cut_command);
int import_sizes(repo_update* ru, const char* ext_size_command, const char* loc_size_command);
#endif //FETCH_HPP

View file

@ -12,6 +12,9 @@
extern const char* size_suffixes[6];
extern const int size_print_padding;
//tool
std::string strpf(std::string const& format, std::string const& var);
//functions
void help();

View file

@ -8,6 +8,7 @@
#include <ztd/shell.hpp>
#include "commands.h"
#include "print.hpp"
//functions
int fetch_update(repo_update* r, const std::string& name, const std::string& command)
@ -67,9 +68,9 @@ int fetch_update(repo_update* r, const std::string& name, const std::string& com
void get_ext_sizes(package_update* pkg, const char* info_command, const char* cut_command)
void get_ext_sizes(package_update* pkg, const char* command)
{
std::string sizes=ztd::sh(info_command + pkg->name + cut_command);
std::string sizes=ztd::sh(strpf(command, pkg->name));
unsigned int i=0, j=0;
while(sizes[i]!='\n')
i++;
@ -80,24 +81,24 @@ void get_ext_sizes(package_update* pkg, const char* info_command, const char* cu
i++;
pkg->new_install_size = std::stoul(sizes.substr(j,i-j));
}
void get_loc_size(package_update* pkg, const char* info_command, const char* cut_command)
void get_loc_size(package_update* pkg, const char* command)
{
std::string size=ztd::sh(info_command + pkg->name + cut_command);
std::string size=ztd::sh(strpf(command, pkg->name));
if(size.size() > 0)
pkg->current_install_size = std::stoul(size);
else
pkg->current_install_size = 0;
}
int import_sizes(repo_update* ru, const char* ext_info_command, const char* loc_info_command, const char* ext_cut_command, const char* loc_cut_command)
int import_sizes(repo_update* ru, const char* ext_size_command, const char* loc_size_command)
{
const unsigned int n=ru->packages.size();
#pragma omp parallel for
for(unsigned int i=0; i<n; i++) //parallel
{
package_update* pkg = &(ru->packages[i]);
get_ext_sizes(pkg, ext_info_command, ext_cut_command);
get_loc_size(pkg, loc_info_command, loc_cut_command);
get_ext_sizes(pkg, ext_size_command);
get_loc_size(pkg, loc_size_command);
pkg->net_size = (long int) pkg->new_install_size - (long int) pkg->current_install_size;
}
ru->download_size = 0;

View file

@ -71,7 +71,7 @@ int pacman_process(bool yay)
//size fetch
if( combine_size )
{
r = import_sizes(&repo, PACMAN_EXT_INFO_COMMAND, PACMAN_LOCAL_INFO_COMMAND, PACMAN_EXT_SIZE_CUT_COMMAND, PACMAN_LOCAL_SIZE_CUT_COMMAND);
r = import_sizes(&repo, PACMAN_EXT_SIZE_COMMAND, PACMAN_LOCAL_SIZE_COMMAND);
}
if(r!=0)
return r;
@ -164,7 +164,7 @@ int apt_process()
if( combine_size )
{
r = import_sizes(&repo, APT_EXT_INFO_COMMAND, APT_LOCAL_INFO_COMMAND, APT_EXT_SIZE_CUT_COMMAND, APT_LOCAL_SIZE_CUT_COMMAND);
r = import_sizes(&repo, APT_EXT_SIZE_COMMAND, APT_LOCAL_SIZE_COMMAND);
}
if(r!=0)
return r;

View file

@ -1,7 +1,7 @@
#include "print.hpp"
#include <iostream>
#include <cmath>
#include <math.h>
#include <ztd/shell.hpp>
@ -11,6 +11,18 @@
const char* size_suffixes[6] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB"};
const int size_print_padding=-21;
//tool
std::string strpf(std::string const& format, std::string const& var)
{
std::string ret;
size_t bufsize = format.size()-1 + var.size();
char* buf = (char*) malloc( sizeof(char)*bufsize );
snprintf(buf, bufsize, format.c_str(), var.c_str());
ret = buf;
free(buf);
return ret;
}
//functions
std::pair<double, const char*> convertN(const long int size, unsigned int n)
{