import boto3 import re import zipfile import os import sys #localFilename = "/Users/patrickjmcd/Dropbox (Henry Pump)/Patrick McDonagh/POConsole/POConsole/currentVersion.txt" outputFolder = "/home/poconsole/src" localFilename = "{0}/{1}".format(outputFolder, "currentVersion.txt") #s3 = boto3.resource('s3') client = boto3.client('s3') def checkUpdateNeeded(force = False): currentVersionObj = client.get_object(Bucket='henrypumpdata', Key='currentVersion.txt') s3currentVersionStr = currentVersionObj['Body']._raw_stream.data s3regex = re.search("(\d+)", s3currentVersionStr) s3cvInt = int(s3regex.groups()[0]) try: localCurrentVersionStr = "" with open(localFilename, 'r') as f: localCurrentVersionStr = f.read() localregex = re.search("(\d+)", localCurrentVersionStr) localcvInt = int(localregex.groups()[0]) except: localcvInt = 0 updateNeeded = not ( s3cvInt == localcvInt) if updateNeeded or force: try: targetFile = "{0}.zip".format(s3cvInt) # if updateNeeded: # print "Update needed to get {0}".format(targetFile) # if force: # print "Forcing update to get {0}".format(targetFile) downloadCmd = client.download_file('henrypumpdata', targetFile, targetFile) zfile = zipfile.ZipFile(targetFile) zfile.extractall(outputFolder) # for name in zfile.namelist(): # (dirname,filename) = os.path.split(name) # if not (dirname[:2] == "__"): # print "Decompressing {0} on {1}.".format(filename,dirname) # if not os.path.exists("{0}/{1}".format(outputFolder, dirname)): # print "Creating {0}/{1}".format(outputFolder, dirname) # os.makedirs("{0}/{1}".format(outputFolder, dirname)) # zfile.extract(name, "{0}/{1}".format(outputFolder, dirname)) with open(localFilename, 'w+') as f2: f2.write(str(targetFile.replace(".zip",""))) return {"status":"success", "message":"Files updated!"} except: return {"status":"error", "message":"error unzipping files"} else: return {"status":"success", "message":"All files are up to date!"} if __name__ == '__main__': if len(sys.argv) > 1: print checkUpdateNeeded(True) else: print checkUpdateNeeded()