def test_add_sys_path(self): c = OxidizedResourceCollector( allowed_locations=["in-memory", "filesystem-relative"]) for path in sys.path: if os.path.isdir(path): for resource in find_resources_in_path(path): c.add_in_memory(resource) c.add_filesystem_relative("", resource) python_exe = os.environ.get("PYTHON_SYS_EXECUTABLE") with assert_tempfile_cleaned_up(): resources, file_installs = c.oxidize(python_exe=python_exe) f = OxidizedFinder() f.add_resources(resources) with (self.td / "serialized").open("wb") as fh: fh.write(f.serialize_indexed_resources()) f = OxidizedFinder() f.index_file_memory_mapped(self.td / "serialized") self.assertGreaterEqual(len(f.indexed_resources()), len(resources)) for r in f.indexed_resources(): r.in_memory_source r.in_memory_bytecode
def test_serialize_simple(self): f = OxidizedFinder() m = OxidizedResource() m.name = "my_module" m.flavor = "module" m.in_memory_source = b"import io" f.add_resource(m) m = OxidizedResource() m.name = "module_b" m.flavor = "module" m.in_memory_bytecode = b"dummy bytecode" f.add_resource(m) serialized = f.serialize_indexed_resources() self.assertIsInstance(serialized, bytes) f2 = OxidizedFinder(resources_data=serialized) modules = { r.name: r for r in f2.indexed_resources() if r.flavor == "module" } self.assertEqual(len(modules), 2) self.assertIn("my_module", modules) self.assertIn("module_b", modules) self.assertEqual(modules["my_module"].in_memory_source, b"import io") self.assertEqual(modules["module_b"].in_memory_bytecode, b"dummy bytecode")
def get_resources_data(self) -> bytes: c = OxidizedResourceCollector(allowed_locations=["in-memory"]) for path in sys.path: if os.path.isdir(path): for i, resource in enumerate(find_resources_in_path(path)): c.add_in_memory(resource) if i == 10: break finder = OxidizedFinder() finder.add_resources(c.oxidize()[0]) return finder.serialize_indexed_resources()
def test_add_sys_path(self): c = OxidizedResourceCollector( policy="prefer-in-memory-fallback-filesystem-relative:prefix") for path in sys.path: if os.path.isdir(path): for resource in find_resources_in_path(path): c.add_in_memory(resource) c.add_filesystem_relative("", resource) resources, file_installs = c.oxidize() f = OxidizedFinder() f.add_resources(resources) with (self.td / "serialized").open("wb") as fh: fh.write(f.serialize_indexed_resources()) f = OxidizedFinder(resources_file=self.td / "serialized") self.assertGreaterEqual(len(f.indexed_resources()), len(resources)) for r in f.indexed_resources(): r.in_memory_source r.in_memory_bytecode