bussy/scripts/utils.py

50 lines
1.2 KiB
Python
Raw Normal View History

# Miscellaneous utilities, meant to be included from build.py
import os
# Do a fork/execl because that way all children get the same stdout,
# i.e. if it's a tty the executed program should behave appropriately.
# Python probably has an idiomatic way to do this, but frankly idc.
def run_sync(program: str, *args):
path = os.popen("which " + program).read().strip()
child = os.fork()
if child == 0:
os.execl(path, program, *args)
print("execl() failed")
exit(1)
elif child == -1:
print("fork() failed")
exit(1)
status = os.waitpid(child, 0)
if status[1] != 0:
print(program + " exited with status code " + str(status[1]))
exit(1)
def generate_crc_tab(poly_reverse: int):
tab = []
for crc in range(256):
for _ in range(8):
lsb = crc % 2
crc >>= 1
if lsb == 1:
crc ^= poly_reverse
tab.append(crc)
return tab
def do_crc(data, table):
crc = 0xffffffff
for byte in data:
crc = (crc >> 8) ^ table[(crc ^ byte) & 0xff]
return ~crc & 0xffffffff
CCITT32_TAB = generate_crc_tab(0xedb88320)
def crc32(data: bytes):
global CCITT32_TAB
return do_crc(data, CCITT32_TAB)