[][src]Constant python_packaging::bytecode::BYTECODE_COMPILER

pub const BYTECODE_COMPILER: &[u8] = b"# This Source Code Form is subject to the terms of the Mozilla Public\n# License, v. 2.0. If a copy of the MPL was not distributed with this\n# file, You can obtain one at https://mozilla.org/MPL/2.0/.\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        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, source_hash, 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, source_hash, checked=False,\n            )\n        else:\n            raise Exception(\"unknown output mode: %s\" % output_mode)\n\n        stdout.write(b\"%d\\n\" % len(out))\n        stdout.write(out)\n        stdout.flush()\n    else:\n        raise Exception(\"invalid command: %s\" % command)\n";