Skip to content

Instantly share code, notes, and snippets.

@sillywilly42
Last active February 25, 2016 16:14
Show Gist options
  • Select an option

  • Save sillywilly42/49af9b6fb4c8729310b1 to your computer and use it in GitHub Desktop.

Select an option

Save sillywilly42/49af9b6fb4c8729310b1 to your computer and use it in GitHub Desktop.
Written in Python because I couldn't get Mark Carver's bash version to work in the DeployStudio Runtime environment.
#!/usr/bin/python
"""cocoaDialog Rsync Progress.
Rsync files using the cocoaDialog progress bar. Example usage:
$ rsync -avr --progress ~/source ~/destination | ./rsync_progress.py
Adapted from Mark Carver's gist:
https://gist.github.com/markcarver/ae6e229c33453f2547a8
NB. Will only work with rsync version 3 and higher.
Copyright (c) 2016, Will Jenkins
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
import subprocess
import sys
COCOA_DIALOG = 'path/to/CocoaDialog.app/Contents/MacOS/CocoaDialog'
COCOA_DIALOG_ARGS = [COCOA_DIALOG, 'progressbar', '--title', 'File Copy',
'--width', '500', '--percent', '0', '--float']
def format_for_cocoadialog(line, percent):
"""Format an output line from rsync for display in cocoaDialog.
Args:
line: a line of output from cocoaDialog when it is invoked with --progress.
percent: the current percentage progress of the rsync
Returns a tuple of new percentage and formatted string.
"""
if '-chk=' in line:
# If this is a progress report line containing to-chk or ir-chk then try to
# calculate the new percentage progress.
values = line.split('-chk=')[1].split(')')[0].split('/')
try:
percent = (1 - (float(values[0])/float(values[1]))) * 100
except ValueError:
# It's possible a filename might contain '-chk=' which results in the line
# being misidentified as a report line.
return percent, '%d %s\n' % (percent, line)
else:
return percent, '%d\n' % percent
else:
# Otherwise this is an informational line eg. filename, so print existing
# percentage followed by the raw line.
return percent, '%d %s\n' % (percent, line)
def main():
"""Main method when run as a script."""
cd_proc = subprocess.Popen(COCOA_DIALOG_ARGS, stdin=subprocess.PIPE)
percent = 0
while True:
try:
# Read a line from STDIN.
line = sys.stdin.readline()
# Either nothing is being piped in or the EOF has been reached.
if not line:
break
# Print the line to STDOUT for logging. The comma supresses \n output.
print line,
percent, formatted_line = format_for_cocoadialog(line, percent)
# Write the formatted string to cocoaDialog's STDIN buffer.
cd_proc.stdin.write(formatted_line)
except KeyboardInterrupt:
# Gracefully catch escape sequences to prevent python traceback.
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment