Refracta Development, Scripts, etc.
Post a reply
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## [Solved, sort of..] function check_space

Sun Oct 13, 2013 7:01 pm

I'm working on the check_space function in refractasnapshot-gui to trim it down. Currently, it runs df and awk six times each, and it takes awhile to complete. I trimmed it down to once each, and I rearranged the output so that the columns line up better (sometimes). That last part depends on how long the paths are. I have some encrypted partitions, and they have long path names.
Please try these, and let me know how they work. The script does nothing except report the space. Ignore the missing stuff like how many snapshots you have, how much space they're taking up and what the config file is. Those variables aren't set. My main concerns are the columns lining up in the df output and the fact that with zenity, I can't seem to control the window height. It might be too big on some screens, and the buttons might be off the screen. I'm also interested to know if it takes awhile to complete. In my tests, there's no delay like there is when running current versions of refractasnapshot.
If you want to try it with a progress bar, comment out the first df line in the check_space function and uncomment the second one. If you know how to use printf in awk better than I do, and you want to play with that, there are some of my failed attempts commented out.
And thanks in advance to anyone who tests this.
This one will use yad in preference to zenity if both are present:

Code:
  `#!/usr/bin/env bash
  # if yad is installed, use in preference
  if [[ -f /usr/bin/yad ]]; then
  DIALOG="yad"
  INFO="image=gtk-dialog-info"
  QUESTION="image=gtk-dialog-question"
  WARNING="image=gtk-dialog-warning"
  ERROR="image=gtk-dialog-error"
  #buttons
  BUTTON0="button"
  BUTTON1="button"
  BUTTON0NUM=":0"
  BUTTON1NUM=":1"
  #cancel button always returns 1 as $?
  #ok button always returns 0 as $?
  #ok is default (highlighted)
  #buttons in yad dialog window may show reversed from zenity window, e.g.
  #yad: ok -- cancel (0 -- 1)
  #zenity: cancel -- ok (1 -- 0)
  elif [[ -f /usr/bin/zenity ]]; then
  # use zenity
  DIALOG="zenity"
  INFO="info"
  QUESTION="question"
  WARNING="warning"
  ERROR="error"
  #buttons
  BUTTON0="ok-label"
  BUTTON1="cancel-label"
  BUTTON0NUM=""
  BUTTON1NUM=""
  else
  xterm -fa monaco -fs 12 -hold -e echo "
  Neither Yad nor Zenity is installed. You can't run the GUI version of
  Refracta Installer without one of those. Instead, you can run
  'refractainstaller' from a terminal or console for the CLI version.
  "
  fi
  # Check disk space on mounted filesystems.
  check_space () {
  disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 "\t" $4 "\t" $5 " \t" $6 "\t\t\t" $1 }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 "\t" $4 "\t" $5 " \t" $6 "\t\t\t" $1 }' | tee >($DIALOG --title="Checking disk space..." --progress --pulsate --auto-close --width 300) ;)
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ print $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t\t\t"}{ printf (%-32s, $1) }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ print $2 "\t" $3 "\t" $4 "\t" $5 "\t" }{ printf "%-18s", $6 }{ printf "%-28s", $1 "\n" }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ printf "%-.32s", $1 }{ print "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ printf "%-40s", $1 "\t" }{ print "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 }')
  }
  # Put information in a zenity or yad window to show current settings and disk space
  report_space () {
  $DIALOG --$QUESTION --title="Disk Space and Settings Report" --${BUTTON0}="Create Snapshot"${BUTTON0NUM} \
  --${BUTTON1}="Exit"${BUTTON1NUM} --height=550 --width=920 \
  --text "Please CLOSE any running applications NOW.
  You will need plenty of free space. It is recommended that free space (Avail) in the partition that holds the work directory (probably \"/\") should be two times the total installed system size (Used).
  You can deduct the space taken up by previous snapshots and any saved copies of the system from the Used amount.
  * You have $snapshot_count snapshots taking up $snapshot_size of disk space.
  $saved_copy
  $save_message
  * The snapshot directory is currently set to $snapshot_dir
  $tmp_warning
  You can change these and other settings by editing
  $configfile.
  Current disk usage:
  (For complete listing, exit and run 'df -h')
  $disk_space
  "
  if [ $? -ne 0 ]; then
  exit 0
  fi
  }
  check_space
  report_space
  echo "Done! "
  `

This one was changed so that it will only use zenity:

Code:
  `#!/usr/bin/env bash
  # if yad is installed, use in preference
  #if [[ -f /usr/bin/yad ]]; then
  DIALOG="yad"
  INFO="image=gtk-dialog-info"
  QUESTION="image=gtk-dialog-question"
  WARNING="image=gtk-dialog-warning"
  ERROR="image=gtk-dialog-error"
  #buttons
  BUTTON0="button"
  BUTTON1="button"
  BUTTON0NUM=":0"
  BUTTON1NUM=":1"
  #cancel button always returns 1 as $?
  #ok button always returns 0 as $?
  #ok is default (highlighted)
  #buttons in yad dialog window may show reversed from zenity window, e.g.
  #yad: ok -- cancel (0 -- 1)
  #zenity: cancel -- ok (1 -- 0)
  #el
  if [[ -f /usr/bin/zenity ]]; then
  # use zenity
  DIALOG="zenity"
  INFO="info"
  QUESTION="question"
  WARNING="warning"
  ERROR="error"
  #buttons
  BUTTON0="ok-label"
  BUTTON1="cancel-label"
  BUTTON0NUM=""
  BUTTON1NUM=""
  else
  xterm -fa monaco -fs 12 -hold -e echo "
  Neither Yad nor Zenity is installed. You can't run the GUI version of
  Refracta Installer without one of those. Instead, you can run
  'refractainstaller' from a terminal or console for the CLI version.
  "
  fi
  # Check disk space on mounted filesystems.
  check_space () {
  disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 "\t" $4 "\t" $5 " \t" $6 "\t\t\t" $1 }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 "\t" $4 "\t" $5 " \t" $6 "\t\t\t" $1 }' | tee >($DIALOG --title="Checking disk space..." --progress --pulsate --auto-close --width 300) ;)
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ print $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t\t\t"}{ printf (%-32s, $1) }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ print $2 "\t" $3 "\t" $4 "\t" $5 "\t" }{ printf "%-18s", $6 }{ printf "%-28s", $1 "\n" }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ printf "%-.32s", $1 }{ print "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 }')
  #disk_space=$(df -h -x tmpfs -x devtmpfs | awk '{ printf "%-40s", $1 "\t" }{ print "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 }')
  }
  # Put information in a zenity or yad window to show current settings and disk space
  report_space () {
  $DIALOG --$QUESTION --title="Disk Space and Settings Report" --${BUTTON0}="Create Snapshot"${BUTTON0NUM} \
  --${BUTTON1}="Exit"${BUTTON1NUM} --height=550 --width=920 \
  --text "Please CLOSE any running applications NOW.
  You will need plenty of free space. It is recommended that free space (Avail) in the partition that holds the work directory (probably \"/\") should be two times the total installed system size (Used).
  You can deduct the space taken up by previous snapshots and any saved copies of the system from the Used amount.
  * You have $snapshot_count snapshots taking up $snapshot_size of disk space.
  $saved_copy
  $save_message
  * The snapshot directory is currently set to $snapshot_dir
  $tmp_warning
  You can change these and other settings by editing
  $configfile.
  Current disk usage:
  (For complete listing, exit and run 'df -h')
  $disk_space
  "
  if [ $? -ne 0 ]; then
  exit 0
  fi
  }
  check_space
  report_space
  echo "Done! "
  `

nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: function check_space - Please test

Sun Oct 13, 2013 10:42 pm

"My main concerns are the columns lining up in the df output and the fact that with zenity, I can't seem to control the window height. It might be too big on some screens, and the buttons might be off the screen. I'm also interested to know if it takes awhile to complete"
First script, but yad not installed:
There is no delay. It opens "immediatly" (as far humans are concerned.
Lining up colums is ok for "Size" "Used" and "Avail", then it is a mess ("Use%" "Mountet" and "Filesystem").
The window is too big (height). I can move it around, so (to me) that is no problem).
Second script:
same like above.
----
I just saw someone at the #debian IRC channel answer a (quite difficult) awk question in the blink of an eye. You might at least post it and ask if there is anything obvious. The poster is there very regulary (i can't remember the name right now).
I don't know awk and i don't know zenity. (aka: It doesn't make much sense i look at the code itself, i simply ran the scripts).
My screen on the laptop is: 1360x768
PS: If you want a screenshot let me know.
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: function check_space - Please test

Sun Oct 13, 2013 10:48 pm

/* Off topic */
Today i looked at really lots of code (echoping) and was cursing due to lack of comments (for the dev they might be superfluous, but i got very hard times to understand what is going on at all.
I looked at your code and like the comments a lot. If you can: stick to it
(say the very first one: # # if yad is installed, use in preference. Yes, that might be obvious, but the comment helps -me- to understand it immediatly, even if i didn't read bash for a while)
like said: off-topic.
fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: function check_space - Please test

Mon Oct 14, 2013 12:23 am

Sure, I'd like to see a screenshot. You show me yours, and I'll show you mine. Things are a little out of line at the end, but it's better than it was. And with yad, the window height was always just right when there was no --height option in the command. I added that for zenity, but it didn't change the height, no matter what numbers I used.
Comments will stay. I can read English a lot faster than I can read bash, and I use the comments to find the section I want. Well, that, and sometimes I need them to figure out what I wrote in the code.
Off-off-topic: What happened to #study-group and #friends-of-freedom? Are they both defunct? Last few times I checked, they were both completely deserted.
Edit: Here it is in refractasnapshot:
http://distro.ibiblio.org/refracta/misc/disk_space2.png
And this is the test version:
http://distro.ibiblio.org/refracta/misc/disk_space1.png
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: function check_space - Please test

Mon Oct 14, 2013 3:50 am

I ran it on a different machine ( a PC with an attached monitor, but same resolution as far i can tell).
Much better. The window is inside of the screen. The output formatting is still a bit odd:
http://resident.dyndns.info/content/wor ... _scrot.png
Give me a bit to get back to the laptop and upload the other shot.
After a bit: http://resident.dyndns.info/content/wor ... crot_0.png
I hope you can see that the bottom of the window is outside the range of the screen. A simple alt+mousedrag will solve that.
I am so barbarian, i probably wouldn't have noticed anything on my own. For me it's no problem
Yes, i guess #study-group is defunct. (It doesn't need to be, as far it's me, but it looks like that's the way it is).
If i am online and if i got an IRC client running i am always at #debian (You can ping me there, right click and send msg, and i will open #study-group). I am also at #dragora (but don't know the server). And a couple of others, but #debian seems like the most easy to remember. name is like usual: nadir or n4dir, not sure.
PS: I think i got the screenshots right. Both must look slightly different and have different desktop-wallpapers. If i borked it, let me know (it really is late, 6 in the morning after a confusing night).
ukbrian

- ukbrian
- Posts: 109
- Joined: Mon Jul 25, 2011 8:22 am
- Location: Cheltenham, UK

## Re: function check_space - Please test

Mon Oct 14, 2013 9:07 am

I'm using SalineOS dev 1 as my base system as it does everything I want and is reliable.
I think it has Yad 0.17 pre-installed and synaptic complains about dependencies if I try to update it.
Refracta snapshot/installer install and run OK but I get this error when I run your script with yad existing in /usr/bin

Code:
  `Neither Yad nor Zenity is installed. You can't run the GUI version of
  Refracta Installer without one of those. Instead, you can run
  'refractainstaller' from a terminal or console for the CLI version.`

fsmithred

- fsmithred
- Posts: 2101
- Joined: Wed Mar 09, 2011 9:13 pm

## Re: function check_space - Please test

Mon Oct 14, 2013 10:24 am

ukbrian, if you're using the second script I posted above, you're getting the expected result (assuming that zenity is not installed.) Second script was altered not to test for yad. Test with the first script, and it should run.
nadir - could you try again after adding some spaces before the tabs in the awk command, like this:

Code:
  `disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 " \t" $4 " \t" $5 " \t" $6 "\t\t\t" $1 }')`

ukbrian

- ukbrian
- Posts: 109
- Joined: Mon Jul 25, 2011 8:22 am
- Location: Cheltenham, UK

## Re: function check_space - Please test

Mon Oct 14, 2013 1:45 pm

I was running script 1 on SalineOS but yad didn't show up as being installed in synaptic.
When I ran the second script I got the same error but when I closed the error(terminal) window the script ran.
I booted into SolydX 32 bit xfce Wheezy testing where yad and zenity showed up in synaptic but I got the same results.
I booted into SolusOS Debian Wheezy alpha 7 both yad and zenity showing in synaptic and I got the error on script 1 but it ran when I closed the error window, with script 2 I got the error but the script failed to run.
dzz

- dzz
- Posts: 647
- Joined: Wed Apr 27, 2011 11:53 am
- Location: Devon, England

## Re: function check_space - Please test

Mon Oct 14, 2013 1:56 pm

I get much the same results as nadir.
It seems "list data" given to zenity as text (e.g. zenity --question --text="$list_data") causes a big space after the text, then the window is too high.for a low-res monitor (no good at all for a netbook) Yad handles it better. Similar results using blkid output.
However if you use zenity --list (something lke this:)

Code:
  `df -h -x tmpfs -x devtmpfs -x iso9660|grep -v Filesystem| $DIALOG --list --title="Disk Space and Settings Report" --separator="" --column "Filesystem Size Used Avail Use% Mountpoint" --${BUTTON0}="Create Snapshot"${BUTTON0NUM} \
  --${BUTTON1}="Exit"${BUTTON1NUM} --height=550 --width=920 \
  --text "Please CLOSE any running applications NOW.
  You will need plenty of free space. It is recommended that free space (Avail) in the partition that holds the work directory (probably \"/\") should be two times the total installed system size (Used).
  You can deduct the space taken up by previous snapshots and any saved copies of the system from the Used amount.
  * You have $snapshot_count snapshots taking up $snapshot_size of disk space.
  $saved_copy
  $save_message
  * The snapshot directory is currently set to $snapshot_dir
  $tmp_warning
  You can change these and other settings by editing
  $configfile.
  Current disk usage:
  (For complete listing, exit and run 'df -h')"
  `

the zenity window behaves better, although it's still a challenge getting it to display as neat columns.
nadir

- nadir
- Posts: 1160
- Joined: Wed Mar 09, 2011 4:18 am
- Location: here

## Re: function check_space - Please test

Mon Oct 14, 2013 8:21 pm

> fsmithred wrote:nadir - could you try again after adding some spaces before the tabs in the awk command, like this:
> Code:
>   `disk_space=$(df -h -x tmpfs -x devtmpfs -x iso9660 | awk '{ print " " $2 "\t" $3 " \t" $4 " \t" $5 " \t" $6 "\t\t\t" $1 }')`

Yes, that solves it.
(There is a very tiny "bad formatting" in the first line of "Filesystem", it is one or two characters too far left. The rest is perfect "aligned"
(Sorry, i don't know the correct words. In short: 99.9% perfect and a tiny incorrectness).
I didn't understand you at first and tried two tabs where you now got a space and a tab. That didn't help. But i guess you already tried that and know it already.
Post a reply
Jump to:

Powered by phpBB © phpBB Group.

phpBB Mobile / SEO by Artodia.