Code cleanup, refactoring and bug fixes:

- Fixed first time running warnings about missing packages.json / desc files
- Fixed bug preventing Select All from being able to be clicked
- Fixed bug in select all and dependent checkboxes / race condition
- Eliminated vertical space gap between page header and table
This commit is contained in:
Eric Schultz
2016-01-24 11:25:23 -06:00
parent 534c5775b8
commit 7576dc3f31
7 changed files with 254 additions and 307 deletions

View File

@ -18,8 +18,8 @@ Usage: packagemanager [options]
EOF;
$shortopts = "drquv";
$longopts = array(
$shortopts = "drquv";
$longopts = [
"delete",
"download",
"help",
@ -27,17 +27,17 @@ $longopts = array(
"uninstall",
"verbose",
"version"
);
];
$args = getopt($shortopts, $longopts);
if (array_key_exists("help", $args)) {
echo $usage;
exit(1);
echo $usage;
exit(1);
}
if (array_key_exists("version", $args)) {
echo "Package Manager Version: 1.2\n";
exit(1);
echo "Package Manager Version: 1.2\n";
exit(1);
}
$argd = (array_key_exists("d", $args) || array_key_exists("download", $args));
@ -46,89 +46,63 @@ $argr = (array_key_exists("r", $args) || array_key_exists("delete", $args));
$argu = (array_key_exists("u", $args) || array_key_exists("uninstall", $args));
$argv = (array_key_exists("v", $args) || array_key_exists("verbose", $args));
require_once("/usr/local/emhttp/plugins/NerdPack/include/DownloadHelpers.php");
require_once '/usr/local/emhttp/plugins/NerdPack/include/NerdPackHelpers.php';
require_once '/usr/local/emhttp/plugins/NerdPack/include/DownloadHelpers.php';
function logger($output, $quiet) {
shell_exec( "echo '$output' 2>&1 | logger -tnerdpack");
if (!$quiet)
echo "\n".$output." \n";
usleep(100000);
}
$plg_path ="/boot/config/plugins/NerdPack";// plugin path
$pkg_path = "$plg_path/packages/"; // package path
$config_file = "$plg_path/NerdPack.cfg";// config file
$pkg_cfg = (is_file($config_file)) ? parse_ini_file($config_file) : array(); // get package configs
$pkgs_downloaded = array_diff( scandir($pkg_path, 1), array(".", "..") ); // get array of packages
$pkgs_installed = array_diff( scandir("/var/log/packages", 1), array(".", "..") );
$pkgs_json = $pkg_path."packages.json";
$pkgs_file = (is_file($pkgs_json)) ? file_get_contents($pkgs_json) : array();
$pkgs_github = json_decode($pkgs_file, true);
logger("Processing Packages...", 0);
foreach($pkg_cfg as $pkg_name => $pkg_pref) { //get preferences for each package
logger('Processing Packages...');
foreach ($pkg_cfg as $pkg_name => $pkg_pref) { // get preferences for each package
$pkg_cmd = '';
$pkg_name = str_replace("_", ".", $pkg_name); // replace "_" with "." in package names
$pkg_pattern = "/^".$pkg_name.".*/"; // search patter for packages
$pkg_name = str_replace('_', '.', $pkg_name); // replace "_" with "." in package names
$pkg_pattern = '/^'.$pkg_name.'.*/'; // search patter for packages
$pkg_install_status = preg_grep($pkg_pattern, $pkgs_installed); // check install status
if($pkg_pref == "yes"){
if ($pkg_pref == 'yes') {
// if executing from the wegui check status and download if necessary
if ($argd) {
if (!preg_grep($pkg_pattern, $pkgs_downloaded)) {
//if executing from the wegui check status and download if necessary
if ($argd){
$pkg_online_status = preg_grep($pkg_pattern, $pkgs_github_array);
if(!preg_grep($pkg_pattern, $pkgs_downloaded)) {
$pkg_online_status = preg_grep($pkg_pattern , $pkgs_github);
$pkg_matches = array_filter($pkgs_github, function($a) use($pkg_pattern ) {
return preg_grep($pkg_pattern , $a);
$pkg_matches = array_filter($pkgs_github_array, function($a) use ($pkg_pattern) {
return preg_grep($pkg_pattern, $a);
});
$pkg_gitname = array_values($pkg_matches)[0]["name"];
$pkg_file = $pkg_path.$pkg_gitname;
$pkg_url = array_values($pkg_matches)[0]["download_url"];
$pkg_sha1 = array_values($pkg_matches)[0]["sha"];
logger("Downloading $pkg_gitname package...", $argq);
$pkg_gitname = array_values($pkg_matches)[0]['name'];
$pkg_file = $pkg_path.$pkg_gitname;
$pkg_url = array_values($pkg_matches)[0]['download_url'];
$pkg_sha1 = array_values($pkg_matches)[0]['sha'];
logger('Downloading '.$pkg_gitname.' package...', $argq);
get_file_from_url($pkg_file, $pkg_url);
if(file_check_sha1($pkg_file, $pkg_sha1))
logger("$pkg_gitname package downloaded sucessfully!", $argq);
if (file_check_sha1($pkg_file, $pkg_sha1))
logger($pkg_gitname.' package downloaded sucessfully!', $argq);
else
logger("$pkg_gitname package download failed!", $argq);
logger($pkg_gitname.' package download failed!', $argq);
}
}
if (!$pkg_install_status){
$pkg_msg = "Installing";
$pkg_cmd = "upgradepkg --install-new ".$pkg_path.$pkg_name."* 2>&1";
if (!$pkg_install_status) {
$pkg_msg = 'Installing';
$pkg_cmd = 'upgradepkg --install-new '.$pkg_path.$pkg_name.'* 2>&1';
}
}elseif($pkg_pref == "no" && $pkg_install_status && $argu){
$pkg_msg = "Uninstalling";
$pkg_cmd = "removepkg ".$pkg_path.$pkg_name."* 2>&1";
if ($argr){
$pkg_cmd .= "; rm ".$pkg_path.$pkg_name."* 2>&1";
$pkg_msg .= " and deleting";
} else if ($pkg_pref == 'no' && $pkg_install_status && $argu) {
$pkg_msg = 'Uninstalling';
$pkg_cmd = 'removepkg '.$pkg_path.$pkg_name.'* 2>&1';
if ($argr) {
$pkg_cmd .= '; rm '.$pkg_path.$pkg_name.'* 2>&1';
$pkg_msg .= ' and deleting';
}
}
if(!empty($pkg_cmd)){
logger("$pkg_msg $pkg_name package...", 0);
if (!empty($pkg_cmd)) {
logger($pkg_msg.' '.$pkg_name.' package...');
shell_exec($pkg_cmd);
//$output = shell_exec($pkg_cmd);
//logger($output);
}else
logger($pkg_name." package up to date", $argq);
} else
logger($pkg_name.' package up to date', $argq);
}
logger("All packages processed", 0);
logger('All packages processed');
?>