76 lines
2.2 KiB
Python
Executable File
76 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import time
|
|
import subprocess
|
|
import sys
|
|
|
|
# Define file paths and default QEMU command
|
|
bin_file_path = "build/src/kernel/appa-os.bin"
|
|
iso_file_path = "appa-os.iso"
|
|
qemu_command = ["qemu-system-i386"]
|
|
use_iso = "--iso" in sys.argv
|
|
|
|
# Adjust command based on the --iso argument
|
|
if use_iso:
|
|
qemu_command += ["-cdrom", iso_file_path]
|
|
else:
|
|
qemu_command += ["-kernel", bin_file_path]
|
|
|
|
|
|
# Function to start QEMU
|
|
def start_qemu():
|
|
return subprocess.Popen(
|
|
qemu_command, cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
|
)
|
|
|
|
|
|
# Function to check if the file has been modified
|
|
def has_been_modified(last_mod_time, file_path):
|
|
return os.path.getmtime(file_path) > last_mod_time
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Choose the file path based on --iso flag
|
|
file_path = iso_file_path if use_iso else bin_file_path
|
|
|
|
# Check if the file exists; if not, wait until it becomes available
|
|
if not os.path.isfile(file_path):
|
|
print(f"Warning: {file_path} not found. Waiting for the file to be created...")
|
|
while not os.path.isfile(file_path):
|
|
time.sleep(1)
|
|
|
|
# Initialize variables
|
|
last_mod_time = os.path.getmtime(file_path)
|
|
qemu_process = start_qemu()
|
|
|
|
try:
|
|
while True:
|
|
# Check if the file exists
|
|
if not os.path.isfile(file_path):
|
|
print(f"Warning: {file_path} not found. Terminating QEMU process...")
|
|
qemu_process.terminate()
|
|
qemu_process.wait()
|
|
time.sleep(1)
|
|
continue
|
|
|
|
# Check for file modification
|
|
if has_been_modified(last_mod_time, file_path):
|
|
print("File has been modified. Restarting QEMU...")
|
|
last_mod_time = os.path.getmtime(file_path)
|
|
|
|
# Terminate existing QEMU process
|
|
qemu_process.terminate()
|
|
qemu_process.wait() # Wait for the process to fully terminate
|
|
|
|
# Start a new QEMU process
|
|
qemu_process = start_qemu()
|
|
|
|
# Delay between checks
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
print("Script interrupted. Shutting down QEMU...")
|
|
qemu_process.terminate()
|
|
qemu_process.wait()
|