def install_pkg(pkg_spec, install_path): data = get_pkg_metadata(pkg_spec) latest_ver = data["info"]["version"] packages = data["releases"][latest_ver] del data gc.collect() assert len(packages) == 1 package_url = packages[0]["url"] print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url)) package_fname = op_basename(package_url) f1 = url_open(package_url) try: f2 = uzlib.DecompIO(f1, gzdict_sz) f3 = tarfile.TarFile(fileobj=f2) meta = install_tar(f3, install_path) finally: f1.close() del f3 del f2 gc.collect() return meta
def open_web(url, gz=False): import socket _, _, host, path = url.split('/', 3) port = 80 if (len(host.split(':')) == 2): host, port = host.split(':', 2) port = int(port) print("host = %s, port = %d, path = %s" % (host, port, path)) addr = socket.getaddrinfo(host, port)[0][-1] s = socket.socket() s.connect(addr) s.send( bytes( 'GET /%s HTTP/1.0\r\nHost: %s\r\nAccept: image/*\r\n\r\n' % (path, host), 'utf8')) for i in range(100): # read first 100 lines searching for if len(s.readline()) < 3: # first empty line (contains "\r\n") break if gz: import uzlib return uzlib.DecompIO(s, 31) return s
def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT, fs_blocksize=0, status_addr=None, addr_64bit=False): # Check firmware is of .dfu or .dfu.gz type try: with open(filename, "rb") as f: hdr = uzlib.DecompIO(f, 16 + 15).read(6) except Exception: with open(filename, "rb") as f: hdr = f.read(6) if hdr != b"DfuSe\x01": print("Firmware must be a .dfu(.gz) file.") return if fs_type in (VFS_LFS1, VFS_LFS2) and not fs_blocksize: raise Exception("littlefs requires fs_blocksize parameter") mount_point = 1 mount_encoding = "<BBQQL" if addr_64bit else "<BBLLL" elems = _create_element( _ELEM_TYPE_MOUNT, struct.pack(mount_encoding, mount_point, fs_type, fs_base, fs_len, fs_blocksize), ) elems += _create_element( _ELEM_TYPE_FSLOAD, struct.pack("<B", mount_point) + bytes(filename, "ascii")) if status_addr is not None: # mboot will write 0 to status_addr on succes, or a negative number on failure machine.mem32[status_addr] = 1 elems += _create_element(_ELEM_TYPE_STATUS, struct.pack("<L", status_addr)) elems += _create_element(_ELEM_TYPE_END, b"") machine.bootloader(elems)
def update_mpy(filename, fs_base, fs_len): # Check firmware is of .dfu.gz type try: with open(filename, 'rb') as f: hdr = uzlib.DecompIO(f, 16 + 15).read(6) except Exception: hdr = None if hdr != b'DfuSe\x01': print('Firmware must be a .dfu.gz file.') return # old PYBD firmware didn't accept args, mboot searched for PYBD_SF2*.dfu.gz regardless machine.bootloader() ELEM_TYPE_END = 1 ELEM_TYPE_MOUNT = 2 ELEM_TYPE_FSLOAD = 3 ELEM_MOUNT_FAT = 1 mount_point = 1 mount = struct.pack('<BBBBLL', ELEM_TYPE_MOUNT, 10, mount_point, ELEM_MOUNT_FAT, fs_base, fs_len) fsup = struct.pack('<BBB', ELEM_TYPE_FSLOAD, 1 + len(filename), mount_point) + bytes(filename, 'ascii') end = struct.pack('<BB', ELEM_TYPE_END, 0) machine.bootloader(mount + fsup + end)
def open_file(filename, gz=False): filedata = open(filename, "rb") if gz: import uzlib return uzlib.DecompIO(filedata,31) return filedata
try: import uzlib as zlib import uio as io except ImportError: import sys print("SKIP") sys.exit() # gzip bitstream buf = io.BytesIO( b'\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00' ) inp = zlib.DecompIO(buf, 16 + 8) print(buf.seek(0, 1)) print(inp.read(1)) print(buf.seek(0, 1)) print(inp.read(2)) print(inp.read()) print(buf.seek(0, 1)) print(inp.read(1)) print(inp.read()) print(buf.seek(0, 1)) # Check FHCRC field buf = io.BytesIO( b'\x1f\x8b\x08\x02\x99\x0c\xe5W\x00\x03\x00\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00' ) inp = zlib.DecompIO(buf, 16 + 8) print(inp.read()) # Check FEXTRA field
# Test passing dictionary buffer to uzlib.DecompIO() try: import uzlib import uio as io except ImportError: print("SKIP") raise SystemExit dictbuf = bytearray(512) # zlib bitstream inp = uzlib.DecompIO( io.BytesIO(b"\x18\x95\xcbH\xcd\xc9\xc9\xcf\x00\x11\x00\x16\xbc\x04)"), 0, dictbuf) print(inp.read()) # Just check thar dictbug got some content in it, though it actually # would contain the output data. print(dictbuf != bytearray(512))
# Test for uzlib.DecompIO.init() try: import uzlib import uio as io except ImportError: print("SKIP") raise SystemExit # Need to pass valid header inp = uzlib.DecompIO(io.BytesIO(b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'), 25) inp.init( io.BytesIO( b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03' b'\xcbH\xcd\xc9\xc9\xcf\x00\x11\x00h\x97\x8c\xf5\n\x00\x00\x00')) print(inp.read()) inp.init( io.BytesIO(b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03' b'+\xcf/\xcaI)\x07\x11\x00\x8c\xa3\x96\xb9\n\x00\x00\x00')) print(inp.read())
try: import uzlib as zlib import uio as io except ImportError: import sys print("SKIP") sys.exit() # Raw DEFLATE bitstream buf = io.BytesIO(b'\xcbH\xcd\xc9\xc9\x07\x00') inp = zlib.DecompIO(buf, -8) print(buf.seek(0, 1)) print(inp.read(1)) print(buf.seek(0, 1)) print(inp.read(2)) print(inp.read()) print(buf.seek(0, 1)) print(inp.read(1)) print(inp.read()) print(buf.seek(0, 1)) # zlib bitstream inp = zlib.DecompIO(io.BytesIO(b'x\x9c30\xa0=\x00\x00\xb3q\x12\xc1')) print(inp.read(10)) print(inp.read()) # zlib bitstream, wrong checksum inp = zlib.DecompIO(io.BytesIO(b'x\x9c30\xa0=\x00\x00\xb3q\x12\xc0')) try:
try: import uzlib as zlib import uio as io except ImportError: print("SKIP") raise SystemExit # Raw DEFLATE bitstream buf = io.BytesIO(b"\xcbH\xcd\xc9\xc9\x07\x00") inp = zlib.DecompIO(buf, -8) print(buf.seek(0, 1)) print(inp.read(1)) print(buf.seek(0, 1)) print(inp.read(2)) print(inp.read()) print(buf.seek(0, 1)) print(inp.read(1)) print(inp.read()) print(buf.seek(0, 1)) # zlib bitstream inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc1")) print(inp.read(10)) print(inp.read()) # zlib bitstream, wrong checksum inp = zlib.DecompIO(io.BytesIO(b"x\x9c30\xa0=\x00\x00\xb3q\x12\xc0")) try: print(inp.read()) except OSError as e: print(repr(e))
# Test for uzlib.DecompIO.init() try: import uzlib import uio as io except ImportError: print("SKIP") raise SystemExit # Need to pass valid header inp = uzlib.DecompIO(io.BytesIO(b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03"), 25) inp.init( io.BytesIO( b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03" b"\xcbH\xcd\xc9\xc9\xcf\x00\x11\x00h\x97\x8c\xf5\n\x00\x00\x00")) print(inp.read()) inp.init( io.BytesIO(b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03" b"+\xcf/\xcaI)\x07\x11\x00\x8c\xa3\x96\xb9\n\x00\x00\x00")) print(inp.read())
def flash_stream_gz(dstream, addr=0, name=""): if name.lower().endswith(".gz"): import uzlib flash_stream(uzlib.DecompIO(dstream, 31), addr) else: flash_stream(dstream, addr)
def prog_stream_gz(dstream, blocksize=4096, name=""): if name.lower().endswith(".gz"): import uzlib prog_stream(uzlib.DecompIO(dstream, 31), blocksize) else: prog_stream(dstream, blocksize)
### This file is to be put in the \animations folder along with the proccessed video file of the Defon Furs badge ### import uzlib import dcfurs # Video File you want to play video_file_name = "beemovie_2.7fps_24color_1bright_nocontrast.gz" # Frame Rate that was used to process the video file frameRate = 7 # Can be 8 or 24. Enter what was used to process the video colorDepth = 24 videoFile = open("animations\\" + video_file_name, 'rb') decoder = uzlib.DecompIO(videoFile, 31) class video: global frameRate def __init__(self): global frameRate self.interval = int(1000 / frameRate) def draw(self): global frameRate global decoder global videoFile global colorDepth