Rev 9357 | Rev 9368 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
9367 | Boppan | 1 | import os |
2 | import sys |
||
3 | import shutil |
||
4 | import urllib.request |
||
5 | import subprocess |
||
6 | |||
7 | import build |
||
8 | |||
9 | path_to_lib = '../lib' |
||
10 | sys.path.append(path_to_lib) |
||
11 | |||
12 | from makeflop import Floppy |
||
13 | |||
14 | # TODO: Move into _tools/lib |
||
15 | def get_file_directory(path): |
||
16 | path = path.replace("\\", "/") |
||
17 | if "/" in path: |
||
18 | folder = "/".join(path.split("/")[:-1]) |
||
19 | if folder == "": |
||
20 | return "/" # It was a file in the root folder |
||
21 | return folder |
||
22 | else: |
||
23 | return "." # Just a filename, let's return current folder |
||
24 | |||
25 | # TODO: Move into _tools/lib |
||
26 | def is_win32(): |
||
27 | return True if sys.platform == "win32" else False |
||
28 | |||
29 | # TODO: Move into _tools/lib |
||
30 | def is_linux(): |
||
31 | return True if sys.platform == "linux" or sys.platform == "linux2" else False |
||
32 | |||
33 | # TODO: Move into _tools/lib |
||
34 | def is_osx(): |
||
35 | return True if sys.platform == "darwin" else False |
||
36 | |||
37 | # TODO: Move into _tools/lib |
||
38 | def log(s, end = "\n"): |
||
39 | print(s, end = end, flush = True) |
||
40 | |||
41 | # TODO: Move into _tools/lib |
||
42 | def download(link, path): |
||
43 | log(f"Downloading {path}... ", end = "") |
||
44 | urllib.request.urlretrieve(link, path) |
||
45 | log("Done.") |
||
46 | |||
47 | # TODO: Move into _tools/lib |
||
48 | def path(*args): |
||
49 | return os.sep.join(args) |
||
50 | |||
51 | # TODO: Move into _tools/lib |
||
52 | def run_qemu(start_dir = "workspace"): |
||
53 | qemu_command = f"qemu-system-i386" |
||
54 | flags = "" |
||
55 | flags += "-L . " # IDK why it does not work without this |
||
56 | flags += "-m 128 " |
||
57 | flags += f"-drive format=raw,file={start_dir}/kolibri.img,index=0,if=floppy -boot a " |
||
58 | flags += "-vga vmware " |
||
59 | flags += "-net nic,model=rtl8139 -net user " |
||
60 | flags += "-soundhw ac97 " |
||
61 | if is_win32(): |
||
62 | qemu_full_path = shutil.which(qemu_command) |
||
63 | qemu_directory = get_file_directory(qemu_full_path) |
||
64 | flags += f"-L {qemu_directory} " |
||
65 | s = f"{qemu_command} {flags}" |
||
66 | qemu_stdout = open(f"{start_dir}/qemu_stdout.log", "w") |
||
67 | qemu_stderr = open(f"{start_dir}/qemu_stderr.log", "w") |
||
68 | if is_win32(): |
||
69 | return subprocess.Popen(s, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, shell = True, start_new_session = True) |
||
70 | else: |
||
71 | a = shlex.split(s) |
||
72 | return subprocess.Popen(a, bufsize = 0, stdout = qemu_stdout, stderr = qemu_stderr, stdin = subprocess.DEVNULL, start_new_session = True) |
||
73 | |||
74 | if __name__ == "__main__": |
||
75 | program_files = build.build() |
||
76 | |||
77 | os.makedirs("workspace", exist_ok = True) |
||
78 | |||
79 | if not os.path.exists("workspace/kolibri.img"): |
||
80 | img_url = "http://builds.kolibrios.org/eng/data/data/kolibri.img" |
||
81 | download(img_url, "workspace/kolibri.img") |
||
82 | |||
83 | # Open the IMG |
||
84 | with open("workspace/kolibri.img", "rb") as img: |
||
85 | img_data = img.read() |
||
86 | img = Floppy(img_data) |
||
87 | |||
88 | # Remove unuseful folders |
||
89 | img.delete_path("GAMES") |
||
90 | img.delete_path("DEMOS") |
||
91 | img.delete_path("3D") |
||
92 | |||
93 | log("Moving program files into kolibri image... ", end = "") |
||
94 | for file_name in program_files: |
||
95 | with open(file_name, "rb") as file: |
||
96 | file_data = file.read() |
||
97 | if not img.add_file_path(file_name, file_data): |
||
98 | print(f"Coudn't move {file_name} into IMG") |
||
99 | img.save("workspace/kolibri.img") |
||
100 | log("Done") |
||
101 | |||
102 | # TODO: Autorun |
||
103 | run_qemu() |