diff --git a/workbench b/workbench index 05f3330..9d20a13 100755 --- a/workbench +++ b/workbench @@ -48,8 +48,9 @@ def create(): logging.info(message) path_to_rollback_csv_file = get_rollback_csv_filepath(config) + write_rollback_config(config, path_to_rollback_csv_file) prep_rollback_csv(config, path_to_rollback_csv_file) - logging.info("Writing rollback CSV to " + path_to_rollback_csv_file) + logging.info(f"Writing rollback CSV to {path_to_rollback_csv_file}.") prepare_csv_id_to_node_id_map(config) @@ -471,8 +472,6 @@ def create(): if "url_alias" in row and len(row["url_alias"]) > 0: create_url_alias(config, node_id, row["url_alias"]) - write_rollback_config(config, path_to_rollback_csv_file) - # If the file named in 'file' can't be found. if "file" in row and len(row["file"].strip()) > 0: if ( @@ -2265,8 +2264,9 @@ def create_from_files(): files = os.listdir(file_dir_path) path_to_rollback_csv_file = get_rollback_csv_filepath(config) + write_rollback_config(config, path_to_rollback_csv_file) prep_rollback_csv(config, path_to_rollback_csv_file) - logging.info("Writing rollback CSV to " + path_to_rollback_csv_file) + logging.info(f"Writing rollback CSV to {path_to_rollback_csv_file}.") num_files = len(files) file_count = 0 diff --git a/workbench_utils.py b/workbench_utils.py index e3974f8..428ef56 100644 --- a/workbench_utils.py +++ b/workbench_utils.py @@ -8955,16 +8955,15 @@ def get_rollback_csv_filepath(config): ) except Exception as e: # We need to account for the very common case where the user has included "valid identifier characters" - # (as defined in https://peps.python.org/pep-0292/) as part of their template. The most common instance - # will likely be underscores separating the template placeholders. - if "config_filename" in str(e) or "input_csv_filename" in str(e): - message = f"One or more parts of the configured rollback filename template needs adjusting: {e}" - else: - message = f'One or more parts of the configured rollback filename template ({config["rollback_csv_filename_template"]}) need adjusting.' - logging.error() - sys.exit( - f"Error: {message}. Please refer to the Workbench documentation for suggestions." - ) + # (as defined in https://peps.python.org/pep-0292/) as part of their template. The most common case will + # likely be underscores separating the template placeholders. + message = f'One or more parts of the configured rollback csv filename template ({config["rollback_csv_filename_template"]}) need adjusting.' + logging.error( + f"{message} A {e.__class__.__name__} exception occured with the error message {e}. Please refer to the Workbench documentation for suggestions." + ) + sys.exit( + f"Error: {message} Please refer to your Workbench log and to the Workbench documentation for suggestions." + ) else: rollback_csv_filename_basename = "rollback" @@ -8986,12 +8985,45 @@ def get_rollback_csv_filepath(config): def write_rollback_config(config, path_to_rollback_csv_file): + if "rollback_config_filename_template" in config: + config_filename, task_config_ext = os.path.splitext(config["config_file"]) + input_csv_filename, input_csv_ext = os.path.splitext(config["input_csv"]) + + rollback_config_filename_template = string.Template( + config["rollback_config_filename_template"] + ) + try: + rollback_config_filename_basename = str( + rollback_config_filename_template.substitute( + { + "config_filename": config_filename, + "input_csv_filename": input_csv_filename, + } + ) + ) + except Exception as e: + # We need to account for the very common case where the user has included "valid identifier characters" + # (as defined in https://peps.python.org/pep-0292/) as part of their template. The most common case will + # likely be underscores separating the template placeholders. + message = f'One or more parts of the configured rollback configuration filename template ({config["rollback_config_filename_template"]}) need adjusting.' + logging.error( + f"{message} A {e.__class__.__name__} exception occured with the error message {e}. Please refer to the Workbench documentation for suggestions." + ) + sys.exit( + f"Error: {message} Please refer to your Workbench log and to the Workbench documentation for suggestions." + ) + else: + rollback_config_filename_basename = "rollback" + if config["timestamp_rollback"] is True: now_string = EXECUTION_START_TIME.strftime("%Y_%m_%d_%H_%M_%S") - rollback_config_filename = "rollback." + now_string + ".yml" + rollback_config_filename = ( + f"{rollback_config_filename_basename}.{now_string}.yml" + ) else: - rollback_config_filename = "rollback.yml" + rollback_config_filename = f"{rollback_config_filename_basename}.yml" + logging.info(f"Writing rollback configuration file to {rollback_config_filename}.") rollback_config_file = open(rollback_config_filename, "w") rollback_comments = get_rollback_config_comments(config) rollback_config_file.write(rollback_comments) @@ -9037,17 +9069,22 @@ def write_rollback_node_id(config, node_id, path_to_rollback_csv_file): def get_rollback_config_comments(config): + comments = list() task = config["task"] config_file = config["config_file"] - time_string = now_string = EXECUTION_START_TIME.strftime("%Y:%m:%d %H:%M:%S") input_csv = config["input_csv"] - comments = ( - f'# Generated by a "{task}" task started {time_string} using' - + "\n" - + f'# config file "{config_file}" and input CSV "{input_csv}".' - + "\n" - ) - return comments + time_string = now_string = EXECUTION_START_TIME.strftime("%Y:%m:%d %H:%M:%S") + + comments.append(f'# Generated by a "{task}" task started {time_string} using') + comments.append(f'config file "{config_file}" and input CSV "{input_csv}".') + if ( + "rollback_file_comments" in config + and config["rollback_file_comments"] is not None + and len(config["rollback_file_comments"]) > 0 + ): + comments.extend(config["rollback_file_comments"]) + + return "\n# ".join(comments) + "\n" def get_csv_from_google_sheet(config):