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