Skip to content

Commit

Permalink
Resolves #855.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Dec 1, 2024
1 parent d1b6ea4 commit f501290
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
1 change: 1 addition & 0 deletions WorkbenchConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def get_default_config(self):
"remind_user_to_run_check": False,
"media_type_by_media_use": False,
"paged_content_ignore_files": ["Thumbs.db"],
"include_password_in_rollback_config_file": False,
}

# Tests validity and existence of configuration file path.
Expand Down
17 changes: 17 additions & 0 deletions workbench
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def create():
print(message)
logging.info(message)

check_rollback_file_path_directories(config)

if config["ignore_existing_parent_ids"] is True:
workbench_execution_start_time = "{:%Y-%m-%d %H:%M:%S}".format(
datetime.datetime.now()
Expand Down Expand Up @@ -2260,6 +2262,8 @@ def create_from_files():
print(message)
logging.info(message)

check_rollback_file_path_directories(config)

prepare_csv_id_to_node_id_map(config)

file_dir_path = config["input_dir"]
Expand Down Expand Up @@ -3495,6 +3499,14 @@ parser.add_argument(
"--log_file_path",
help="Overrides the 'log_file_path' configuration setting.",
)
parser.add_argument(
"--rollback_config_file_path",
help="Overrides the 'rollback_config_file_path' configuration setting.",
)
parser.add_argument(
"--rollback_csv_file_path",
help="Overrides the 'rollback_csv_file_path' configuration setting.",
)
parser.add_argument("--version", action="version", version="Islandora Workbench 0.0.0")
args = parser.parse_args()

Expand All @@ -3514,6 +3526,7 @@ except Exception as e:
sys.exit(str(e))
config = workbench_config.get_config()

# Override configuration settings that are provided as command-line arguments.
if args.input_csv is not None:
config["input_csv"] = args.input_csv
if args.input_csv is not None:
Expand All @@ -3522,6 +3535,10 @@ if args.input_dir is not None:
config["input_dir"] = args.input_dir
if args.log_file_path is not None:
config["log_file_path"] = args.log_file_path
if args.rollback_config_file_path is not None:
config["rollback_config_file_path"] = args.rollback_config_file_path
if args.rollback_csv_file_path is not None:
config["rollback_csv_file_path"] = args.rollback_csv_file_path

if args.print_config is True:
print("\nCurrent Islandora Workbench configuration, including defaults")
Expand Down
106 changes: 97 additions & 9 deletions workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,10 @@ def check_input(config, args):
print(message)
logging.info(message)

# Check that the rollback configuration file and CSV file directories exist and are writable.
if config["task"] in ["create", "create_from_files"]:
check_rollback_file_path_directories(config)

create_temp_dir(config)

# Perform checks on get_data_from_view tasks. Since this task doesn't use input_dir, input_csv, etc.,
Expand Down Expand Up @@ -3891,6 +3895,26 @@ def check_input(config, args):
sys.exit(0)


def check_rollback_file_path_directories(config):
rollback_config_file_path = get_rollback_config_filepath(config)
rollback_config_file_path_head, rollback_config_file_path_tail = os.path.split(
rollback_config_file_path
)
if not os.access(rollback_config_file_path_head, os.W_OK):
message = f'Directory "{rollback_config_file_path_head}" in the rollback configuration file path does not exist or is not writable.'
logging.error(message)
sys.exit("Error: " + message)

rollback_csv_file_path = get_rollback_csv_filepath(config)
rollback_csv_file_path_head, rollback_csv_file_path_tail = os.path.split(
rollback_csv_file_path
)
if not os.access(rollback_csv_file_path_head, os.W_OK):
message = f'Directory "{rollback_csv_file_path_head}" in the rollback CSV file path does not exist or is not writable.'
logging.error(message)
sys.exit("Error: " + message)


def get_registered_media_extensions(config, media_bundle, field_name_filter=None):
"""For the given media bundle, gets a list of file extensions registered in Drupal's
"Allowed file extensions" configuration for each field that has this setting.
Expand Down Expand Up @@ -9004,6 +9028,8 @@ def get_rollback_csv_filepath(config):

if config["timestamp_rollback"] is True:
now_string = EXECUTION_START_TIME.strftime("%Y_%m_%d_%H_%M_%S")

if config["timestamp_rollback"] is True:
rollback_csv_filename = f"{rollback_csv_filename_basename}.{now_string}.csv"
else:
rollback_csv_filename = f"{rollback_csv_filename_basename}.csv"
Expand All @@ -9014,12 +9040,30 @@ def get_rollback_csv_filepath(config):
config_file_id = get_config_file_identifier(config)
rollback_csv_filename = rollback_csv_filename + "." + config_file_id

return os.path.join(
config["rollback_dir"] or config["input_dir"], rollback_csv_filename
)
if "rollback_csv_file_path" in config and len(config["rollback_csv_file_path"]) > 0:
if config["timestamp_rollback"] is True:
rollback_csv_file_path_head, rollback_csv_file_path_tail = os.path.split(
config["rollback_csv_file_path"]
)
rollback_csv_file_basename, rollback_csv_file_ext = os.path.splitext(
rollback_csv_file_path_tail
)
rollback_csv_file_path = os.path.join(
rollback_csv_file_path_head,
f"{rollback_csv_file_basename}.{now_string}{rollback_csv_file_ext}",
)
return os.path.abspath(rollback_csv_file_path)
else:
return os.path.abspath(config["rollback_csv_file_path"])
else:
return os.path.abspath(
os.path.join(
config["rollback_dir"] or config["input_dir"], rollback_csv_filename
)
)


def write_rollback_config(config, path_to_rollback_csv_file):
def get_rollback_config_filepath(config):
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"])
Expand Down Expand Up @@ -9050,28 +9094,72 @@ def write_rollback_config(config, path_to_rollback_csv_file):
else:
rollback_config_filename_basename = "rollback"

# Get workbench's current directory, to use as the default directory for the rollback config file.
# We only override this location if "rollback_config_file_path" is set in the config.
if "rollback_config_file_path" not in config:
rb_config_file_dir = sys.path[0]
else:
rb_config_file_dir = ""

if config["timestamp_rollback"] is True:
now_string = EXECUTION_START_TIME.strftime("%Y_%m_%d_%H_%M_%S")
rollback_config_filename = (
f"{rollback_config_filename_basename}.{now_string}.yml"

if config["timestamp_rollback"] is True:
rollback_config_filepath = os.path.join(
f"{rb_config_file_dir}",
f"{rollback_config_filename_basename}.{now_string}.yml",
)
else:
rollback_config_filename = f"{rollback_config_filename_basename}.yml"
rollback_config_filepath = os.path.join(
f"{rb_config_file_dir}", f"{rollback_config_filename_basename}.yml"
)

if (
"rollback_config_file_path" in config
and len(config["rollback_config_file_path"]) > 0
):
if config["timestamp_rollback"] is True:
rollback_config_file_path_head, rollback_config_file_path_tail = (
os.path.split(config["rollback_config_file_path"])
)
rollback_config_file_basename, rollback_config_file_ext = os.path.splitext(
rollback_config_file_path_tail
)
rollback_config_file_path = os.path.join(
rollback_config_file_path_head,
f"{rollback_config_file_basename}.{now_string}{rollback_config_file_ext}",
)
return os.path.abspath(rollback_config_file_path)
else:
rollback_config_filepath = os.path.abspath(
config["rollback_config_file_path"]
)

return rollback_config_filepath


def write_rollback_config(config, path_to_rollback_csv_file):
rollback_config_filename = get_rollback_config_filepath(config)

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)

if config["include_password_in_rollback_config_file"] is True:
password = config["password"]
else:
password = None

yaml.dump(
{
"task": "delete",
"host": config["host"],
"username": config["username"],
"password": config["password"],
"password": password,
"input_dir": config["input_dir"],
"standalone_media_url": config["standalone_media_url"],
"input_csv": os.path.basename(path_to_rollback_csv_file),
"input_csv": path_to_rollback_csv_file,
},
rollback_config_file,
)
Expand Down

0 comments on commit f501290

Please sign in to comment.