def ensure_downloaded(url, dest, sha256sum=None, use_sudo=False): if files.exists(dest, use_sudo): if sha256sum is None: return result = calc_sha256sum(dest, use_sudo) if sha256sum == result: return download(url, dest, use_sudo)
def ensure_template(filename, destination, context=None, template_dir=None, use_sudo=False, backup=True, mirror_local_mode=False, mode=None, fabric_managed_format=FABRIC_MANAGED_DEFAULT_FORMAT): run_fn = run_or_sudo(use_sudo) # Normalize destination to be an actual filename, due to using StringIO with settings(hide('everything'), warn_only=True): if run_fn('test -d %s' % _expand_path(destination)).succeeded: sep = "" if destination.endswith('/') else "/" destination += sep + os.path.basename(filename) context = modify_context(filename, destination, context=context, fabric_managed_format=fabric_managed_format) text = render_as_text(filename, context=context, template_dir=template_dir) local_sum = hashlib.sha256(text).hexdigest() remote_sum = file.calc_sha256sum(destination, use_sudo=use_sudo) if remote_sum == local_sum: return False # Use mode kwarg to implement mirror_local_mode, again due to using # StringIO if mirror_local_mode and mode is None: mode = os.stat(filename).st_mode # To prevent put() from trying to do this # logic itself mirror_local_mode = False # Back up original file if backup and exists(destination): run_fn("cp %s{,.bak}" % _expand_path(destination)) # Upload the file. put(local_path=StringIO(text), remote_path=destination, use_sudo=use_sudo, mirror_local_mode=mirror_local_mode, mode=mode) return True
def ensure_template(filename, destination, context=None, template_dir=None, use_sudo=False, backup=True, mirror_local_mode=False, mode=None, fabric_managed_format=FABRIC_MANAGED_DEFAULT_FORMAT): run_fn = run_or_sudo(use_sudo) # Normalize destination to be an actual filename, due to using StringIO with settings(hide('everything'), warn_only=True): if run_fn('test -d %s' % _expand_path(destination)).succeeded: sep = "" if destination.endswith('/') else "/" destination += sep + os.path.basename(filename) context = modify_context(filename, destination, context=context, fabric_managed_format=fabric_managed_format) text = render_as_text(filename, context=context, template_dir=template_dir) local_sum = hashlib.sha256(text).hexdigest() remote_sum = file.calc_sha256sum(destination, use_sudo=use_sudo) if remote_sum == local_sum: return False # Use mode kwarg to implement mirror_local_mode, again due to using # StringIO if mirror_local_mode and mode is None: mode = os.stat(filename).st_mode # To prevent put() from trying to do this # logic itself mirror_local_mode = False # Back up original file if backup and exists(destination): run_fn("cp %s{,.bak}" % _expand_path(destination)) # Upload the file. put( local_path=StringIO(text), remote_path=destination, use_sudo=use_sudo, mirror_local_mode=mirror_local_mode, mode=mode ) return True