safe_removal_of_data

Functions

check_files_for_backup_local(files, ...)

Checks which files have been backed up locally (e.g., external drive).

check_files_for_backup_remote(files, ...)

Checks which files have been backed up on a remote server.

main(directory, days, safe, backup_dir, remote)

Main function to remove old data files from a directory.

parse_timestamp_from_filename(filename)

Parses a timestamp from a filename.

remove_file(file_path, removed_count)

Removes a single file and logs the action.

remove_old_data(directory, days, safe, ...)

Core logic to identify and remove old data files.

check_files_for_backup_remote(files: list[str], directory: str, backup_dir: str, remote_user: str, remote_host: str, port: int | None) list[str][source]

Checks which files have been backed up on a remote server.

Parameters:
  • files (list[str]) – List of filenames to check.

  • directory (str) – The local directory containing the files.

  • backup_dir (str) – The remote backup directory.

  • remote_user (str) – Username on remote system.

  • remote_host (str) – Remote hostname or IP.

  • port (int | None) – SSH port.

Returns:

List of full paths of files that can be safe to remove locally.

Return type:

list[str]

check_files_for_backup_local(files: list[str], directory: str, backup_dir: str) list[str][source]

Checks which files have been backed up locally (e.g., external drive).

Parameters:
  • files (list[str]) – List of filenames to check.

  • directory (str) – The local directory containing the source files.

  • backup_dir (str) – The local backup directory.

Returns:

List of full paths of files that can be safe to remove.

Return type:

list[str]

parse_timestamp_from_filename(filename: str) datetime | None[source]

Parses a timestamp from a filename.

Parameters:

filename (str) – The filename string.

Returns:

The parsed datetime object, or None if parsing fails.

Return type:

datetime | None

remove_old_data(directory: str, days: int, safe: bool, backup_dir: str, remote: bool, remote_user: str | None = None, remote_host: str | None = None, port: int | None = None) None[source]

Core logic to identify and remove old data files.

Parameters:
  • directory (str) – Directory to clean up.

  • days (int) – Retention period in days.

  • safe (bool) – If True, verifies backup before deletion.

  • backup_dir (str) – Backup directory path.

  • remote (bool) – If True, checks remote backup; otherwise local.

  • remote_user (str | None) – Remote username.

  • remote_host (str | None) – Remote host.

  • port (int | None) – SSH port.

remove_file(file_path: str, removed_count: int) int[source]

Removes a single file and logs the action.

Parameters:
  • file_path (str) – Path to the file.

  • removed_count (int) – Current count of removed files.

Returns:

Updated count of removed files.

Return type:

int

main(directory: str, days: int, safe: bool, backup_dir: str, remote: bool, remote_user: str | None = None, remote_host: str | None = None, port: int | None = None) None[source]

Main function to remove old data files from a directory.

Parameters:
  • directory (str) – Directory to clean up.

  • days (int) – Retention period in days.

  • safe (bool) – If True, verifies backup before deletion.

  • backup_dir (str) – Backup directory path.

  • remote (bool) – If True, checks remote backup; otherwise local.

  • remote_user (str | None) – Remote username.

  • remote_host (str | None) – Remote host.

  • port (int | None) – SSH port.