Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# temporary directory and extract our resources to there, but we don't want to
# pay the case of setting up a a temporary directory and shuffling bytes around
# in the common case where these files already exist on disk side by side. So
# we will check what loader the embedded package used, if it's a
# SourceFileLoader then we'll assume it's going to be on the filesystem and
# just use importlib.resources.path.
# jsii-runtime.js MUST be the first item in this list.
filenames = ["jsii-runtime.js", "jsii-runtime.js.map", "mappings.wasm"]
if isinstance(
jsii._embedded.jsii.__loader__, importlib.machinery.SourceFileLoader
):
paths = [
self._ctx_stack.enter_context(
importlib_resources.path(jsii._embedded.jsii, f)
)
for f in filenames
]
else:
tmpdir = self._ctx_stack.enter_context(tempfile.TemporaryDirectory())
paths = [os.path.join(tmpdir, filename) for filename in filenames]
for path, filename in zip(paths, filenames):
with open(path, "wb") as fp:
fp.write(
importlib_resources.read_binary(jsii._embedded.jsii, filename)
)
# Ensure that our jsii-runtime.js is the first entry in our paths, and that all
# of our paths, are in a commmon directory, and we didn't get them split into
# multiple directories somehow.
# We have the JSII Runtime bundled with our package and we want to extract it,
# however if we just blindly use importlib.resources for this, we're going to
# have our jsii-runtime.js existing in a *different* temporary directory from
# the jsii-runtime.js.map, which we don't want. We can manually set up a
# temporary directory and extract our resources to there, but we don't want to
# pay the case of setting up a a temporary directory and shuffling bytes around
# in the common case where these files already exist on disk side by side. So
# we will check what loader the embedded package used, if it's a
# SourceFileLoader then we'll assume it's going to be on the filesystem and
# just use importlib.resources.path.
# jsii-runtime.js MUST be the first item in this list.
filenames = ["jsii-runtime.js", "jsii-runtime.js.map", "mappings.wasm"]
if isinstance(
jsii._embedded.jsii.__loader__, importlib.machinery.SourceFileLoader
):
paths = [
self._ctx_stack.enter_context(
importlib_resources.path(jsii._embedded.jsii, f)
)
for f in filenames
]
else:
tmpdir = self._ctx_stack.enter_context(tempfile.TemporaryDirectory())
paths = [os.path.join(tmpdir, filename) for filename in filenames]
for path, filename in zip(paths, filenames):
with open(path, "wb") as fp:
fp.write(
importlib_resources.read_binary(jsii._embedded.jsii, filename)
)