Subversion Repositories Kolibri OS

Rev

Rev 9391 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9367 Boppan 1
import os
2
import sys
9368 Boppan 3
import shlex
9367 Boppan 4
import shutil
5
import urllib.request
6
import subprocess
7
 
9374 Boppan 8
path_to_tools_workspace = os.path.dirname(os.path.abspath(__file__))
9
path_to_tools = os.path.dirname(path_to_tools_workspace)
9370 Boppan 10
sys.path.append(path_to_tools)
9367 Boppan 11
 
9374 Boppan 12
from workspace.build import build
13
 
9390 Boppan 14
from lib.builds import builds_get, builds_get_contents
9370 Boppan 15
from lib.makeflop import Floppy
16
from lib.platform import is_win32, path
9411 Boppan 17
from lib.logging import log, require_tools
9383 Boppan 18
from lib.constants import tools_cache_kolibri_img
9367 Boppan 19
 
20
# TODO: Move into _tools/lib
21
def run_qemu(start_dir = "workspace"):
9411 Boppan 22
    require_tools(("qemu-system-i386",))
23
 
9367 Boppan 24
    qemu_command = f"qemu-system-i386"
25
    flags = ""
26
    flags += "-L . " # IDK why it does not work without this
27
    flags += "-m 128 "
28
    flags += f"-drive format=raw,file={start_dir}/kolibri.img,index=0,if=floppy -boot a "
29
    flags += "-vga vmware "
30
    flags += "-net nic,model=rtl8139 -net user "
31
    flags += "-soundhw ac97 "
32
    if is_win32():
33
        qemu_full_path = shutil.which(qemu_command)
9381 Boppan 34
        qemu_directory = os.path.dirname(qemu_full_path)
9367 Boppan 35
        flags += f"-L {qemu_directory} "
36
    s = f"{qemu_command} {flags}"
37
    qemu_stdout = open(f"{start_dir}/qemu_stdout.log", "w")
38
    qemu_stderr = open(f"{start_dir}/qemu_stderr.log", "w")
39
    if is_win32():
40
        return subprocess.Popen(s, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, shell = True, start_new_session = True)
41
    else:
42
        a = shlex.split(s)
43
        return subprocess.Popen(a, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, start_new_session = True)
44
 
45
if __name__ == "__main__":
9377 Boppan 46
    program_name = build()
9367 Boppan 47
 
48
    os.makedirs("workspace", exist_ok = True)
49
 
9373 Boppan 50
    # Create a copy of IMG
9390 Boppan 51
    kolibri_img = builds_get("eng/data/data/kolibri.img", "workspace/kolibri.img")
9373 Boppan 52
 
9367 Boppan 53
    # Open the IMG
9390 Boppan 54
    with open(kolibri_img, "rb") as img:
9367 Boppan 55
        img_data = img.read()
56
    img = Floppy(img_data)
57
 
58
    # Remove unuseful folders
59
    img.delete_path("GAMES")
60
    img.delete_path("DEMOS")
61
    img.delete_path("3D")
62
 
9391 Boppan 63
    # Insert faster kernel if no --compressed-kernel flag passed
64
    if "--compressed-kernel" not in sys.argv:
9389 Boppan 65
        new_kernel = builds_get_contents("eng/data/kernel/trunk/kernel.mnt.pretest")
66
        img.add_file_path("KERNEL.MNT", new_kernel)
67
 
9377 Boppan 68
    log("Moving program into kolibri image... ", end = "")
69
    with open(program_name, "rb") as file:
70
        file_data = file.read()
71
    if not img.add_file_path(program_name.upper(), file_data):
72
        print(f"Coudn't move {program_name} into IMG")
9367 Boppan 73
    log("Done")
74
 
9377 Boppan 75
    log("Adding program to autorun.dat... ", end = "")
76
    lines_to_add = bytes(f"\r\n/SYS/{program_name.upper()}\t\t""\t0\t# Your program", "ascii")
9373 Boppan 77
    autorun_dat = img.extract_file_path("SETTINGS\AUTORUN.DAT")
78
    place_for_new_lines = autorun_dat.index(b"\r\n/SYS/@TASKBAR")# b"\r\n### Hello, ASM World! ###")
79
    autorun_dat = autorun_dat[:place_for_new_lines] + lines_to_add + autorun_dat[place_for_new_lines:]
80
    img.delete_path("SETTINGS\AUTORUN.DAT")
81
    img.add_file_path("SETTINGS\AUTORUN.DAT", autorun_dat)
82
    log("Done")
83
 
9390 Boppan 84
    img.save(kolibri_img)
9373 Boppan 85
 
9367 Boppan 86
    run_qemu()