From 3de03147f1d8217ef2fe58c1388fc603720471be Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Fri, 9 Jun 2023 14:41:13 -0400 Subject: [PATCH] handling pyupgrade changes --- salt/states/file.py | 448 +++++++++++++++++++++----------------------- 1 file changed, 211 insertions(+), 237 deletions(-) diff --git a/salt/states/file.py b/salt/states/file.py index 2b8a63f40b0..ae5e33d709b 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -385,11 +385,11 @@ def _check_user(user, group): if user: uid = __salt__["file.user_to_uid"](user) if uid == "": - err += "User {} is not available ".format(user) + err += f"User {user} is not available " if group: gid = __salt__["file.group_to_gid"](group) if gid == "": - err += "Group {} is not available".format(group) + err += f"Group {group} is not available" if err and __opts__["test"]: # Write the warning with error message, but prevent failing, # in case of applying the state in test mode. @@ -439,7 +439,7 @@ def _gen_recurse_managed_files( exclude_pat=None, maxdepth=None, include_empty=False, - **kwargs + **kwargs, ): """ Generate the list of files managed by a recurse state @@ -643,10 +643,10 @@ def _check_file(name): if not os.path.isabs(name): ret = False - msg = "Specified file {} is not an absolute path".format(name) + msg = f"Specified file {name} is not an absolute path" elif not os.path.exists(name): ret = False - msg = "{}: file not found".format(name) + msg = f"{name}: file not found" return ret, msg @@ -739,7 +739,7 @@ def _check_directory( try: recurse_set = _get_recurse_set(recurse) except (TypeError, ValueError) as exc: - return False, "{}".format(exc), changes + return False, f"{exc}", changes if "user" not in recurse_set: user = None if "group" not in recurse_set: @@ -823,9 +823,9 @@ def _check_directory( comments = ["The following files will be changed:\n"] for fn_ in changes: for key, val in changes[fn_].items(): - comments.append("{}: {} - {}\n".format(fn_, key, val)) + comments.append(f"{fn_}: {key} - {val}\n") return None, "".join(comments), changes - return True, "The directory {} is in the correct state".format(name), changes + return True, f"The directory {name} is in the correct state", changes def _check_directory_win( @@ -855,9 +855,9 @@ def _check_directory_win( )["changes"] if changes: - return None, 'The directory "{}" will be changed'.format(name), changes + return None, f'The directory "{name}" will be changed', changes - return True, "The directory {} is in the correct state".format(name), changes + return True, f"The directory {name} is in the correct state", changes def _check_dir_meta(name, user, group, mode, follow_symlinks=False): @@ -905,17 +905,17 @@ def _check_touch(name, atime, mtime): "changes": {"new": name}, } if not os.path.exists(name): - ret["comment"] = "File {} is set to be created".format(name) + ret["comment"] = f"File {name} is set to be created" else: stats = __salt__["file.stats"](name, follow_symlinks=False) if (atime is not None and str(atime) != str(stats["atime"])) or ( mtime is not None and str(mtime) != str(stats["mtime"]) ): - ret["comment"] = "Times set to be updated on file {}".format(name) + ret["comment"] = f"Times set to be updated on file {name}" ret["changes"] = {"touched": name} else: ret["result"] = True - ret["comment"] = "File {} exists and has the correct times".format(name) + ret["comment"] = f"File {name} exists and has the correct times" return ret @@ -968,7 +968,7 @@ def _symlink_check(name, target, force, user, group, win_owner): changes["new"] = name return ( None, - "Symlink {} to {} is set for creation".format(name, target), + f"Symlink {name} to {target} is set for creation", changes, ) if __salt__["file.is_link"](name): @@ -976,12 +976,12 @@ def _symlink_check(name, target, force, user, group, win_owner): changes["change"] = name return ( None, - "Link {} target is set to be changed to {}".format(name, target), + f"Link {name} target is set to be changed to {target}", changes, ) else: result = True - msg = "The symlink {} is present".format(name) + msg = f"The symlink {name} is present" if not _check_symlink_ownership(name, user, group, win_owner): result = None changes["ownership"] = "{}:{}".format(*_get_symlink_ownership(name)) @@ -1031,28 +1031,28 @@ def _hardlink_check(name, target, force): """ changes = {} if not os.path.exists(target): - msg = "Target {} for hard link does not exist".format(target) + msg = f"Target {target} for hard link does not exist" return False, msg, changes elif os.path.isdir(target): - msg = "Unable to hard link from directory {}".format(target) + msg = f"Unable to hard link from directory {target}" return False, msg, changes if os.path.isdir(name): - msg = "Unable to hard link to directory {}".format(name) + msg = f"Unable to hard link to directory {name}" return False, msg, changes elif not os.path.exists(name): - msg = "Hard link {} to {} is set for creation".format(name, target) + msg = f"Hard link {name} to {target} is set for creation" changes["new"] = name return None, msg, changes elif __salt__["file.is_hardlink"](name): if _hardlink_same(name, target): - msg = "The hard link {} is presently targetting {}".format(name, target) + msg = f"The hard link {name} is presently targetting {target}" return True, msg, changes - msg = "Link {} target is set to be changed to {}".format(name, target) + msg = f"Link {name} target is set to be changed to {target}" changes["change"] = name return None, msg, changes @@ -1160,7 +1160,7 @@ def _get_template_texts( return _error(ret, msg) txtl.append("".join(tmplines)) else: - msg = "Failed to load template file {}".format(source) + msg = f"Failed to load template file {source}" log.debug(msg) ret["name"] = source return _error(ret, msg) @@ -1224,7 +1224,7 @@ def _shortcut_check( changes["new"] = name return ( None, - 'Shortcut "{}" to "{}" is set for creation'.format(name, target), + f'Shortcut "{name}" to "{target}" is set for creation', changes, ) @@ -1255,10 +1255,10 @@ def _shortcut_check( ) else: result = True - msg = 'The shortcut "{}" is present'.format(name) + msg = f'The shortcut "{name}" is present' if not _check_shortcut_ownership(name, user): result = None - changes["ownership"] = "{}".format(_get_shortcut_ownership(name)) + changes["ownership"] = f"{_get_shortcut_ownership(name)}" msg += ( ", but the ownership of the shortcut would be changed " "from {1} to {0}".format(user, _get_shortcut_ownership(name)) @@ -1342,7 +1342,7 @@ def hardlink( user=None, group=None, dir_mode=None, - **kwargs + **kwargs, ): """ Create a hard link @@ -1410,20 +1410,16 @@ def hardlink( gid = __salt__["file.group_to_gid"](group) if uid == "": - preflight_errors.append("User {} does not exist".format(user)) + preflight_errors.append(f"User {user} does not exist") if gid == "": - preflight_errors.append("Group {} does not exist".format(group)) + preflight_errors.append(f"Group {group} does not exist") if not os.path.isabs(name): - preflight_errors.append( - "Specified file {} is not an absolute path".format(name) - ) + preflight_errors.append(f"Specified file {name} is not an absolute path") if not os.path.isabs(target): - preflight_errors.append( - "Specified target {} is not an absolute path".format(target) - ) + preflight_errors.append(f"Specified target {target} is not an absolute path") if preflight_errors: msg = ". ".join(preflight_errors) @@ -1442,11 +1438,11 @@ def hardlink( # tracker that complains about not linking the zip builtin. for direction, item in zip_longest(["to", "from"], [name, target]): if os.path.isdir(item): - msg = "Unable to hard link {} directory {}".format(direction, item) + msg = f"Unable to hard link {direction} directory {item}" return _error(ret, msg) if not os.path.exists(target): - msg = "Target {} for hard link does not exist".format(target) + msg = f"Target {target} for hard link does not exist" return _error(ret, msg) # Check that the directory to write the hard link to exists @@ -1474,9 +1470,7 @@ def hardlink( # Otherwise throw an error else: - return _error( - ret, "File exists where the hard link {} should be".format(name) - ) + return _error(ret, f"File exists where the hard link {name} should be") # If the file is a hard link, then we can simply rewrite its target since # nothing is really being lost here. @@ -1508,7 +1502,7 @@ def hardlink( # Good to go ret["result"] = True - ret["comment"] = "Set target of hard link {} -> {}".format(name, target) + ret["comment"] = f"Set target of hard link {name} -> {target}" ret["changes"]["new"] = name # The link is not present, so simply make it @@ -1526,7 +1520,7 @@ def hardlink( # Made a new hard link, things are ok ret["result"] = True - ret["comment"] = "Created new hard link {} -> {}".format(name, target) + ret["comment"] = f"Created new hard link {name} -> {target}" ret["changes"]["new"] = name return ret @@ -1548,7 +1542,7 @@ def symlink( atomic=False, disallow_copy_and_unlink=False, inherit_user_and_group=False, - **kwargs + **kwargs, ): """ Create a symbolic link (symlink, soft link) @@ -1718,7 +1712,7 @@ def symlink( try: salt.utils.win_functions.get_sid_from_name(win_owner) except CommandExecutionError as exc: - preflight_errors.append("User {} does not exist".format(win_owner)) + preflight_errors.append(f"User {win_owner} does not exist") # Make sure users passed in win_perms exist if win_perms: @@ -1726,7 +1720,7 @@ def symlink( try: salt.utils.win_functions.get_sid_from_name(name_check) except CommandExecutionError as exc: - preflight_errors.append("User {} does not exist".format(name_check)) + preflight_errors.append(f"User {name_check} does not exist") # Make sure users passed in win_deny_perms exist if win_deny_perms: @@ -1734,21 +1728,19 @@ def symlink( try: salt.utils.win_functions.get_sid_from_name(name_check) except CommandExecutionError as exc: - preflight_errors.append("User {} does not exist".format(name_check)) + preflight_errors.append(f"User {name_check} does not exist") else: uid = __salt__["file.user_to_uid"](user) gid = __salt__["file.group_to_gid"](group) if uid == "": - preflight_errors.append("User {} does not exist".format(user)) + preflight_errors.append(f"User {user} does not exist") if gid == "": - preflight_errors.append("Group {} does not exist".format(group)) + preflight_errors.append(f"Group {group} does not exist") if not os.path.isabs(name): - preflight_errors.append( - "Specified file {} is not an absolute path".format(name) - ) + preflight_errors.append(f"Specified file {name} is not an absolute path") if preflight_errors: msg = ". ".join(preflight_errors) @@ -1763,7 +1755,7 @@ def symlink( if not os.path.isdir(os.path.dirname(name)): if makedirs: if __opts__["test"]: - tcomment += "\n{} will be created".format(os.path.dirname(name)) + tcomment += f"\n{os.path.dirname(name)} will be created" else: try: _makedirs( @@ -1777,7 +1769,7 @@ def symlink( win_inheritance=win_inheritance, ) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: if __opts__["test"]: tcomment += "\nDirectory {} for symlink is not present".format( @@ -1823,7 +1815,7 @@ def symlink( ret["comment"] = "Set ownership of symlink {} to {}:{}".format( name, user, group ) - ret["changes"]["ownership"] = "{}:{}".format(user, group) + ret["changes"]["ownership"] = f"{user}:{group}" else: ret["result"] = False if salt.utils.platform.is_windows(): @@ -1894,7 +1886,7 @@ def symlink( ) return _error( ret, - "{} exists where the symlink {} should be".format(fs_entry_type, name), + f"{fs_entry_type} exists where the symlink {name} should be", ) try: @@ -1906,7 +1898,7 @@ def symlink( ) return ret else: - ret["comment"] = "Created new symlink {} -> {}".format(name, target) + ret["comment"] = f"Created new symlink {name} -> {target}" ret["changes"]["new"] = name if not _check_symlink_ownership(name, user, group, win_owner): @@ -1936,38 +1928,38 @@ def absent(name, **kwargs): if not name: return _error(ret, "Must provide name to file.absent") if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if name == "/": return _error(ret, 'Refusing to make "/" absent') if os.path.isfile(name) or os.path.islink(name): if __opts__["test"]: ret["result"] = None ret["changes"]["removed"] = name - ret["comment"] = "File {} is set for removal".format(name) + ret["comment"] = f"File {name} is set for removal" return ret try: __salt__["file.remove"](name, force=True) - ret["comment"] = "Removed file {}".format(name) + ret["comment"] = f"Removed file {name}" ret["changes"]["removed"] = name return ret except CommandExecutionError as exc: - return _error(ret, "{}".format(exc)) + return _error(ret, f"{exc}") elif os.path.isdir(name): if __opts__["test"]: ret["result"] = None ret["changes"]["removed"] = name - ret["comment"] = "Directory {} is set for removal".format(name) + ret["comment"] = f"Directory {name} is set for removal" return ret try: __salt__["file.remove"](name, force=True) - ret["comment"] = "Removed directory {}".format(name) + ret["comment"] = f"Removed directory {name}" ret["changes"]["removed"] = name return ret except OSError: - return _error(ret, "Failed to remove directory {}".format(name)) + return _error(ret, f"Failed to remove directory {name}") - ret["comment"] = "File {} is not present".format(name) + ret["comment"] = f"File {name} is not present" return ret @@ -1984,7 +1976,7 @@ def tidied( age_size_logical_operator="OR", age_size_only=None, rmlinks=True, - **kwargs + **kwargs, ): """ .. versionchanged:: 3006.0,3005 @@ -2098,9 +2090,9 @@ def tidied( # Check preconditions if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if not os.path.isdir(name): - return _error(ret, "{} does not exist or is not a directory.".format(name)) + return _error(ret, f"{name} does not exist or is not a directory.") # Check time_comparison parameter poss_comp = ["atime", "ctime", "mtime"] @@ -2197,7 +2189,7 @@ def tidied( if todelete: if __opts__["test"]: ret["result"] = None - ret["comment"] = "{} is set for tidy".format(name) + ret["comment"] = f"{name} is set for tidy" ret["changes"] = {"removed": todelete} return ret ret["changes"]["removed"] = [] @@ -2207,14 +2199,14 @@ def tidied( __salt__["file.remove"](path) ret["changes"]["removed"].append(path) except CommandExecutionError as exc: - return _error(ret, "{}".format(exc)) + return _error(ret, f"{exc}") # Set comment for the summary ret["comment"] = "Removed {} files or directories from directory {}".format( len(todelete), name ) else: # Set comment in case there was nothing to remove - ret["comment"] = "Nothing to remove from directory {}".format(name) + ret["comment"] = f"Nothing to remove from directory {name}" return ret @@ -2237,9 +2229,9 @@ def exists(name, **kwargs): if not name: return _error(ret, "Must provide name to file.exists") if not os.path.exists(name): - return _error(ret, "Specified path {} does not exist".format(name)) + return _error(ret, f"Specified path {name} does not exist") - ret["comment"] = "Path {} exists".format(name) + ret["comment"] = f"Path {name} exists" return ret @@ -2257,9 +2249,9 @@ def missing(name, **kwargs): if not name: return _error(ret, "Must provide name to file.missing") if os.path.exists(name): - return _error(ret, "Specified path {} exists".format(name)) + return _error(ret, f"Specified path {name} exists") - ret["comment"] = "Path {} is missing".format(name) + ret["comment"] = f"Path {name} is missing" return ret @@ -2303,7 +2295,7 @@ def managed( win_perms_reset=False, verify_ssl=True, use_etag=False, - **kwargs + **kwargs, ): r""" Manage a given file, this function allows for a file to be downloaded from @@ -2988,7 +2980,7 @@ def managed( nextp, __NOT_FOUND, delimiter=contents_delimiter ) if nextc is __NOT_FOUND: - return _error(ret, "Pillar {} does not exist".format(nextp)) + return _error(ret, f"Pillar {nextp} does not exist") list_contents.append(nextc) use_contents = os.linesep.join(list_contents) else: @@ -2996,7 +2988,7 @@ def managed( contents_pillar, __NOT_FOUND, delimiter=contents_delimiter ) if use_contents is __NOT_FOUND: - return _error(ret, "Pillar {} does not exist".format(contents_pillar)) + return _error(ret, f"Pillar {contents_pillar} does not exist") elif contents_grains is not None: if isinstance(contents_grains, list): @@ -3006,7 +2998,7 @@ def managed( nextg, __NOT_FOUND, delimiter=contents_delimiter ) if nextc is __NOT_FOUND: - return _error(ret, "Grain {} does not exist".format(nextc)) + return _error(ret, f"Grain {nextc} does not exist") list_contents.append(nextc) use_contents = os.linesep.join(list_contents) else: @@ -3014,7 +3006,7 @@ def managed( contents_grains, __NOT_FOUND, delimiter=contents_delimiter ) if use_contents is __NOT_FOUND: - return _error(ret, "Grain {} does not exist".format(contents_grains)) + return _error(ret, f"Grain {contents_grains} does not exist") elif contents is not None: use_contents = contents @@ -3025,9 +3017,9 @@ def managed( if use_contents is not None: if not allow_empty and not use_contents: if contents_pillar: - contents_id = "contents_pillar {}".format(contents_pillar) + contents_id = f"contents_pillar {contents_pillar}" elif contents_grains: - contents_id = "contents_grains {}".format(contents_grains) + contents_id = f"contents_grains {contents_grains}" else: contents_id = "'contents'" return _error( @@ -3104,19 +3096,17 @@ def managed( if not create: if not os.path.isfile(name): # Don't create a file that is not already present - ret[ - "comment" - ] = "File {} is not present and is not set for creation".format(name) + ret["comment"] = f"File {name} is not present and is not set for creation" return ret u_check = _check_user(user, group) if u_check: # The specified user or group do not exist return _error(ret, u_check) if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if os.path.isdir(name): - ret["comment"] = "Specified target {} is a directory".format(name) + ret["comment"] = f"Specified target {name} is a directory" ret["result"] = False return ret @@ -3167,11 +3157,11 @@ def managed( "state of {}".format(name, mode, ret_perms["lmode"]) ) else: - ret["comment"] = "File {} not updated".format(name) + ret["comment"] = f"File {name} not updated" elif not ret["changes"] and ret["result"]: ret[ "comment" - ] = "File {} exists with proper permissions. No changes made.".format(name) + ] = f"File {name} exists with proper permissions. No changes made." return ret accum_data, _ = _load_accumulators() @@ -3205,7 +3195,7 @@ def managed( serange=serange, verify_ssl=verify_ssl, follow_symlinks=follow_symlinks, - **kwargs + **kwargs, ) if salt.utils.platform.is_windows(): @@ -3230,7 +3220,7 @@ def managed( ret["changes"] = {} elif ret["changes"]: ret["result"] = None - ret["comment"] = "The file {} is set to be changed".format(name) + ret["comment"] = f"The file {name} is set to be changed" ret["comment"] += ( "\nNote: No changes made, actual changes may\n" "be different due to other states." @@ -3239,7 +3229,7 @@ def managed( ret["changes"]["diff"] = "" else: ret["result"] = True - ret["comment"] = "The file {} is in the correct state".format(name) + ret["comment"] = f"The file {name} is in the correct state" return ret @@ -3247,7 +3237,7 @@ def managed( source, source_hash = __salt__["file.source_list"](source, source_hash, __env__) except CommandExecutionError as exc: ret["result"] = False - ret["comment"] = "Unable to manage file: {}".format(exc) + ret["comment"] = f"Unable to manage file: {exc}" return ret # Gather the source file from the server @@ -3268,12 +3258,12 @@ def managed( skip_verify, verify_ssl=verify_ssl, use_etag=use_etag, - **kwargs + **kwargs, ) except Exception as exc: # pylint: disable=broad-except ret["changes"] = {} log.debug(traceback.format_exc()) - return _error(ret, "Unable to manage file: {}".format(exc)) + return _error(ret, f"Unable to manage file: {exc}") tmp_filename = None @@ -3287,7 +3277,7 @@ def managed( except Exception as exc: # pylint: disable=broad-except return _error( ret, - "Unable to copy file {} to {}: {}".format(name, tmp_filename, exc), + f"Unable to copy file {name} to {tmp_filename}: {exc}", ) try: @@ -3323,7 +3313,7 @@ def managed( setype=setype, serange=serange, use_etag=use_etag, - **kwargs + **kwargs, ) except Exception as exc: # pylint: disable=broad-except ret["changes"] = {} @@ -3340,7 +3330,7 @@ def managed( sfn = __salt__["cp.is_cached"](source, __env__) if sfn: salt.utils.files.remove(sfn) - return _error(ret, "Unable to check_cmd file: {}".format(exc)) + return _error(ret, f"Unable to check_cmd file: {exc}") # file being updated to verify using check_cmd if ret["changes"]: @@ -3402,12 +3392,12 @@ def managed( setype=setype, serange=serange, use_etag=use_etag, - **kwargs + **kwargs, ) except Exception as exc: # pylint: disable=broad-except ret["changes"] = {} log.debug(traceback.format_exc()) - return _error(ret, "Unable to manage file: {}".format(exc)) + return _error(ret, f"Unable to manage file: {exc}") finally: if tmp_filename: salt.utils.files.remove(tmp_filename) @@ -3444,7 +3434,7 @@ def _get_recurse_set(recurse): if recurse_set is None or not set(_RECURSE_TYPES) >= recurse_set: raise ValueError( 'Types for "recurse" limited to {}.'.format( - ", ".join('"{}"'.format(rtype) for rtype in _RECURSE_TYPES) + ", ".join(f'"{rtype}"' for rtype in _RECURSE_TYPES) ) ) if "ignore_files" in recurse_set and "ignore_dirs" in recurse_set: @@ -3490,7 +3480,7 @@ def directory( win_deny_perms=None, win_inheritance=True, win_perms_reset=False, - **kwargs + **kwargs, ): r""" Ensure that a named directory is present and has the right perms @@ -3877,7 +3867,7 @@ def directory( # Must be an absolute path if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") # Check for existing file or symlink if ( @@ -3921,13 +3911,9 @@ def directory( ret["changes"]["forced"] = "Directory was forcibly replaced" else: if os.path.isfile(name): - return _error( - ret, "Specified location {} exists and is a file".format(name) - ) + return _error(ret, f"Specified location {name} exists and is a file") elif os.path.islink(name): - return _error( - ret, "Specified location {} exists and is a symlink".format(name) - ) + return _error(ret, f"Specified location {name} exists and is a symlink") # Check directory? if salt.utils.platform.is_windows(): @@ -3988,9 +3974,9 @@ def directory( win_inheritance=win_inheritance, ) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: - return _error(ret, "No directory to create {} in".format(name)) + return _error(ret, f"No directory to create {name} in") if salt.utils.platform.is_windows(): __salt__["file.mkdir"]( @@ -4005,7 +3991,7 @@ def directory( __salt__["file.mkdir"](name, user=user, group=group, mode=dir_mode) if not os.path.isdir(name): - return _error(ret, "Failed to create directory {}".format(name)) + return _error(ret, f"Failed to create directory {name}") ret["changes"][name] = {"directory": "new"} return ret @@ -4043,7 +4029,7 @@ def directory( recurse_set = _get_recurse_set(recurse) except (TypeError, ValueError) as exc: ret["result"] = False - ret["comment"] = "{}".format(exc) + ret["comment"] = f"{exc}" # NOTE: Should this be enough to stop the whole check altogether? if recurse_set: if "user" in recurse_set: @@ -4076,7 +4062,7 @@ def directory( ret["result"] = False ret[ "comment" - ] = "Failed to enforce group ownership for group {}".format(group) + ] = f"Failed to enforce group ownership for group {group}" else: ret["result"] = False ret["comment"] = ( @@ -4148,24 +4134,24 @@ def directory( removed = _clean_dir(name, list(keep), exclude_pat) if removed: ret["changes"]["removed"] = removed - ret["comment"] = "Files cleaned from directory {}".format(name) + ret["comment"] = f"Files cleaned from directory {name}" # issue 32707: reflect children_only selection in comments if not ret["comment"]: if children_only: - ret["comment"] = "Directory {}/* updated".format(name) + ret["comment"] = f"Directory {name}/* updated" else: if ret["changes"]: - ret["comment"] = "Directory {} updated".format(name) + ret["comment"] = f"Directory {name} updated" if __opts__["test"]: - ret["comment"] = "Directory {} not updated".format(name) + ret["comment"] = f"Directory {name} not updated" elif not ret["changes"] and ret["result"]: orig_comment = None if ret["comment"]: orig_comment = ret["comment"] - ret["comment"] = "Directory {} is in the correct state".format(name) + ret["comment"] = f"Directory {name} is in the correct state" if orig_comment: ret["comment"] = "\n".join([ret["comment"], orig_comment]) @@ -4173,7 +4159,7 @@ def directory( ret["result"] = False ret["comment"] += "\n\nThe following errors were encountered:\n" for error in errors: - ret["comment"] += "\n- {}".format(error) + ret["comment"] += f"\n- {error}" return ret @@ -4204,7 +4190,7 @@ def recurse( win_perms=None, win_deny_perms=None, win_inheritance=True, - **kwargs + **kwargs, ): """ Recurse through a subdirectory on the master and copy said subdirectory @@ -4465,7 +4451,7 @@ def recurse( # The specified user or group do not exist return _error(ret, u_check) if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") # expand source into source_list source_list = _validate_str_list(source) @@ -4477,7 +4463,7 @@ def recurse( if not precheck.startswith("salt://"): return _error( ret, - "Invalid source '{}' (must be a salt:// URI)".format(precheck), + f"Invalid source '{precheck}' (must be a salt:// URI)", ) # Select the first source in source_list that exists @@ -4485,7 +4471,7 @@ def recurse( source, source_hash = __salt__["file.source_list"](source_list, "", __env__) except CommandExecutionError as exc: ret["result"] = False - ret["comment"] = "Recurse failed: {}".format(exc) + ret["comment"] = f"Recurse failed: {exc}" return ret # Check source path relative to fileserver root, make sure it is a @@ -4508,7 +4494,7 @@ def recurse( if not os.path.isdir(name): if os.path.exists(name): # it is not a dir, but it exists - fail out - return _error(ret, "The path {} exists and is not a directory".format(name)) + return _error(ret, f"The path {name} exists and is not a directory") if not __opts__["test"]: if salt.utils.platform.is_windows(): win_owner = win_owner if win_owner else user @@ -4547,7 +4533,7 @@ def recurse( if clean and os.path.exists(path) and os.path.isdir(path) and replace: _ret = {"name": name, "changes": {}, "result": True, "comment": ""} if __opts__["test"]: - _ret["comment"] = "Replacing directory {} with a file".format(path) + _ret["comment"] = f"Replacing directory {path} with a file" _ret["result"] = None merge_ret(path, _ret) return @@ -4577,7 +4563,7 @@ def recurse( context=context, defaults=defaults, backup=backup, - **pass_kwargs + **pass_kwargs, ) merge_ret(path, _ret) @@ -4587,7 +4573,7 @@ def recurse( if clean and os.path.exists(path) and not os.path.isdir(path): _ret = {"name": name, "changes": {}, "result": True, "comment": ""} if __opts__["test"]: - _ret["comment"] = "Replacing {} with a directory".format(path) + _ret["comment"] = f"Replacing {path} with a directory" _ret["result"] = None merge_ret(path, _ret) return @@ -4651,10 +4637,10 @@ def recurse( ).strip() if not ret["comment"]: - ret["comment"] = "Recursively updated {}".format(name) + ret["comment"] = f"Recursively updated {name}" if not ret["changes"] and ret["result"]: - ret["comment"] = "The directory {} is in the correct state".format(name) + ret["comment"] = f"The directory {name} is in the correct state" return ret @@ -5183,7 +5169,7 @@ def line( if mode not in modeswithemptycontent and content is None: return _error( ret, - "Content can only be empty if mode is {}".format(modeswithemptycontent), + f"Content can only be empty if mode is {modeswithemptycontent}", ) del modeswithemptycontent @@ -5589,11 +5575,11 @@ def keyvalue( append_if_not_found = True file_contents = [] else: - ret["comment"] = "unable to open {n}".format(n=name) + ret["comment"] = f"unable to open {name}" ret["result"] = True if ignore_if_missing else False return ret except OSError as exc: - ret["comment"] = "unable to open {n}".format(n=name) + ret["comment"] = f"unable to open {name}" ret["result"] = True if ignore_if_missing else False return ret @@ -5682,7 +5668,7 @@ def keyvalue( # the old line always needs to go, so that will be # reflected in the diff (this is the original line from # the file being read) - diff.append("- {}".format(line)) + diff.append(f"- {line}") line = line[:0] # any non-zero value means something needs to go back in @@ -5705,7 +5691,7 @@ def keyvalue( ) ) else: - diff.append("+ {}".format(line)) + diff.append(f"+ {line}") changes += 1 # subtract one from the count if it was larger than 0, so # next lines are removed. if it is less than 0 then count is @@ -5728,7 +5714,7 @@ def keyvalue( for key, value in key_values.items(): if diff_count[key] > 0: line = tmpl.format(key=key, sep=separator, value=value) - tmpdiff.append("+ {}".format(line)) + tmpdiff.append(f"+ {line}") content.append(line) changes += 1 if tmpdiff: @@ -5746,7 +5732,7 @@ def keyvalue( if not did_diff: diff.insert(0, " " + os.linesep) did_diff = True - diff.insert(1, "+ {}".format(line)) + diff.insert(1, f"+ {line}") content.insert(0, line) changes += 1 @@ -5772,7 +5758,7 @@ def keyvalue( # otherwise return the actual diff lines else: - ret["comment"] = "Changed {c} lines".format(c=changes) + ret["comment"] = f"Changed {changes} lines" if show_changes: ret["changes"]["diff"] = "".join(diff) else: @@ -5788,7 +5774,7 @@ def keyvalue( fd.close() except OSError: # return an error if the file was not writable - ret["comment"] = "{n} not writable".format(n=name) + ret["comment"] = f"{name} not writable" ret["result"] = False return ret # if all went well, then set result to true @@ -6094,7 +6080,7 @@ def blockreplace( log.exception("Encountered error managing block") ret[ "comment" - ] = "Encountered error managing block: {}. See the log for details.".format(exc) + ] = f"Encountered error managing block: {exc}. See the log for details." return ret if changes: @@ -6170,7 +6156,7 @@ def comment(name, regex, char="#", backup=".bak", ignore_missing=False): # remove (?i)-like flags, ^ and $ unanchor_regex = re.sub(r"^(\(\?[iLmsux]\))?\^?(.*?)\$?$", r"\2", regex) - uncomment_regex = r"^(?!\s*{}).*".format(char) + unanchor_regex + uncomment_regex = rf"^(?!\s*{char}).*" + unanchor_regex comment_regex = char + unanchor_regex # Make sure the pattern appears in the file before continuing @@ -6184,11 +6170,11 @@ def comment(name, regex, char="#", backup=".bak", ignore_missing=False): ret["result"] = True return ret else: - return _error(ret, "{}: Pattern not found".format(unanchor_regex)) + return _error(ret, f"{unanchor_regex}: Pattern not found") if __opts__["test"]: ret["changes"][name] = "updated" - ret["comment"] = "File {} is set to be updated".format(name) + ret["comment"] = f"File {name} is set to be updated" ret["result"] = None return ret @@ -6282,11 +6268,11 @@ def uncomment(name, regex, char="#", backup=".bak"): ret["result"] = True return ret else: - return _error(ret, "{}: Pattern not found".format(regex)) + return _error(ret, f"{regex}: Pattern not found") if __opts__["test"]: ret["changes"][name] = "updated" - ret["comment"] = "File {} is set to be updated".format(name) + ret["comment"] = f"File {name} is set to be updated" ret["result"] = None return ret @@ -6489,14 +6475,14 @@ def append( if makedirs is True: dirname = os.path.dirname(name) if __opts__["test"]: - ret["comment"] = "Directory {} is set to be updated".format(dirname) + ret["comment"] = f"Directory {dirname} is set to be updated" ret["result"] = None else: if not __salt__["file.directory_exists"](dirname): try: _makedirs(name=name) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") check_res, check_msg, check_changes = ( _check_directory_win(dirname) @@ -6548,13 +6534,13 @@ def append( continue for line_item in chunk.splitlines(): - append_lines.append("{}".format(line_item)) + append_lines.append(f"{line_item}") except TypeError: return _error(ret, "No text found to append. Nothing appended") if __opts__["test"]: - ret["comment"] = "File {} is set to be updated".format(name) + ret["comment"] = f"File {name} is set to be updated" ret["result"] = None nlines = list(slines) nlines.extend(append_lines) @@ -6565,15 +6551,15 @@ def append( # Changes happened, add them ret["changes"]["diff"] = "\n".join(difflib.unified_diff(slines, nlines)) else: - ret["comment"] = "File {} is in correct state".format(name) + ret["comment"] = f"File {name} is in correct state" ret["result"] = True return ret if append_lines: __salt__["file.append"](name, args=append_lines) - ret["comment"] = "Appended {} lines".format(len(append_lines)) + ret["comment"] = f"Appended {len(append_lines)} lines" else: - ret["comment"] = "File {} is in correct state".format(name) + ret["comment"] = f"File {name} is in correct state" with salt.utils.files.fopen(name, "rb") as fp_: nlines = fp_.read() @@ -6774,14 +6760,14 @@ def prepend( if makedirs is True: dirname = os.path.dirname(name) if __opts__["test"]: - ret["comment"] = "Directory {} is set to be updated".format(dirname) + ret["comment"] = f"Directory {dirname} is set to be updated" ret["result"] = None else: if not __salt__["file.directory_exists"](dirname): try: _makedirs(name=name) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") check_res, check_msg, check_changes = ( _check_directory_win(dirname) @@ -6838,9 +6824,9 @@ def prepend( for line in lines: if __opts__["test"]: - ret["comment"] = "File {} is set to be updated".format(name) + ret["comment"] = f"File {name} is set to be updated" ret["result"] = None - test_lines.append("{}\n".format(line)) + test_lines.append(f"{line}\n") else: preface.append(line) count += 1 @@ -6855,7 +6841,7 @@ def prepend( ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines)) ret["result"] = None else: - ret["comment"] = "File {} is in correct state".format(name) + ret["comment"] = f"File {name} is in correct state" ret["result"] = True return ret @@ -6894,9 +6880,9 @@ def prepend( ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines)) if count: - ret["comment"] = "Prepended {} lines".format(count) + ret["comment"] = f"Prepended {count} lines" else: - ret["comment"] = "File {} is in correct state".format(name) + ret["comment"] = f"File {name} is in correct state" ret["result"] = True return ret @@ -6914,7 +6900,7 @@ def patch( reject_file=None, strip=None, saltenv=None, - **kwargs + **kwargs, ): """ Ensure that a patch has been applied to the specified file or directory @@ -7055,14 +7041,14 @@ def patch( try: name = os.path.expanduser(name) except Exception: # pylint: disable=broad-except - ret["comment"] = "Invalid path '{}'".format(name) + ret["comment"] = f"Invalid path '{name}'" return ret else: if not os.path.isabs(name): - ret["comment"] = "{} is not an absolute path".format(name) + ret["comment"] = f"{name} is not an absolute path" return ret elif not os.path.exists(name): - ret["comment"] = "{} does not exist".format(name) + ret["comment"] = f"{name} does not exist" return ret else: is_dir = os.path.isdir(name) @@ -7079,11 +7065,11 @@ def patch( try: reject_file_parent = os.path.dirname(reject_file) except Exception: # pylint: disable=broad-except - ret["comment"] = "Invalid path '{}' for reject_file".format(reject_file) + ret["comment"] = f"Invalid path '{reject_file}' for reject_file" return ret else: if not os.path.isabs(reject_file_parent): - ret["comment"] = "'{}' is not an absolute path".format(reject_file) + ret["comment"] = f"'{reject_file}' is not an absolute path" return ret elif not os.path.isdir(reject_file_parent): ret["comment"] = ( @@ -7183,7 +7169,7 @@ def patch( "included in the source URL." ) else: - source_match += "?saltenv={}".format(saltenv) + source_match += f"?saltenv={saltenv}" cleanup = [] @@ -7256,7 +7242,7 @@ def patch( # then remove. patch_opts = ["-N", "-r", patch_rejects, "-o", patch_output] if is_dir and strip is not None: - patch_opts.append("-p{}".format(strip)) + patch_opts.append(f"-p{strip}") pre_check = _patch(patch_file, patch_opts) if pre_check["retcode"] != 0: @@ -7307,7 +7293,7 @@ def patch( # If we've made it here, the patch should apply cleanly patch_opts = [] if is_dir and strip is not None: - patch_opts.append("-p{}".format(strip)) + patch_opts.append(f"-p{strip}") ret["changes"] = _patch(patch_file, patch_opts) if ret["changes"]["retcode"] == 0: @@ -7372,7 +7358,7 @@ def touch(name, atime=None, mtime=None, makedirs=False): if not name: return _error(ret, "Must provide name to file.touch") if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if __opts__["test"]: ret.update(_check_touch(name, atime, mtime)) @@ -7382,15 +7368,15 @@ def touch(name, atime=None, mtime=None, makedirs=False): try: _makedirs(name=name) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") if not os.path.isdir(os.path.dirname(name)): - return _error(ret, "Directory not present to touch file {}".format(name)) + return _error(ret, f"Directory not present to touch file {name}") extant = os.path.exists(name) ret["result"] = __salt__["file.touch"](name, atime, mtime) if not extant and ret["result"]: - ret["comment"] = "Created empty file {}".format(name) + ret["comment"] = f"Created empty file {name}" ret["changes"]["new"] = name elif extant and ret["result"]: ret["comment"] = "Updated times on {} {}".format( @@ -7412,7 +7398,7 @@ def copy_( mode=None, dir_mode=None, subdir=False, - **kwargs + **kwargs, ): """ If the file defined by the ``source`` option exists on the minion, copy it @@ -7507,7 +7493,7 @@ def copy_( ret = { "name": name, "changes": {}, - "comment": 'Copied "{}" to "{}"'.format(source, name), + "comment": f'Copied "{source}" to "{name}"', "result": True, } if not name: @@ -7515,10 +7501,10 @@ def copy_( changed = True if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if not os.path.exists(source): - return _error(ret, 'Source file "{}" is not present'.format(source)) + return _error(ret, f'Source file "{source}" is not present') if preserve: user = __salt__["file.get_user"](source) @@ -7577,7 +7563,7 @@ def copy_( except OSError: return _error( ret, - 'Failed to delete "{}" in preparation for forced move'.format(name), + f'Failed to delete "{name}" in preparation for forced move', ) if __opts__["test"]: @@ -7589,14 +7575,12 @@ def copy_( else: ret[ "comment" - ] = 'The target file "{}" exists and will not be overwritten'.format(name) + ] = f'The target file "{name}" exists and will not be overwritten' ret["result"] = True return ret if not changed: - ret[ - "comment" - ] = 'The target file "{}" exists and will not be overwritten'.format(name) + ret["comment"] = f'The target file "{name}" exists and will not be overwritten' ret["result"] = True return ret @@ -7617,9 +7601,9 @@ def copy_( try: _makedirs(name=name, user=user, group=group, dir_mode=dir_mode) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: - return _error(ret, "The target directory {} is not present".format(dname)) + return _error(ret, f"The target directory {dname} is not present") # All tests pass, move the file into place try: if os.path.isdir(source): @@ -7647,7 +7631,7 @@ def copy_( ret["result"] = check_ret["result"] ret["comment"] = check_ret["comment"] except OSError: - return _error(ret, 'Failed to copy "{}" to "{}"'.format(source, name)) + return _error(ret, f'Failed to copy "{source}" to "{name}"') return ret @@ -7681,7 +7665,7 @@ def rename(name, source, force=False, makedirs=False, **kwargs): return _error(ret, "Must provide name to file.rename") if not os.path.isabs(name): - return _error(ret, "Specified file {} is not an absolute path".format(name)) + return _error(ret, f"Specified file {name} is not an absolute path") if not os.path.lexists(source): ret["comment"] = 'Source file "{}" has already been moved out of place'.format( @@ -7693,7 +7677,7 @@ def rename(name, source, force=False, makedirs=False, **kwargs): if not force: ret[ "comment" - ] = 'The target file "{}" exists and will not be overwritten'.format(name) + ] = f'The target file "{name}" exists and will not be overwritten' return ret elif not __opts__["test"]: # Remove the destination to prevent problems later @@ -7702,11 +7686,11 @@ def rename(name, source, force=False, makedirs=False, **kwargs): except OSError: return _error( ret, - 'Failed to delete "{}" in preparation for forced move'.format(name), + f'Failed to delete "{name}" in preparation for forced move', ) if __opts__["test"]: - ret["comment"] = 'File "{}" is set to be moved to "{}"'.format(source, name) + ret["comment"] = f'File "{source}" is set to be moved to "{name}"' ret["result"] = None return ret @@ -7717,9 +7701,9 @@ def rename(name, source, force=False, makedirs=False, **kwargs): try: _makedirs(name=name) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: - return _error(ret, "The target directory {} is not present".format(dname)) + return _error(ret, f"The target directory {dname} is not present") # All tests pass, move the file into place try: if os.path.islink(source): @@ -7729,9 +7713,9 @@ def rename(name, source, force=False, makedirs=False, **kwargs): else: shutil.move(source, name) except OSError: - return _error(ret, 'Failed to move "{}" to "{}"'.format(source, name)) + return _error(ret, f'Failed to move "{source}" to "{name}"') - ret["comment"] = 'Moved "{}" to "{}"'.format(source, name) + ret["comment"] = f'Moved "{source}" to "{name}"' ret["changes"] = {name: source} return ret @@ -7854,7 +7838,7 @@ def serialize( serializer=None, serializer_opts=None, deserializer_opts=None, - **kwargs + **kwargs, ): """ Serializes dataset and store it into managed file. Useful for sharing @@ -8061,9 +8045,7 @@ def serialize( if not create: if not os.path.isfile(name): # Don't create a file that is not already present - ret[ - "comment" - ] = "File {} is not present and is not set for creation".format(name) + ret["comment"] = f"File {name} is not present and is not set for creation" return ret formatter = kwargs.pop("formatter", None) @@ -8089,8 +8071,8 @@ def serialize( ) group = user - serializer_name = "{}.serialize".format(serializer) - deserializer_name = "{}.deserialize".format(serializer) + serializer_name = f"{serializer}.serialize" + deserializer_name = f"{serializer}.deserialize" if serializer_name not in __serializers__: return { @@ -8148,7 +8130,7 @@ def serialize( ) if existing_data == merged_data: ret["result"] = True - ret["comment"] = "The file {} is in the correct state".format(name) + ret["comment"] = f"The file {name} is in the correct state" return ret dataset = merged_data else: @@ -8190,7 +8172,7 @@ def serialize( saltenv=__env__, contents=contents, skip_verify=False, - **kwargs + **kwargs, ) if ret["changes"]: @@ -8203,7 +8185,7 @@ def serialize( ret["changes"]["diff"] = "" else: ret["result"] = True - ret["comment"] = "The file {} is in the correct state".format(name) + ret["comment"] = f"The file {name} is in the correct state" else: ret = __salt__["file.manage_file"]( name=name, @@ -8320,7 +8302,7 @@ def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"): # Check if it is a character device elif not __salt__["file.is_chrdev"](name): if __opts__["test"]: - ret["comment"] = "Character device {} is set to be created".format(name) + ret["comment"] = f"Character device {name} is set to be created" ret["result"] = None else: ret = __salt__["file.mknod"]( @@ -8341,9 +8323,7 @@ def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"): else: ret = __salt__["file.check_perms"](name, None, user, group, mode)[0] if not ret["changes"]: - ret[ - "comment" - ] = "Character device {} is in the correct state".format(name) + ret["comment"] = f"Character device {name} is in the correct state" elif ntype == "b": # Check for file existence @@ -8357,7 +8337,7 @@ def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"): # Check if it is a block device elif not __salt__["file.is_blkdev"](name): if __opts__["test"]: - ret["comment"] = "Block device {} is set to be created".format(name) + ret["comment"] = f"Block device {name} is set to be created" ret["result"] = None else: ret = __salt__["file.mknod"]( @@ -8392,7 +8372,7 @@ def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"): # Check if it is a fifo elif not __salt__["file.is_fifo"](name): if __opts__["test"]: - ret["comment"] = "Fifo pipe {} is set to be created".format(name) + ret["comment"] = f"Fifo pipe {name} is set to be created" ret["result"] = None else: ret = __salt__["file.mknod"]( @@ -8403,7 +8383,7 @@ def mknod(name, ntype, major=0, minor=0, user=None, group=None, mode="0600"): else: ret = __salt__["file.check_perms"](name, None, user, group, mode)[0] if not ret["changes"]: - ret["comment"] = "Fifo pipe {} is in the correct state".format(name) + ret["comment"] = f"Fifo pipe {name} is in the correct state" else: ret["comment"] = ( @@ -8423,7 +8403,7 @@ def mod_run_check_cmd(cmd, filename, **check_cmd_opts): """ log.debug("running our check_cmd") - _cmd = "{} {}".format(cmd, filename) + _cmd = f"{cmd} {filename}" cret = __salt__["cmd.run_all"](_cmd, **check_cmd_opts) if cret["retcode"] != 0: ret = { @@ -8571,7 +8551,7 @@ def shortcut( backupname=None, makedirs=False, user=None, - **kwargs + **kwargs, ): """ Create a Windows shortcut @@ -8666,12 +8646,10 @@ def shortcut( uid = __salt__["file.user_to_uid"](user) if uid == "": - preflight_errors.append("User {} does not exist".format(user)) + preflight_errors.append(f"User {user} does not exist") if not os.path.isabs(name): - preflight_errors.append( - "Specified file {} is not an absolute path".format(name) - ) + preflight_errors.append(f"Specified file {name} is not an absolute path") if preflight_errors: msg = ". ".join(preflight_errors) @@ -8693,7 +8671,7 @@ def shortcut( try: _makedirs(name=name, user=user) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: return _error( ret, @@ -8722,7 +8700,7 @@ def shortcut( try: _makedirs(name=backupname) except CommandExecutionError as exc: - return _error(ret, "Drive {} is not mapped".format(exc.message)) + return _error(ret, f"Drive {exc.message} is not mapped") else: return _error( ret, @@ -8780,7 +8758,7 @@ def shortcut( ret["comment"] = "Set ownership of shortcut {} to {}".format( name, user ) - ret["changes"]["ownership"] = "{}".format(user) + ret["changes"]["ownership"] = f"{user}" else: ret["result"] = False ret[ @@ -8810,7 +8788,7 @@ def shortcut( ) return ret else: - ret["comment"] = "Created new shortcut {} -> {}".format(name, target) + ret["comment"] = f"Created new shortcut {name} -> {target}" ret["changes"]["new"] = name if not _check_shortcut_ownership(name, user): @@ -8978,15 +8956,13 @@ def cached( if source_sum: hash = __salt__["file.get_hash"](local_copy, __opts__["hash_type"]) if hash == source_sum["hsum"]: - ret["comment"] = "File already cached: {}".format(name) + ret["comment"] = f"File already cached: {name}" else: - ret[ - "comment" - ] = "Hashes don't match.\nFile will be cached: {}".format(name) + ret["comment"] = f"Hashes don't match.\nFile will be cached: {name}" else: - ret["comment"] = "No hash found. File will be cached: {}".format(name) + ret["comment"] = f"No hash found. File will be cached: {name}" else: - ret["comment"] = "File will be cached: {}".format(name) + ret["comment"] = f"File will be cached: {name}" ret["changes"] = {} ret["result"] = None return ret @@ -9017,10 +8993,10 @@ def cached( return ret else: ret["result"] = True - ret["comment"] = "File {} is present on the minion".format(full_path) + ret["comment"] = f"File {full_path} is present on the minion" return ret else: - ret["comment"] = "File {} is not present on the minion".format(full_path) + ret["comment"] = f"File {full_path} is not present on the minion" return ret local_copy = __salt__["cp.is_cached"](name, saltenv=saltenv) @@ -9098,7 +9074,7 @@ def cached( # We're not enforcing a hash, and we already know that the file was # successfully cached, so we know the state was successful. ret["result"] = True - ret["comment"] = "File is cached to {}".format(local_copy) + ret["comment"] = f"File is cached to {local_copy}" return ret @@ -9146,14 +9122,14 @@ def not_cached(name, saltenv="base"): try: os.remove(local_copy) except Exception as exc: # pylint: disable=broad-except - ret["comment"] = "Failed to delete {}: {}".format(local_copy, exc.__str__()) + ret["comment"] = f"Failed to delete {local_copy}: {exc.__str__()}" else: ret["result"] = True ret["changes"]["deleted"] = True - ret["comment"] = "{} was deleted".format(local_copy) + ret["comment"] = f"{local_copy} was deleted" else: ret["result"] = True - ret["comment"] = "{} is not cached".format(name) + ret["comment"] = f"{name} is not cached" return ret @@ -9184,7 +9160,7 @@ def mod_beacon(name, **kwargs): data["recurse"] = _beacon_data.get("recurse", True) data["exclude"] = _beacon_data.get("exclude", []) - beacon_name = "beacon_{}_{}".format(beacon_module, name) + beacon_name = f"beacon_{beacon_module}_{name}" beacon_kwargs = { "name": beacon_name, "files": {name: data}, @@ -9253,7 +9229,7 @@ def pruned(name, recurse=False, ignore_errors=False, older_than=None): if __opts__["test"]: ret["result"] = None ret["changes"]["deleted"] = name - ret["comment"] = "Directory {} is set for removal".format(name) + ret["comment"] = f"Directory {name} is set for removal" return ret res = __salt__["file.rmdir"]( @@ -9263,12 +9239,10 @@ def pruned(name, recurse=False, ignore_errors=False, older_than=None): if result: if recurse and res["deleted"]: - ret[ - "comment" - ] = "Recursively removed empty directories under {}".format(name) + ret["comment"] = f"Recursively removed empty directories under {name}" ret["changes"]["deleted"] = sorted(res["deleted"]) elif not recurse: - ret["comment"] = "Removed directory {}".format(name) + ret["comment"] = f"Removed directory {name}" ret["changes"]["deleted"] = name return ret elif ignore_errors and res["deleted"]: @@ -9280,8 +9254,8 @@ def pruned(name, recurse=False, ignore_errors=False, older_than=None): ret["result"] = result ret["changes"] = res - ret["comment"] = "Failed to remove directory {}".format(name) + ret["comment"] = f"Failed to remove directory {name}" return ret - ret["comment"] = "Directory {} is not present".format(name) + ret["comment"] = f"Directory {name} is not present" return ret