def _external_module_get_html_bytes(id_name: str,
                                    version: str) -> Optional[bytes]:
    prefix = "%s/%s/" % (id_name, version)
    all_keys = minio.list_file_keys(minio.ExternalModulesBucket, prefix)
    try:
        html_key = next(k for k in all_keys if k.endswith(".html"))
    except StopIteration:
        return None  # there is no HTML file

    return minio.get_object_with_data(minio.ExternalModulesBucket,
                                      html_key)["Body"]
def _load_external_module_uncached(module_id_name: str,
                                   version_sha1: str) -> CompiledModule:
    """
    Load a Python Module given a name and version.
    """
    prefix = "%s/%s/" % (module_id_name, version_sha1)
    all_keys = minio.list_file_keys(minio.ExternalModulesBucket, prefix)
    python_code_key = next(k for k in all_keys
                           if _is_basename_python_code(k[len(prefix):]))

    # Now we can load the code into memory.
    name = "%s.%s" % (module_id_name, version_sha1)
    with minio.temporarily_download(minio.ExternalModulesBucket,
                                    python_code_key) as path:
        logger.info(f"Loading {name} from {path}")
        return cjwstate.modules.kernel.compile(path, name)
Example #3
0
def _download_module_zipfile_deprecated(
    output_path: Path,  # tempdir/module.abcd12.zip
    module_id: ModuleId,
    version: ModuleVersion,
    spec: Dict[str, Any],
    js_module: str,
) -> None:
    prefix = "%s/%s/" % (module_id, version)
    all_keys = minio.list_file_keys(minio.ExternalModulesBucket, prefix)
    try:
        python_code_key = next(k for k in all_keys
                               if _is_basename_python_code(k[len(prefix):]))
    except StopIteration:
        raise FileNotFoundError
    try:
        html_key = next(k for k in all_keys if k.endswith(".html"))
    except StopIteration:
        html_key = None  # there is no HTML file

    # Write to a temporary file and then move. That makes it safe to read a
    # zipfile from one thread while downloading it from another. (".develop"
    # zipfiles are mutable.)
    with zipfile.ZipFile(output_path, mode="w") as zf:
        # Write .yaml spec
        zf.writestr("%s.yaml" % module_id, json.dumps(spec).encode("utf-8"))

        # Write .js js_module
        if js_module:
            zf.writestr("%s.js" % module_id, js_module.encode("utf-8"))

        # Write .py module code
        # raise FileNotFoundError
        with minio.temporarily_download(minio.ExternalModulesBucket,
                                        python_code_key) as py_path:
            zf.write(py_path, "%s.py" % module_id)

        # Write .html file
        if html_key:
            with minio.temporarily_download(minio.ExternalModulesBucket,
                                            html_key) as html_path:
                zf.write(html_path, "%s.html" % module_id)