Subversion Repositories Kolibri OS

Rev

Rev 9383 | Rev 9388 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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