Constant BYTECODE_COMPILER

Source
pub const BYTECODE_COMPILER: &[u8] = b"# Copyright 2022 Gregory Szorc.\n#\n# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or\n# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license\n# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your\n# option. This file may not be copied, modified, or distributed\n# except according to those terms.\n\n# This script is executed to compile Python bytecode by the repackager.\n#\n# When invoked, we start a server that listens for commands. We then\n# react to those commands and send results to the caller.\n\nimport importlib._bootstrap_external\nimport importlib.util\nimport marshal\nimport os\nimport re\nimport sys\n\n\nRE_CODING = re.compile(b\"^[ \\t\\f]*#.*?coding[:=][ \\t]*([-_.a-zA-Z0-9]+)\")\n\n\nif marshal.version != 4:\n    raise Exception(\"unexpected marshal version: %d\" % marshal.version)\n\nstdin = sys.__stdin__.buffer\nstdout = sys.__stdout__.buffer\n\n\nwhile True:\n    command = stdin.readline().rstrip()\n\n    if command == b\"exit\":\n        sys.exit(0)\n    elif command == b\"magic_number\":\n        stdout.write(importlib._bootstrap_external.MAGIC_NUMBER)\n        stdout.flush()\n    elif command == b\"compile\":\n        try:\n            name_len = stdin.readline().rstrip()\n            source_len = stdin.readline().rstrip()\n            optimize_level = stdin.readline().rstrip()\n            output_mode = stdin.readline().rstrip()\n\n            name_len = int(name_len)\n            source_len = int(source_len)\n            optimize_level = int(optimize_level)\n\n            name = stdin.read(name_len)\n            source = stdin.read(source_len)\n\n            name = os.fsdecode(name)\n\n            # Default source encoding is UTF-8. But per PEP 263, the first or second\n            # line of source can match a regular expression to define a custom\n            # encoding. We need to detect custom encodings and use it to decode\n            # the passed bytes to str.\n            encoding = \"utf-8\"\n\n            for line in source.splitlines()[0:2]:\n                m = RE_CODING.match(line)\n                if m:\n                    encoding = m.group(1).decode(\"ascii\")\n                    break\n\n            # Someone has set us up the BOM! According to PEP 263 the file should\n            # be interpreted as UTF-8.\n            if source.startswith(b\"\\xef\\xbb\\xbf\"):\n                encoding = \"utf-8\"\n                source = source[3:]\n\n            source_bytes = source\n            source = source.decode(encoding)\n\n            code = compile(source, name, \"exec\", optimize=optimize_level)\n            bytecode = marshal.dumps(code)\n\n            if output_mode == b\"bytecode\":\n                out = bytecode\n            elif output_mode == b\"pyc-checked-hash\":\n                source_hash = importlib.util.source_hash(source_bytes)\n                out = importlib._bootstrap_external._code_to_hash_pyc(\n                    code,\n                    source_hash,\n                    checked=True,\n                )\n            elif output_mode == b\"pyc-unchecked-hash\":\n                source_hash = importlib.util.source_hash(source_bytes)\n                out = importlib._bootstrap_external._code_to_hash_pyc(\n                    code,\n                    source_hash,\n                    checked=False,\n                )\n            else:\n                raise Exception(\"unknown output mode: %s\" % output_mode)\n\n            # Code 0 means success.\n            stdout.write(b\"0\\n\")\n            stdout.write(b\"%d\\n\" % len(out))\n            stdout.write(out)\n        except Exception as e:\n            stdout.write(b\"1\\n\")\n            error_bytes = str(e).encode(\"utf-8\", errors=\"replace\")\n            stdout.write(b\"%d\\n\" % len(error_bytes))\n            stdout.write(error_bytes)\n\n        stdout.flush()\n    else:\n        raise Exception(\"invalid command: %s\" % command)\n";