Wrap main routine in if __name__ == '__main__'

This commit is contained in:
Ryan Tucker 2011-09-20 16:55:23 -04:00
parent 45360b0b1a
commit c4cb3c8a7f

View file

@ -236,12 +236,13 @@ def send_file(bucket, filename, cmd, mesg):
return k return k
# Read in arguments if __name__ == '__main__':
if len(sys.argv) != 12: # Read in arguments
if len(sys.argv) != 12:
sys.stderr.write("Usage: %s tarCreatePath splitPath parPath host bkupNum \ sys.stderr.write("Usage: %s tarCreatePath splitPath parPath host bkupNum \
compPath fileExt splitSize outLoc parFile share\n" % sys.argv[0]) compPath fileExt splitSize outLoc parFile share\n" % sys.argv[0])
sys.exit(1) sys.exit(1)
else: else:
tarCreate = sys.argv[1] tarCreate = sys.argv[1]
splitPath = sys.argv[2] splitPath = sys.argv[2]
parPath = sys.argv[3] parPath = sys.argv[3]
@ -254,43 +255,43 @@ else:
parfile = sys.argv[10] parfile = sys.argv[10]
share = sys.argv[11] share = sys.argv[11]
for i in [tarCreate, compPath, splitPath, parPath]: for i in [tarCreate, compPath, splitPath, parPath]:
if i is not '' and not is_exe(i): if i is not '' and not is_exe(i):
sys.stderr.write('Error: %s is not an executable program\n' % i) sys.stderr.write('Error: %s is not an executable program\n' % i)
sys.exit(1) sys.exit(1)
# open s3 connection # open s3 connection
bucket = open_s3(secrets.accesskey, secrets.sharedkey, host) bucket = open_s3(secrets.accesskey, secrets.sharedkey, host)
beginning = time.time() beginning = time.time()
mesg = "Writing archive for host %s, backup #%i" % (host, bkupNum) mesg = "Writing archive for host %s, backup #%i" % (host, bkupNum)
if splitSize > 0 and is_exe(splitPath): if splitSize > 0 and is_exe(splitPath):
mesg += ', split into %i byte chunks' % splitSize mesg += ', split into %i byte chunks' % splitSize
if secrets.gpgsymmetrickey: if secrets.gpgsymmetrickey:
mesg += ', encrypted with secret key' mesg += ', encrypted with secret key'
logging.info(mesg) logging.info(mesg)
sys.stdout.write(time.strftime('%d-%H:%M:%S') + ": " + mesg + '\n') sys.stdout.write(time.strftime('%d-%H:%M:%S') + ": " + mesg + '\n')
sys.stdout.flush() sys.stdout.flush()
# Prepare the pipeline # Prepare the pipeline
if share == '*': if share == '*':
share = '\*' share = '\*'
cmd = '%s -t -h %s -n %i -s %s . ' % (tarCreate, host, bkupNum, share) cmd = '%s -t -h %s -n %i -s %s . ' % (tarCreate, host, bkupNum, share)
if splitSize > 0 and is_exe(splitPath): if splitSize > 0 and is_exe(splitPath):
filehead = '%s/%s.%i.tar.' % (outLoc, host, bkupNum) filehead = '%s/%s.%i.tar.' % (outLoc, host, bkupNum)
fileglob = filehead + '*' fileglob = filehead + '*'
cmd += '| %s -b %i - %s' % (splitPath, splitSize, filehead) cmd += '| %s -b %i - %s' % (splitPath, splitSize, filehead)
else: else:
fileglob = '%s/%s.%i.tar' % (outLoc, host, bkupNum) fileglob = '%s/%s.%i.tar' % (outLoc, host, bkupNum)
cmd += '> %s' % fileglob cmd += '> %s' % fileglob
filehead = fileglob + '.' filehead = fileglob + '.'
# is there already evidence of this having been done before? # is there already evidence of this having been done before?
if glob.glob('%s/%s.*.tar.*' % (outLoc, host)): if glob.glob('%s/%s.*.tar.*' % (outLoc, host)):
logging.info('Evidence of failed execution run prior! Finishing it.') logging.info('Evidence of failed execution run prior! Finishing it.')
somefile = os.path.basename(glob.glob('%s/%s.*.tar.*' % (outLoc, host))[0]) somefile = os.path.basename(glob.glob('%s/%s.*.tar.*' % (outLoc, host))[0])
keyparts = somefile.split('.') keyparts = somefile.split('.')
@ -316,7 +317,7 @@ if glob.glob('%s/%s.*.tar.*' % (outLoc, host)):
logging.info(mesg) logging.info(mesg)
sys.stdout.write(time.strftime('%d-%H:%M:%S') + ": " + mesg + '\n') sys.stdout.write(time.strftime('%d-%H:%M:%S') + ": " + mesg + '\n')
sys.stdout.flush() sys.stdout.flush()
else: else:
logging.debug('Executing %s' % cmd) logging.debug('Executing %s' % cmd)
returncode = os.system(cmd) returncode = os.system(cmd)
@ -325,9 +326,9 @@ else:
logger.error('%s died with exit code %i' % (cmd, returncode)) logger.error('%s died with exit code %i' % (cmd, returncode))
sys.exit(1) sys.exit(1)
logging.info('Beginning post-processing of %i files from %s #%i' % (len(glob.glob(fileglob)), host, bkupNum)) logging.info('Beginning post-processing of %i files from %s #%i' % (len(glob.glob(fileglob)), host, bkupNum))
for i in sorted(glob.glob(fileglob)): for i in sorted(glob.glob(fileglob)):
sending_start = time.time() sending_start = time.time()
if secrets.gpgsymmetrickey and not i.endswith('.gpg'): if secrets.gpgsymmetrickey and not i.endswith('.gpg'):
sendfile = encrypt_file(i, secrets.gpgsymmetrickey, compPath) sendfile = encrypt_file(i, secrets.gpgsymmetrickey, compPath)
@ -369,10 +370,8 @@ for i in sorted(glob.glob(fileglob)):
sys.stdout.write('%s: File sent. Total time %i seconds, crypto time %i seconds, transfer speed %i bytes/second.\n' % (time.strftime('%d-%H:%M:%S'), sending_seconds, encrypt_seconds, bytespersecond)) sys.stdout.write('%s: File sent. Total time %i seconds, crypto time %i seconds, transfer speed %i bytes/second.\n' % (time.strftime('%d-%H:%M:%S'), sending_seconds, encrypt_seconds, bytespersecond))
sys.stdout.flush() sys.stdout.flush()
# finalize the backup # finalize the backup
key = MyKey(bucket) key = MyKey(bucket)
key.key = '%sCOMPLETE' % os.path.basename(filehead) key.key = '%sCOMPLETE' % os.path.basename(filehead)
key.set_contents_from_string('%s %s "%s"' % (beginning, time.time(), mesg)) key.set_contents_from_string('%s %s "%s"' % (beginning, time.time(), mesg))
key.close() key.close()