def test_allow_current_context(self):
        """Check that has_python_package only creates a context when needed."""
        package = finder.get_nearest_rez_package(_CURRENT_DIRECTORY)
        inspection.in_valid_context = _check_called(
            inspection.in_valid_context)

        self.assertTrue(
            inspection.has_python_package(package,
                                          allow_current_context=False))
        self.assertFalse(inspection.in_valid_context.was_run)
        self.assertTrue(
            inspection.has_python_package(package, allow_current_context=True))
        self.assertTrue(inspection.in_valid_context.was_run)
Пример #2
0
def is_not_a_python_package(package):
    """Check if the given package has at least one Python package / file inside of it.

    Args:
        package (:class:`rez.packages_.Package`): The Rez package to check for Python packages.

    Returns:
        str: A generic message to report if this class finds a non-python Rez package.

    """
    return not inspection.has_python_package(package)
    def test_build(self):
        """A build Rez package should return True if it has a Python module inside of it."""
        root = tempfile.mkdtemp(
            suffix="_a_rez_source_package_with_no_python_modules_until_build")
        self.delete_item_later(root)
        root = os.path.join(root, "some_package")
        os.makedirs(root)

        with open(os.path.join(root, "package.py"), "w") as handler:
            template = textwrap.dedent("""\
                name = "some_package"
                version = "1.0.0"
                requires = ["python-{sys.version_info.major}"]
                build_command = "python {{root}}/rezbuild.py {{install}}"

                def commands():
                    import os

                    env.PYTHONPATH.append(os.path.join("{{root}}", "python"))
                """)
            handler.write(template.format(sys=sys))

        with open(os.path.join(root, "rezbuild.py"), "w") as handler:
            handler.write(_get_rezbuild_text())

        python_root = os.path.join(root, "python", "some_package")
        os.makedirs(python_root)

        open(os.path.join(python_root, "__init__.py"), "a").close()

        package = finder.get_nearest_rez_package(root)

        install_root = tempfile.mkdtemp(suffix="_installed_rez_package")
        self.delete_item_later(install_root)

        build_package = creator.build(package, install_root, quiet=True)

        self.assertTrue(
            inspection.has_python_package(
                build_package,
                paths=[root] + config.packages_path,  # pylint: disable=no-member
                allow_build=True,
            ))
    def test_source_with_variants(self):
        """A Rez source package that contains a Python package should return true.

        If that source package has variants, as long as the variant contains
        a Python module, it shouldn't need to be built and still return True.

        """
        root = tempfile.mkdtemp(suffix="_a_rez_source_package_with_a_variant")
        self.delete_item_later(root)
        root = os.path.join(root, "some_package")
        os.makedirs(root)

        with open(os.path.join(root, "package.py"), "w") as handler:
            handler.write(
                textwrap.dedent("""\
                    name = "some_package"
                    version = "1.0.0"
                    variants = [["python-2"]]

                    def commands():
                        import os

                        env.PYTHONPATH.append(os.path.join("{root}", "python"))
                    """))

        python_root = os.path.join(root, "python", "some_package")
        os.makedirs(python_root)

        open(os.path.join(python_root, "__init__.py"), "a").close()
        open(os.path.join(python_root, "some_module.py"), "a").close()

        local.LocalBuildProcess.build = _check_called(
            local.LocalBuildProcess.build)

        package = finder.get_nearest_rez_package(root)
        self.assertTrue(
            inspection.has_python_package(
                package,
                paths=[root] + config.packages_path,  # pylint: disable=no-member
                allow_build=False,
            ))

        self.assertFalse(local.LocalBuildProcess.build.was_run)
    def test_source(self):
        """Return True if a Rez package with at least one Python module is found."""
        root = tempfile.mkdtemp(
            suffix="_a_rez_source_package_that_has_at_least_1_python_module")
        self.delete_item_later(root)
        root = os.path.join(root, "some_package")
        os.makedirs(root)

        with open(os.path.join(root, "package.py"), "w") as handler:
            handler.write(
                textwrap.dedent("""\
                    name = "some_package"
                    version = "1.0.0"

                    def commands():
                        import os

                        env.PYTHONPATH.append(os.path.join("{root}", "python"))
                    """))

        python_root = os.path.join(root, "python", "some_package")
        os.makedirs(python_root)

        open(os.path.join(python_root, "__init__.py"), "a").close()
        open(os.path.join(python_root, "some_module.py"), "a").close()

        local.LocalBuildProcess.build = _check_called(
            local.LocalBuildProcess.build)

        package = finder.get_nearest_rez_package(root)
        self.assertTrue(
            inspection.has_python_package(
                package,
                paths=[root] + config.packages_path,  # pylint: disable=no-member
                allow_build=False,
            ))

        self.assertFalse(local.LocalBuildProcess.build.was_run)
 def test_python_package_invalid(self):
     """Make sure a non-package input raises an exception."""
     with self.assertRaises(ValueError):
         inspection.has_python_package(None)
 def test_python_package_true(self):
     """Make sure a source Rez package with no variant still works."""
     package = finder.get_nearest_rez_package(
         os.path.dirname(_CURRENT_DIRECTORY))
     self.assertTrue(inspection.has_python_package(package))