Subversion Repositories Kolibri OS

Rev

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

Rev 9374 Rev 9376
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
17
from lib.network import download
18
 
18
 
19
# TODO: Move into _tools/lib
19
# TODO: Move into _tools/lib
20
def get_file_directory(path):
20
def get_file_directory(path):
21
    path = path.replace("\\", "/")
21
    path = path.replace("\\", "/")
22
    if "/" in path:
22
    if "/" in path:
23
        folder = "/".join(path.split("/")[:-1])
23
        folder = "/".join(path.split("/")[:-1])
24
        if folder == "":
24
        if folder == "":
25
            return "/" # It was a file in the root folder
25
            return "/" # It was a file in the root folder
26
        return folder
26
        return folder
27
    else:
27
    else:
28
        return "." # Just a filename, let's return current folder
28
        return "." # Just a filename, let's return current folder
29
 
29
 
30
# TODO: Move into _tools/lib
30
# TODO: Move into _tools/lib
31
def run_qemu(start_dir = "workspace"):
31
def run_qemu(start_dir = "workspace"):
32
    qemu_command = f"qemu-system-i386"
32
    qemu_command = f"qemu-system-i386"
33
    flags = ""
33
    flags = ""
34
    flags += "-L . " # IDK why it does not work without this
34
    flags += "-L . " # IDK why it does not work without this
35
    flags += "-m 128 "
35
    flags += "-m 128 "
36
    flags += f"-drive format=raw,file={start_dir}/kolibri.img,index=0,if=floppy -boot a "
36
    flags += f"-drive format=raw,file={start_dir}/kolibri.img,index=0,if=floppy -boot a "
37
    flags += "-vga vmware "
37
    flags += "-vga vmware "
38
    flags += "-net nic,model=rtl8139 -net user "
38
    flags += "-net nic,model=rtl8139 -net user "
39
    flags += "-soundhw ac97 "
39
    flags += "-soundhw ac97 "
40
    if is_win32():
40
    if is_win32():
41
        qemu_full_path = shutil.which(qemu_command)
41
        qemu_full_path = shutil.which(qemu_command)
42
        qemu_directory = get_file_directory(qemu_full_path)
42
        qemu_directory = get_file_directory(qemu_full_path)
43
        flags += f"-L {qemu_directory} "
43
        flags += f"-L {qemu_directory} "
44
    s = f"{qemu_command} {flags}"
44
    s = f"{qemu_command} {flags}"
45
    qemu_stdout = open(f"{start_dir}/qemu_stdout.log", "w")
45
    qemu_stdout = open(f"{start_dir}/qemu_stdout.log", "w")
46
    qemu_stderr = open(f"{start_dir}/qemu_stderr.log", "w")
46
    qemu_stderr = open(f"{start_dir}/qemu_stderr.log", "w")
47
    if is_win32():
47
    if is_win32():
48
        return subprocess.Popen(s, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, shell = True, start_new_session = True)
48
        return subprocess.Popen(s, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, shell = True, start_new_session = True)
49
    else:
49
    else:
50
        a = shlex.split(s)
50
        a = shlex.split(s)
51
        return subprocess.Popen(a, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, start_new_session = True)
51
        return subprocess.Popen(a, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, start_new_session = True)
52
 
52
 
53
if __name__ == "__main__":
53
if __name__ == "__main__":
54
    program_files = build()
54
    program_files = build()
55
 
55
 
56
    os.makedirs("workspace", exist_ok = True)
56
    os.makedirs("workspace", exist_ok = True)
57
 
57
 
58
    if not os.path.exists("workspace/kolibri.unmodified.img"):
58
    if not os.path.exists("workspace/kolibri.unmodified.img"):
59
        img_url = "http://builds.kolibrios.org/eng/data/data/kolibri.img"
59
        img_url = "http://builds.kolibrios.org/eng/data/data/kolibri.img"
60
        download(img_url, "workspace/kolibri.unmodified.img")
60
        download(img_url, "workspace/kolibri.unmodified.img")
61
 
61
 
62
    # Create a copy of IMG
62
    # Create a copy of IMG
63
    shutil.copyfile("workspace/kolibri.unmodified.img", "workspace/kolibri.img")
63
    shutil.copyfile("workspace/kolibri.unmodified.img", "workspace/kolibri.img")
64
 
64
 
65
    # Open the IMG
65
    # Open the IMG
66
    with open("workspace/kolibri.img", "rb") as img:
66
    with open("workspace/kolibri.img", "rb") as img:
67
        img_data = img.read()
67
        img_data = img.read()
68
    img = Floppy(img_data)
68
    img = Floppy(img_data)
69
 
69
 
70
    # Remove unuseful folders
70
    # Remove unuseful folders
71
    img.delete_path("GAMES")
71
    img.delete_path("GAMES")
72
    img.delete_path("DEMOS")
72
    img.delete_path("DEMOS")
73
    img.delete_path("3D")
73
    img.delete_path("3D")
74
 
74
 
75
    log("Moving program files into kolibri image... ", end = "")
75
    log("Moving program files into kolibri image... ", end = "")
76
    for file_name in program_files:
76
    for file_name in program_files:
77
        with open(file_name, "rb") as file:
77
        with open(file_name, "rb") as file:
78
            file_data = file.read()
78
            file_data = file.read()
79
        if not img.add_file_path(file_name.upper(), file_data):
79
        if not img.add_file_path(file_name.upper(), file_data):
80
            print(f"Coudn't move {file_name} into IMG")
80
            print(f"Coudn't move {file_name} into IMG")
81
    log("Done")
81
    log("Done")
82
 
82
 
83
    # TODO: Figure out which of compiled files is a program executable and only run it
83
    # TODO: Figure out which of compiled files is a program executable and only run it
84
    log("Adding program to autorun.dat", end = "")
84
    log("Adding program to autorun.dat", end = "")
85
    lines_to_add = b""
85
    lines_to_add = b""
86
    for file_name in program_files:
86
    for file_name in program_files:
87
        lines_to_add += bytes(f"\r\n/SYS/{file_name.upper()}\t\t""\t0\t# Your program", "ascii")
87
        lines_to_add += bytes(f"\r\n/SYS/{file_name.upper()}\t\t""\t0\t# Your program", "ascii")
88
    autorun_dat = img.extract_file_path("SETTINGS\AUTORUN.DAT")
88
    autorun_dat = img.extract_file_path("SETTINGS\AUTORUN.DAT")
89
    place_for_new_lines = autorun_dat.index(b"\r\n/SYS/@TASKBAR")# b"\r\n### Hello, ASM World! ###")
89
    place_for_new_lines = autorun_dat.index(b"\r\n/SYS/@TASKBAR")# b"\r\n### Hello, ASM World! ###")
90
    autorun_dat = autorun_dat[:place_for_new_lines] + lines_to_add + autorun_dat[place_for_new_lines:]
90
    autorun_dat = autorun_dat[:place_for_new_lines] + lines_to_add + autorun_dat[place_for_new_lines:]
91
    print(autorun_dat)
-
 
92
    img.delete_path("SETTINGS\AUTORUN.DAT")
91
    img.delete_path("SETTINGS\AUTORUN.DAT")
93
    img.add_file_path("SETTINGS\AUTORUN.DAT", autorun_dat)
92
    img.add_file_path("SETTINGS\AUTORUN.DAT", autorun_dat)
94
    log("Done")
93
    log("Done")
95
 
94
 
96
    img.save("workspace/kolibri.img")
95
    img.save("workspace/kolibri.img")
97
 
96
 
98
    run_qemu()
97
    run_qemu()