def replace_if_changed(localfile, jottapath, JFS): """Compare md5 hash to determine if contents have changed. Upload a file from local disk and replace file on JottaCloud if the md5s differ, or continue uploading if the file is incompletely uploaded. Returns the JottaFile object""" jf = JFS.getObject(jottapath) with open(localfile) as lf: lf_hash = calculate_md5(lf) if type(jf) == JFSIncompleteFile: logging.debug("Local file %s is incompletely uploaded, continue", localfile) return resume(localfile, jf, JFS) elif jf.md5 == lf_hash: # hashes are the same logging.debug("hash match (%s), file contents haven't changed", lf_hash) return jf # return the version from jottaclouds else: return new(localfile, jottapath, JFS)
def replace_if_changed(localfile, jottapath, JFS): """Compare md5 hash to determine if contents have changed. Upload a file from local disk and replace file on JottaCloud if the md5s differ, or continue uploading if the file is incompletely uploaded. Returns the JottaFile object""" jf = JFS.getObject(jottapath) lf_hash = getxattrhash(localfile) # try to read previous hash, stored in xattr if lf_hash is None: # no valid hash found in xattr, with open(localfile) as lf: lf_hash = calculate_md5(lf) # (re)calculate it if type(jf) == JFSIncompleteFile: log.debug("Local file %s is incompletely uploaded, continue", localfile) return resume(localfile, jf, JFS) elif jf.md5 == lf_hash: # hashes are the same log.debug("hash match (%s), file contents haven't changed", lf_hash) setxattrhash(localfile, lf_hash) return jf # return the version from jottaclouds else: setxattrhash(localfile, lf_hash) return new(localfile, jottapath, JFS)
def replace_if_changed(localfile, jottapath, JFS): """Compare md5 hash to determine if contents have changed. Upload a file from local disk and replace file on JottaCloud if the md5s differ, or continue uploading if the file is incompletely uploaded. Returns the JottaFile object""" jf = JFS.getObject(jottapath) lf_hash = getxattrhash(localfile) # try to read previous hash, stored in xattr if lf_hash is None: # no valid hash found in xattr, with open(localfile, 'rb') as lf: lf_hash = calculate_md5(lf) # (re)calculate it if type(jf) == JFSIncompleteFile: log.debug("Local file %s is incompletely uploaded, continue", localfile) return resume(localfile, jf, JFS) elif jf.md5 == lf_hash: # hashes are the same log.debug("hash match (%s), file contents haven't changed", lf_hash) setxattrhash(localfile, lf_hash) return jf # return the version from jottaclouds else: setxattrhash(localfile, lf_hash) return new(localfile, jottapath, JFS)