Skip to content

Instantly share code, notes, and snippets.

@kalebo
Last active August 29, 2015 14:05
Show Gist options
  • Save kalebo/698641314e9685c5c909 to your computer and use it in GitHub Desktop.
Save kalebo/698641314e9685c5c909 to your computer and use it in GitHub Desktop.
A wrapper for cwRsync to check that the correct drives are present before running
import sys
from ctypes import *
from datetime import datetime
import subprocess as subp
STD_OUTPUT_HANDLE_ID = c_ulong(0xfffffff5)
windll.Kernel32.GetStdHandle.restype = c_ulong
std_output_hdl = windll.Kernel32.GetStdHandle(STD_OUTPUT_HANDLE_ID)
def printcolor(string, color):
windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, color)
print(string)
windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, 7)
try:
f_sn = subp.check_output('dir F:', shell=True).split('\r')[1].split(' ')[-1]
g_sn = subp.check_output('dir G:', shell=True).split('\r')[1].split(' ')[-1]
except CalledProcessError:
printcolor("Failed to get serial numbers for drives F:\\ and/or G:\\!",12)
printcolor("Exiting...")
sys.exit(1)
if f_sn == "69FC-81E1" and g_sn == "0305-0BB0":
printcolor("Drive serial numbers match expected values...",2)
else:
printcolor("Drive serial numbers for F:\\ and/or G:\\ do NOT match expected values!",12)
printcolor("Exiting...")
sys.exit(2)
printcolor("Running Rsync...",2)
#rsyncout = subp.check_output("cwrsync.cmd")
#print(rsyncout)
logfilename = "rsync-" + datetime.today().strftime("%Y%m%dT%H%M") + ".log"
logfile = file(logfilename, 'w')
rsync = subp.Popen("cwrsync.cmd", stdout=subp.PIPE)
for line in iter(rsync.stdout.readline, ""):
logfile.write(line)
sys.stdout.write(line)
printcolor("Rsync is finished!",2)
printcolor("Log will be saved to ./{}".format(logfilename),6)
logfile.close()
raw_input("\nPress <ENTER> to close.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment