Author Topic: repair-database (My first zenity script)  (Read 8786 times)

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
repair-database (My first zenity script)
« on: January 11, 2011, 12:53:33 PM »
Thanks to: Neal for all the help and icon, Leiche for the help and the progress bar, and any one who worked on the getvirtualbox script it was very helpful.
I decided to make this script because I remember when I first used Linux I hated rebuilding the database.
The script rebuilds and cleans the repository database according to Texstars requirements: http://www.pclinuxos.com/forum/index.php/topic,80839.0.html

REQUIRES YAD Available in Synaptic
Below are all the resource to make this yourself.
repair-database (shell script) - /usr/bin
repair-database.desktop - /usr/share/applications
dbrepair.png - /usr/share/icons


yad version
Updated as of 09/05/2011, this will be the final version unless new advances are made with the cancel button. -Thank You
Code: [Select]
#!/bin/sh
#This is a script to rebuild repos in PClinuxOS.
#This script has been converted from zenity to yad.
#Special thanks to: Neal for all the help and icon, Leiche for the help and the progress bar, and any one who worked on the getvirtualbox script it was very helpful.
#By glamdring


# variables
#==========
WICON='/usr/share/icons/dbrepair.png' && TITLE="Repair Database"
XURD='/tmp/xsurepair-database'
RXURD="/bin/rm -f $XURD"
ZEN2="yad --window-icon=$WICON"
TITLE="Repair Database"
REMOVE="rm -rf /var/lib/rpm/__db*"
REBUILD="rpm --rebuilddb"
CLEANAPT="apt-get clean"
CLEANAPT2="apt-get autoclean"


#export
#======
export TEXTDOMAIN=repair-database
export TEXTDOMAINDIR="/usr/share/locale"


#clear xurd
#==========
#$RXURD


# Check if xterm can be used
#===========================
/bin/rm -f /tmp/checkxterm.txt && XOK=0
xterm -u8 -iconic -e touch /tmp/checkxterm.txt
if [ -e /tmp/checkxterm.txt ];then XOK="1";fi
/bin/rm -f /tmp/checkxterm.txt


# check if we run a pclos kernel
#===============================
if [ -z "`uname -r |grep pclos`" ]; then
  MSG="Repair Database requires a 'pclos'-kernel.\n\nExiting."
  $ZEN2 --form --text="$MSG" --button="gtk-ok:0"
  exit 0
fi


#login as super user
#===================
if [ ! -e $XURD ];then
  if [ -r /usr/bin/gksu ]; then LGI="/usr/bin/gksu"; fi
  if [ -r /usr/lib/kde4/libexec/kdesu ]; then LGI="/usr/lib/kde4/libexec/kdesu"; fi
  if [ -r /usr/bin/kdesu ]; then LGI="/usr/bin/kdesu"; fi
  # setup XURD
  echo "#"'!/bin/bash' > $XURD
  if [ "$UID" != "0" ]; then echo $LGI repair-database >> $XURD; else echo repair-database >> $XURD; fi
  chmod a+x $XURD
  $XURD &
  exit 0
fi


#test for super user privliges
#=============================
if [ "$UID" != "0" ]; then
  $ZEN2 --form --title="$TITLE" --notification --text=$" Please run this script as root. \n If this is the first time you have seen this please hit retry." \
  --button="Retry:1" --button="gtk-close:2" echo $?
  if [ "$?" == "1" ]; then
if [ -r /tmp/xsurepair-database ]; then $RXURD; fi
  "/usr/bin/repair-database";
  fi
exit 0
fi


# check internet connection
#==========================
cd /tmp
/bin/rm -f index.html inet.log
MSG="Testing your Internet connection."
( wget --timeout=15 http://www.google.com/index.html -o inet.log ) 2>&1 | $ZEN2 --title="$TITLE" --progress --text "$MSG" --pulsate --auto-close


if [ ! -f /tmp/index.html ]; then
  MSG="No Internet connection found.\n\nExiting."
  $ZEN2 --form --text "$MSG" --button="gtk-ok:1"
  $RXURD
  exit 1
fi


#repair database?
#================
MSG="Do you want to repair the Synaptic databse?"
MSG2="No changes were made to your system."
$ZEN2 --form --title="$TITLE" --text="$MSG" --button="gtk-yes:0" --button="gtk-no:1" --sticky --fixed ret=$?
if [ "$?" = "1" ]; then
  $ZEN2 --title="$TITLE" --form --notification --text="$MSG2" --button="gtk-close:0" --sticky --fixed --on-top
  $RXURD
  exit 0
fi


#remove corrupted database
#==========================
rm -rf /var/lib/rpm/__db*


#rebuild rpm database
#=====================
MSG="Rebuilding database, This can take some time please let it finish."
MSG2="You chose to exit. The database was not repaired!"
(echo && $REBUILD) | $ZEN2 --title="$TITLE" --text="$MSG" --progress --pulsate --auto-close --auto-kill --button="gtk-cancel:1" --sticky --fixed ret=$?
if [ "$?" = "1" ]; then
  $ZEN2 --title="$TITLE" --form --notification --text="$MSG2" --button="gtk-close:0" --sticky --fixed --on-top --center
  $RXURD
  exit 0
fi


#check for duplicate installed package
#=====================================
dupeclean


#clean out apt
#=============
$CLEANAPT
MSG="Cleaning database, please be patient."
MSG2="You chose to exit. The database was not cleaned!"
(echo && $CLEANAPT2) | $ZEN2 --progress --pulsate --auto-close --auto-close --title="$TITLE" --text="$MSG" --button="gtk-cancel:1" --sticky --fixed --center  ret=$?
if [ "$?" = "1" ]; then
  $ZEN2 --title="$TITLE" --form --notification --text="$MSG2" --button="gtk-close:0" --sticky --fixed --on-top --center
  $RXURD
  exit 0
fi


#exit
#====
MSG="Synaptic RPM database successfully repaired."
$ZEN2 --title="$TITLE" --form --notification --text="$MSG" --button="gtk-close:0" --sticky --fixed --on-top --center
$RXURD
exit 0


The icon:

The .desktop file
Code: [Select]
[Desktop Entry]
Version=0.1
Type=Application
Name=Repair Database
Exec=repair-database
Icon=dbrepair.png
Comment=Repair Database
Categories=RepairDatabase;System;X-MandrivaLinux-System-Configuration;X-MandrivaLinux-System-Configuration-Packaging;
Terminal=false
Encoding=UTF-8
StartupNotify=true


zenity version
Please be aware that this version is not updated and is considered obsolete.
Code: [Select]
#!/bin/sh
#This is a script to rebuild repos in PClinuxOS.

# variables
#==========
WICON='/usr/share/icons/dbrepair.png' && TITLE="Repair Database"
XURD='/tmp/xsurepair-database'
RXURD="/bin/rm -f $XURD"
ZEN2="zenity --window-icon=$WICON"
TITLE="Repair Database"
REMOVE="rm -rf /var/lib/rpm/__db*"
REBUILD="rpm --rebuilddb"
CLEANAPT="apt-get clean"
CLEANAPT2="apt-get autoclean"


#export
#======
export TEXTDOMAIN=repair-database
export TEXTDOMAINDIR="/usr/share/locale"


# Check if xterm can be used
#===========================
/bin/rm -f /tmp/checkxterm.txt && XOK=0
xterm -u8 -iconic -e touch /tmp/checkxterm.txt
if [ -e /tmp/checkxterm.txt ];then XOK="1";fi
/bin/rm -f /tmp/checkxterm.txt


# check if we run a pclos kernel
#===============================
if [ -z "`uname -r |grep pclos`" ]; then
  MSG=$"Repair Database requires a 'pclos'-kernel.\n\nExiting."
  $ZEN2 --error --text "$MSG"
  $RXURD
  exit 0
fi


#login as super user
#===================
if [ ! -e $XURD ];then
  if [ -r /usr/bin/gksu ]; then LGI=/usr/bin/gksu; fi
  if [ -r /usr/lib/kde4/libexec/kdesu ]; then LGI=/usr/lib/kde4/libexec/kdesu; fi
  if [ -r /usr/bin/kdesu ]; then LGI=/usr/bin/kdesu; fi
  # setup XURD
  echo "#"'!/bin/bash' > $XURD
  if [ "$UID" != "0" ]; then echo $LGI repair-database >> $XURD; else echo repair-database >> $XURD; fi
  chmod a+x $XURD
  $XURD &
  exit 0
fi


#test for super user privliges
#=============================
if [ "$UID" != "0" ]; then
  $ZEN2 --error --text=$"Please run this script as root."
  $RXURD
  exit 1
fi


# check internet connection
#==========================
cd /tmp
/bin/rm -f index.html inet.log
MSG=$"Testing your Internet connection."
( wget --timeout=15 http://www.google.com/index.html -o inet.log ) 2>&1 | $ZEN2 --title="$TITLE" --progress --text "$MSG" --pulsate --auto-close


if [ ! -f /tmp/index.html ]; then
  MSG=$"No Internet connection found.\n\nExiting."
  $ZEN2 --error --text "$MSG"
  $RXURD
  exit 1
fi


#repair database?
#================
MSG="Do you want to repair the repository databse?"
MSG2="No changes were made to your system."
zenity --question --title="$TITLE" --text="$MSG" echo $?
if [ "$?" = "1" ]; then
  $ZEN2 --info --width=250 --title="$TITLE" --text="$MSG2"
  $RXURD
  exit 0
fi


#remove corrupted database
#==========================
rm -rf /var/lib/rpm/__db*


#rebuild rpm database
#=====================
MSG=$"Rebuilding database, This can take some time please let it finish."
MSG2=$"You chose to exit. The database was not repaired!"
(echo && $REBUILD) | $ZEN2 --progress --pulsate --width 350 --auto-close --auto-kill --title="$TITLE" --text "$MSG" echo $?
if [ "$?" = "1" ]; then
  $ZEN2 --warning --width=250 --title="$TITLE" --text="$MSG2"
  $RXURD
  exit 0
fi


#check for duplicate installed package
#=====================================
dupeclean


#clean out apt
#=============
$CLEANAPT
MSG=$"Cleaning database."
MSG2=$"You chose to exit. The database was not cleaned!"
(echo && $CLEANAPT2) | $ZEN2 --progress --pulsate --width 350 --auto-close --auto-kill --title="$TITLE" --text "$MSG" echo $?
if [ "$?" = "1" ]; then
  $ZEN2 --warning --width=350 --title="$TITLE" --text="$MSG2"
  $RXURD
  exit 0
fi


#exit
#====
MSG=$"Database successfully repaired."
$ZEN2 --info --width=350 --title="$TITLE" --text="$MSG"
$RXURD
exit 0

Thanks to: Neal for all the help and icon, blindschLeiche for the progress bar, and any one who worked on the getvirtualbox script it was very helpful.


Added
WICON - window icon: 01/12/2011
XURD - /tmp/xsurepair-database
RXURD - removes XURD
Added --title to ZEN2
export - textdomain/textdomaindir
check xterm
check kernel
messages after hitting cancel/finished
new yad version: 01/13/2011
retry button for check super user (67): 01/14/2011

Updated: 01/12/2011
'' marks on /tmp/xsurepair-database
Shortened the rebuild database(102) now matches cleaning database (120)
added missing $ in rebuilding database(101,6)
--title removed from ZEN2

Problems: 09/05/2011
Cancel button on rebuilding database finshes current operation before stopping.

Original post:

I finished my first zenity script and I bet I'm going to get a bunch comments on what I did wrong haha. But that is good it will help me learn.

I decided to make this script because I remember when I first used Linux I hated rebuilding the database.
The script rebuilds and cleans the repository database according to Texstars requirements: http://www.pclinuxos.com/forum/index.php/topic,80839.0.html
« Last Edit: November 13, 2011, 03:07:20 PM by glamdring »

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3794
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: Finished my first zenity script
« Reply #1 on: January 11, 2011, 01:04:51 PM »
Hi,
found here some information about cancle button, hopeful it's what you need  ;)

Quote
--text=STRING     Set the dialog text
--percentage=INT  Set initial percentage
--auto-close      Close dialog when 100% has been reached
--auto-kill       Kill parent process if cancel button is pressed
--pulsate         Pulsate progress bar

Good luck, and congratulations for your first script  :)

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #2 on: January 11, 2011, 02:11:27 PM »
Moving this to Code Hackers. :)

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Finished my first zenity script
« Reply #3 on: January 11, 2011, 02:34:47 PM »
auto-kill worked perfectly!


Now to figure out how I can add this to my menu or make a icon that can launch it. Dose any one know how to make a window come up with a root login? Like when you launch synaptic.

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #4 on: January 11, 2011, 02:52:19 PM »
You would need to create a .desktop file for it to have it show in your menu. Check /usr/share/applications for examples.

The Exec= line will be either gksu /usr/bin/app-name (if you put it in /usr/bin) or kdesu /usr/bin/app-name, if you're using kde.


Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Finished my first zenity script
« Reply #5 on: January 11, 2011, 04:29:44 PM »
Okay so some good some bad.

Launching of it from terminal it works fine, but launching it by clicking on the script has some problem with closing. If i close the script at any time it just relaunches it and even if I let it work all the way through it still relaunches the script. Will this even be a problem if I made a menu entry?

I'm Using
Code: [Select]
kdesu /usr/bin/repair-database

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #6 on: January 11, 2011, 04:41:22 PM »
Have you updated the script in your first post? If not, please do.
What settings do you have for your script?

Here is an icon. If it doesn't suit, let me know.


Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Finished my first zenity script
« Reply #7 on: January 11, 2011, 05:18:03 PM »
Just updated them.

No special settings for script just 755.

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #8 on: January 11, 2011, 05:26:14 PM »
Just updated them.

No special settings for script just 755.

Did you mark it as executable?


Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #9 on: January 11, 2011, 05:46:11 PM »
I installed to my test box, from file manager superuser mode, I right clicked, chose Open and then Open in Terminal. The feedback I'm getting in terminal is -

Code: [Select]
--auto-close is not supported for this dialog
No duplicate/multiple versions of rpms found.

The progress bar closes, and the the process begins again. Same output repeated over and over.


Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Finished my first zenity script
« Reply #10 on: January 11, 2011, 06:07:21 PM »
Sorry gave you the wrong update I had to remove --auto-close in the question ill update now.

EDIT
But still getting the same thing if not launched from terminal. (Yes executable is marked)

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Finished my first zenity script
« Reply #11 on: January 12, 2011, 03:19:10 AM »
Some other scripts you may want to have a look at are --
getopenoffice
addlocale
dupeclean-gui
lx-autostart
ezswitch

(for starters ;))

I believe you will learn something useful from each of them.


Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Finished my first zenity script
« Reply #12 on: January 12, 2011, 07:33:47 AM »
Some other scripts you may want to have a look at are --
getopenoffice
addlocale
dupeclean-gui
lx-autostart
ezswitch

(for starters ;))

I believe you will learn something useful from each of them.



Thanks a lot already learning a lot from getopenooffice. Working on the script hope to post a newer version later today!

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: My first zenity script
« Reply #13 on: January 12, 2011, 11:40:25 AM »
After using the whole xterm system I found in many of Pclos scripts my login works right. But it would be nice if some one could explain to me how it works. I understand everything besides the part under #setup XURD I know its exporting the file to /tmp/repair-database because I had to make it delete that file when done or canceled or the script won't run right again.

But the cancel button on #rebuilding rpm database doesn't seem to work right. I don't see anything different from that one to the other ones but its the only one not working.

-Thanks for all the help so far guys

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3794
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: My first zenity script
« Reply #14 on: January 12, 2011, 01:15:12 PM »
get your script, but not work for me, get this messages

Code: [Select]
/tmp/xsurepair-database: (Zeile) line 2: repair-database: (Kommando nicht gefunden)command not found.
What is needed?

I would like to see what is wrong with cancle button by *rebuilding rpm database...


EDIT:

okay found the mistake

Code: [Select]
XURD=/tmp/xsurepair-database
change to

Code: [Select]
XURD='/tmp/xsurepair-database'
then it will work...

Should it not call
Quote
Do you want to repair the repository database?

« Last Edit: January 12, 2011, 01:31:40 PM by blindschLeiche »