builder_peppermint_void/builder/core/move_iso.py
2025-04-25 12:38:42 +00:00

169 lines
6.3 KiB
Python

# -*- coding: utf-8 -*-
"""
SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team
(peppermintosteam@proton.me)
SPDX-License-Identifier: GPL-3.0-or-later
This module provides functions to move the built ISO to the download server,
calculate and create its checksum, rename the ISO, and remove old files from the server.
Credits:
- PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project.
License:
This code is distributed under the GNU General Public License version 3 or later (GPL-3.0-or-later).
For more details, please refer to the LICENSE file included in the project or visit:
https://www.gnu.org/licenses/gpl-3.0.html
"""
import os
import shutil
import hashlib
import datetime
import time
import sys
import logging
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, base_dir)
try:
from builder.configs import logger_config
from builder.core.bootstrap.paths import paths
except ImportError as e:
print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly. Details: {e}")
sys.exit(1)
logger = logger_config.setup_logger('move_iso')
def calculate_checksum(file_path):
"""Calculates the SHA256 checksum of a file."""
hasher = hashlib.sha256()
try:
with open(file_path, 'rb') as file:
while chunk := file.read(4096):
hasher.update(chunk)
except FileNotFoundError:
logger.error(f"File not found: {file_path}")
return None
return hasher.hexdigest()
def remove_old_files(directory, hours=24):
"""Removes files in a directory older than 'hours' hours."""
now = time.time()
cutoff = now - (hours * 3600) # hours * 60 * 60
removed_count = 0
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
try:
if os.path.isfile(file_path):
timestamp = os.path.getmtime(file_path)
if timestamp < cutoff:
os.remove(file_path)
logger.info(f"=> Removed old file: {file_path}")
removed_count += 1
except OSError as e:
logger.error(f"Error processing file {file_path}: {e}")
if removed_count > 0:
logger.info(f"=> {removed_count} files older than {hours} hours were removed from {directory}.")
else:
logger.info(f"=> No old files found to remove in {directory}.")
def find_iso_file(directory):
"""Finds the ISO file in the specified directory."""
iso_files = [f for f in os.listdir(directory) if f.endswith(".iso")]
if not iso_files:
logger.error(f"No ISO file found in: {directory}")
return None
elif len(iso_files) > 1:
logger.warning(f"Multiple ISO files found in: {directory}. Using the first one found: {iso_files[0]}")
return os.path.join(directory, iso_files[0])
def rename_iso_file(iso_path, arch, desktop, kernel_type, iso_label_prefix):
"""Renames the ISO file with the specified format."""
iso_dir = os.path.dirname(iso_path)
today_date = datetime.date.today().strftime('%Y%m%d')
new_iso_name = f"{iso_label_prefix}-{arch}-{desktop}-{kernel_type}-{today_date}.iso"
new_iso_path = os.path.join(iso_dir, new_iso_name)
try:
os.rename(iso_path, new_iso_path)
logger.info(f"=> ISO renamed to: {new_iso_path}")
return new_iso_path
except OSError as e:
logger.error(f"Error renaming the ISO file: {e}")
return None
def move_iso_to_server(iso_path, server_dir):
"""Moves the ISO file to the server directory."""
iso_name = os.path.basename(iso_path)
destination_path = os.path.join(server_dir, iso_name)
try:
shutil.move(iso_path, destination_path)
logger.info(f"=> ISO moved to the server: {destination_path}")
return destination_path
except OSError as e:
logger.error(f"Error moving the ISO to the server: {e}")
return None
def create_checksum_file(iso_path, server_dir):
"""Creates the checksum file for the ISO in the server directory."""
checksum = calculate_checksum(iso_path)
if checksum:
checksum_file_name = f"{os.path.basename(iso_path)}.sha256"
checksum_file_path = os.path.join(server_dir, checksum_file_name)
try:
with open(checksum_file_path, "w") as file:
file.write(checksum)
logger.info(f"=> Checksum file created at: {checksum_file_path}")
return True
except OSError as e:
logger.error(f"Error creating the checksum file: {e}")
return False
return False
def move_and_cleanup_iso(arch, desktop, kernel_type, iso_label_prefix="PeppermintOS"):
"""
Takes the built ISO, calculates the checksum, renames it, moves it to the server, and removes old ISOs.
Args:
arch (str): Architecture of the ISO.
desktop (str): Name of the desktop environment.
kernel_type (str): Type of the kernel.
iso_label_prefix (str): Prefix of the ISO label.
"""
iso_output_dir = paths.get("ISO_OUTPUT_DIR")
server_download_dir = paths.get("SERVER_DOWNLOAD_DIR") # Using paths for server dir
if not iso_output_dir:
logger.error("The ISO_OUTPUT_DIR variable is not defined in the paths module.")
return
if not server_download_dir:
logger.error("The SERVER_DOWNLOAD_DIR variable is not defined in the paths module.")
return
if not os.path.isdir(iso_output_dir):
logger.error(f"The ISO output directory does not exist: {iso_output_dir}")
return
if not os.path.isdir(server_download_dir):
logger.error(f"The server download directory does not exist: {server_download_dir}")
return
iso_file = find_iso_file(iso_output_dir)
if not iso_file:
return
logger.info(f"=> Calculating checksum for: {iso_file}")
checksum = calculate_checksum(iso_file)
if not checksum:
return
renamed_iso_path = rename_iso_file(iso_file, arch, desktop, kernel_type, iso_label_prefix)
if not renamed_iso_path:
return
logger.info(f"=> Removing files older than 24 hours from: {server_download_dir}")
remove_old_files(server_download_dir)
moved_iso_path = move_iso_to_server(renamed_iso_path, server_download_dir)
if moved_iso_path:
create_checksum_file(moved_iso_path, server_download_dir)