def test_urllib(self):
        c = OxidizedResourceCollector(
            allowed_locations=["filesystem-relative"])

        for path in sys.path:
            if os.path.isdir(path):
                for resource in find_resources_in_path(path):
                    if isinstance(resource, PythonModuleBytecode):
                        if resource.module.startswith("urllib"):
                            if resource.optimize_level == 0:
                                c.add_filesystem_relative("lib", resource)

        python_exe = os.environ.get("PYTHON_SYS_EXECUTABLE")
        with assert_tempfile_cleaned_up():
            resources, file_installs = c.oxidize(python_exe=python_exe)
        self.assertEqual(len(resources), len(file_installs))

        idx = None
        for i, resource in enumerate(resources):
            if resource.name == "urllib.request":
                idx = i
                break

        self.assertIsNotNone(idx)

        (path, data, executable) = file_installs[idx]
        self.assertEqual(
            path,
            pathlib.Path("lib") / "urllib" / "__pycache__" /
            ("request.%s.pyc" % sys.implementation.cache_tag),
        )

        self.assertTrue(data.startswith(importlib.util.MAGIC_NUMBER))
    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
Example #3
0
    def test_load_pkg_info(self):
        # In absence of a METADATA file, a PKG-INFO file will be read.
        pkginfo_path = self.td / "my_package-1.0.egg-info" / "PKG-INFO"
        pkginfo_path.parent.mkdir()

        with pkginfo_path.open("w", encoding="utf-8") as fh:
            fh.write("Name: my_package\n")
            fh.write("Version: 1.0\n")

        collector = OxidizedResourceCollector(allowed_locations=["in-memory"])
        for r in find_resources_in_path(self.td):
            collector.add_in_memory(r)

        f = OxidizedFinder()
        f.add_resources(
            collector.oxidize(
                python_exe=os.environ.get("PYTHON_SYS_EXECUTABLE"))[0])

        dists = list(f.find_distributions())
        self.assertEqual(len(dists), 1)

        metadata = dists[0].metadata
        self.assertIsInstance(metadata, email.message.Message)
        self.assertEqual(metadata["Name"], "my_package")
        self.assertEqual(metadata["Version"], "1.0")
    def test_urllib(self):
        c = OxidizedResourceCollector(policy="filesystem-relative-only:lib")

        for path in sys.path:
            if os.path.isdir(path):
                for resource in find_resources_in_path(path):
                    if isinstance(resource, PythonModuleBytecode):
                        if resource.module.startswith("urllib"):
                            if resource.optimize_level == 0:
                                c.add_filesystem_relative("lib", resource)

        resources, file_installs = c.oxidize()
        self.assertEqual(len(resources), len(file_installs))

        idx = None
        for i, resource in enumerate(resources):
            if resource.name == "urllib.request":
                idx = i
                break

        self.assertIsNotNone(idx)

        (path, data, executable) = file_installs[idx]
        self.assertEqual(
            path,
            pathlib.Path("lib") / "urllib" / "__pycache__" /
            ("request.%s.pyc" % sys.implementation.cache_tag),
        )

        self.assertTrue(data.startswith(importlib.util.MAGIC_NUMBER))
Example #5
0
    def _finder_from_td(self):
        collector = OxidizedResourceCollector(allowed_locations=["in-memory"])
        for r in find_resources_in_path(self.td):
            collector.add_in_memory(r)

        f = OxidizedFinder()
        f.add_resources(collector.oxidize()[0])

        return f
Example #6
0
    def _finder_from_td(self):
        collector = OxidizedResourceCollector(policy="in-memory-only")
        for r in find_resources_in_path(self.td):
            collector.add_in_memory(r)

        f = OxidizedFinder()
        f.add_resources(collector.oxidize()[0])

        return f
    def _finder_from_td(self):
        collector = OxidizedResourceCollector(allowed_locations=["in-memory"])
        for r in find_resources_in_path(self.td):
            collector.add_in_memory(r)

        f = OxidizedFinder()
        f.add_resources(
            collector.oxidize(
                python_exe=os.environ.get("PYTHON_SYS_EXECUTABLE"))[0])

        return f
    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_tempdir_error(self):
        class BadTempDir(tempfile.TemporaryDirectory):
            def cleanup(self):
                super().cleanup()
                raise FileNotFoundError(self.name)

        python_exe = os.environ.get("PYTHON_SYS_EXECUTABLE")
        c = OxidizedResourceCollector(allowed_locations=["in-memory"])
        stderr = StringIO()
        assertion = assert_tempfile_cleaned_up(BadTempDir)
        with assertion as (TrackingTempDir, cleanup), redirect_stderr(stderr):
            oxide = c.oxidize(python_exe=python_exe)
        self.assertRegex(
            stderr.getvalue(),
            fr"""Exception ignored in: <bound method {cleanup.__qualname__} """
            fr"""of <{TrackingTempDir.__name__} (['"]){tempfile.gettempdir()}"""
            fr"""[/\\].+\1>>""")
    def test_source_module(self):
        c = OxidizedResourceCollector(policy="in-memory-only")

        source_path = self.td / "foo.py"

        with source_path.open("wb") as fh:
            fh.write(b"import io\n")

        for resource in find_resources_in_path(self.td):
            c.add_in_memory(resource)

        f = OxidizedFinder()
        f.add_resources(c.oxidize()[0])

        resources = [r for r in f.indexed_resources() if r.name == "foo"]
        self.assertEqual(len(resources), 1)

        r = resources[0]
        self.assertEqual(r.in_memory_source, b"import io\n")
    def test_source_module(self):
        c = OxidizedResourceCollector(allowed_locations=["in-memory"])

        source_path = self.td / "foo.py"

        with source_path.open("wb") as fh:
            fh.write(b"import io\n")

        for resource in find_resources_in_path(self.td):
            c.add_in_memory(resource)

        f = OxidizedFinder()
        python_exe = os.environ.get("PYTHON_SYS_EXECUTABLE")
        with assert_tempfile_cleaned_up():
            oxide = c.oxidize(python_exe=python_exe)
        f.add_resources(oxide[0])

        resources = [r for r in f.indexed_resources() if r.name == "foo"]
        self.assertEqual(len(resources), 1)

        r = resources[0]
        self.assertEqual(r.in_memory_source, b"import io\n")
Example #12
0
    def test_load_pkg_info(self):
        # In absence of a METADATA file, a PKG-INFO file will be read.
        pkginfo_path = self.td / "my_package-1.0.egg-info" / "PKG-INFO"
        pkginfo_path.parent.mkdir()

        with pkginfo_path.open("w", encoding="utf-8") as fh:
            fh.write("Name: my_package\n")
            fh.write("Version: 1.0\n")

        collector = OxidizedResourceCollector(policy="in-memory-only")
        for r in find_resources_in_path(self.td):
            collector.add_in_memory(r)

        f = OxidizedFinder()
        f.add_resources(collector.oxidize()[0])

        dists = f.find_distributions()
        self.assertEqual(len(dists), 1)

        metadata = dists[0].metadata
        self.assertIsInstance(metadata, email.message.Message)
        self.assertEqual(metadata["Name"], "my_package")
        self.assertEqual(metadata["Version"], "1.0")
    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