sotn-decomp/tools/bin2c.py
Jonathan Hohle 8287841ccd
Python virtualenv Support (#1620)
Ubuntu/Debian and Python recommend using virtual environments for
project-specific Python dependencies and as of Ubuntu 24 this is lightly
enforced when installing packages via pip. This is due to pip and the
system package manager installing files to the same location which may
cause either's internal state to no longer reflect what is actually
installed.

This updates the project to use a Python `virtualenv` for project
dependencies and updates internal scripts to support both global and
virtualenvs, but favors virtualenvs for new workspaces.

All tools that hardcode `/usr/bin/python3` now use `env(1)` to find the
first `python3` in the path. For those with a virtualenv configured,
this will be the Python managed there. For everyone else, this should be
the system Python or whatever other scheme they may have used previously
(assuming `python3` already existed in their `PATH`).

The `Makefile` has been updated to prepend `.venv/bin` to the `PATH` and
use `python3` from there if it exists and has been configured. It also
has a new `python-dependencies` target which will configure the venv and
install all python dependnecies there.

The `Dockerfile` has been updated to create an external `.venv` outside
of the project directory. Python's `virtualenv`s are not relocatable and
may hardcode paths which are mounted differently in the container and
host. To deal with differences in paths between the container (which
mounts the host's project directory to `/sotn`) host which may be at an
arbitarary directory the `VENV_PATH` environment variable is used to
override paths in the `Makefile`.

GitHub workflows have been updated to use `.venv`.
2024-09-17 23:19:20 -07:00

97 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import os
import sys
parser = argparse.ArgumentParser(
description="Converts a binary file into C-compatible data"
)
group_size = parser.add_mutually_exclusive_group()
group_size.add_argument(
"-1",
"--byte",
help="write as a 8-bit array (default)",
action="store_true",
required=False,
)
group_size.add_argument(
"-2", "--word", help="write as a 16-bit array", action="store_true", required=False
)
group_size.add_argument(
"-4", "--dword", help="write as a 32-bit array", action="store_true", required=False
)
group_sign = parser.add_mutually_exclusive_group()
group_sign.add_argument(
"-u",
"--unsigned",
help="write values as unsigned (default)",
action="store_true",
required=False,
)
group_sign.add_argument(
"-s", "--signed", help="write values as signed", action="store_true", required=False
)
parser.add_argument("filein", help="binary file name to convert", nargs=1, default=None)
parser.add_argument("name", help="variable name", nargs=1, default=None)
args = parser.parse_args()
def get_array_size(args):
if args.word == True:
return 2
if args.dword == True:
return 4
return 1
def is_signed(args):
if args.signed == True:
return True
return False
def as_byte(bytes):
return f"0x{chunk[0]:x}, "
def as_word(bytes):
return f"0x{chunk[1]:x}{chunk[0]:x}, "
def as_dword(bytes):
return f"0x{chunk[3]:x}{chunk[2]:x}{chunk[1]:x}{chunk[0]:x}, "
if __name__ == "__main__":
array_size = get_array_size(args)
is_signed = is_signed(args)
file_name = args.filein[0]
name = args.name[0]
data_size = os.stat(file_name).st_size
if data_size % array_size != 0:
sys.stderr.write(f"{file_name} size not aligend by {array_size}\n")
line = ""
if is_signed:
line += "signed "
else:
line += "unsigned "
if array_size == 1:
line += "char "
str_func = as_byte
if array_size == 2:
line += "short "
str_func = as_word
if array_size == 4:
line += "int "
str_func = as_dword
sys.stdout.write(f"{line}{name}[] = {{\n")
with open(file_name, "rb") as file:
while file.tell() < data_size:
chunk = file.read(array_size)
sys.stdout.write(str_func(chunk))
sys.stdout.write("\n};")