Exemplo n.º 1
0
    def test_build_requires(self):
        """Include `build_requires` in the Rez resolve."""
        directory = _make_test_packages()

        build_directory = tempfile.mkdtemp(
            suffix="_BuildRequires_test_private_build_requires"
        )
        atexit.register(functools.partial(shutil.rmtree, build_directory))

        package_directory = os.path.join(directory, "package_b")

        creator.build(
            finder.get_nearest_rez_package(package_directory),
            build_directory,
            quiet=True,
            packages_path=[directory],
        )

        with _keep_pwd():
            os.chdir(package_directory)

            result = _test(
                "directory --directory '{directory}' unittest_* --build-requires".format(
                    directory=os.path.join(build_directory, "package_b", "1.1.0"),
                ),
                directory,
                build_directory=build_directory,
            )

        self.assertEqual({"build_a-1.2.0", "package_a", "package_b-1.1.0"}, result)
Exemplo n.º 2
0
    def _make_installed_package(self, name, text, packages_path=None):
        """Create a Rez source package and install it into a temporary directory.

        All temporary directories will be marked for clean-up. Clean-up
        occurs after each test is run.

        Args:
            name (str):
                The name of the source Rez packge to create.
            text (str):
                The text that will be used for a "package.py" file.
            packages_path (list[str], optional):
                The paths that will be used to search for Rez packages while
                building. This is usually to make it easier to find package
                dependencies. If `packages_path` is not defined, Rez will
                use its default paths. Default is None.

        Returns:
            :class:`rez.developer_package.DeveloperPackage`:
                The package the represents the newly-built package.

        """
        directory = make_fake_source_package(name, text)

        package = packages_.get_developer_package(directory)
        new_package = creator.build(package,
                                    tempfile.mkdtemp(),
                                    packages_path=packages_path,
                                    quiet=True)

        self.delete_item_later(os.path.dirname(directory))
        self.delete_item_later(
            inspection.get_packages_path_from_package(new_package))

        return new_package
    def test_build_build(self):
        """Test that a built Rez package cannot be built again."""
        root = tempfile.mkdtemp("_fake_source_package_with_build_method")
        build_package, build_root = _build_source_that_has_build_method(root)
        self.delete_item_later(root)
        self.delete_item_later(build_root)

        # Typically, you can't build something that has already been
        # built Unless the build process also copies the files needed to
        # build the package again. Which is non-standard and weird but I
        # guess still possible.
        #
        another_root = tempfile.mkdtemp(suffix="_another_root")
        self.delete_item_later(another_root)

        with self.assertRaises(exceptions.BuildSystemError):
            creator.build(build_package, another_root, quiet=True)
    def test_no_build_method(self):
        """Fail to build a Rez package if it has no declared way to build the package."""
        package_root = tempfile.mkdtemp(
            "_fake_source_package_with_no_build_method")

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

        package = packages_.get_developer_package(package_root)
        root = tempfile.mkdtemp(suffix="_build_test")
        self.delete_item_later(root)

        with self.assertRaises(exceptions.BuildSystemError):
            creator.build(package, root, quiet=True)
Exemplo n.º 5
0
    def test_no_requires(self):
        """Allow the build requires flags even if the Rez package doesn't define them."""
        directory = _make_test_packages()

        build_directory = tempfile.mkdtemp(
            suffix="_BuildRequires_test_private_build_requires"
        )
        atexit.register(functools.partial(shutil.rmtree, build_directory))

        package_directory = os.path.join(directory, "package_a")

        creator.build(
            finder.get_nearest_rez_package(package_directory),
            build_directory,
            quiet=True,
            packages_path=[directory],
        )

        with _keep_pwd():
            os.chdir(package_directory)

            result = _test(
                " ".join(
                    [
                        "directory",
                        "--directory",
                        "'{directory}'".format(
                            directory=os.path.join(
                                build_directory,
                                "package_a",
                                "1.0.0",
                            )
                        ),
                        "name_A",
                        "--build-requires",
                        "--private-build-requires",
                    ]
                ),
                directory,
                build_directory=build_directory,
            )

        self.assertEqual({"package_a-1.0.0", "package_d-1.1.0"}, result)
    def test_installed_package_with_variants(self):
        """Create a Rez package with variants and get the folders affecting PYTHONPATH."""
        dependencies = self._make_test_dependencies()
        package = self._make_fake_rez_source_package(
            "some_fake_package",
            textwrap.dedent("""\
                name = "some_fake_package"
                version = "1.0.0"
                variants = [["some_dependency-1", "another_one-2.3"], ["another_one-2.3"]]
                build_command = "echo 'foo'"

                def commands():
                    import os

                    env.PYTHONPATH.append(os.path.join("{root}", "python"))
                """),
        )
        install_path = tempfile.mkdtemp(suffix="_install_path")
        self.delete_item_later(install_path)

        build_package = creator.build(
            package,
            install_path,
            packages_path=dependencies + config.packages_path,  # pylint: disable=no-member
            quiet=True,
        )

        context = resolved_context.ResolvedContext(
            [
                "{build_package.name}==1.0.0".format(
                    build_package=build_package)
            ],
            package_paths=[
                inspection.get_packages_path_from_package(build_package)
            ] + dependencies + config.packages_path,  # pylint: disable=no-member
        )

        python_files = inspection.get_package_python_paths(
            build_package,
            context.get_environ().get("PYTHONPATH", "").split(os.pathsep))

        self.assertEqual(
            {
                os.path.join(
                    install_path,
                    "some_fake_package",
                    "1.0.0",
                    "another_one-2.3",
                    "python",
                )
            },
            python_files,
        )
Exemplo n.º 7
0
    def _verify_installed_package(self, directory):
        """Build a Rez package and make sure it builds correctly.

        Args:
            directory (str): The absolute path where a Rez source package is defined.

        """
        install_directory = tempfile.mkdtemp(
            prefix="rez_pip_boy_", suffix="_verify_installed_package")
        atexit.register(functools.partial(shutil.rmtree, install_directory))

        package = finder.get_nearest_rez_package(directory)
        creator.build(package, install_directory, quiet=True)

        installed_package_directory = os.path.join(install_directory,
                                                   package.name,
                                                   str(package.version))
        self.assertTrue(
            os.path.isfile(
                os.path.join(installed_package_directory, "package.py")))
        self.assertFalse(
            os.path.isfile(
                os.path.join(installed_package_directory, "rezbuild.py")))
Exemplo n.º 8
0
    def test_from_package_pwd(self):
        """Get the resolve from a package's directory."""
        directory = _make_test_packages()
        package_directory = os.path.join(directory, "package_b")

        build_directory = tempfile.mkdtemp(suffix="_Run_test_from_package_pwd")
        atexit.register(functools.partial(shutil.rmtree, build_directory))

        creator.build(
            finder.get_nearest_rez_package(package_directory),
            build_directory,
            quiet=True,
        )

        with _keep_pwd():
            os.chdir(package_directory)

            result = _test(
                "directory unittest_*",
                directory,
                build_directory=build_directory,
            )

        self.assertEqual({"package_a", "package_b-1.1.0"}, result)
Exemplo n.º 9
0
    def test_from_package_directory(self):
        """Get the resolve from a package's directory."""
        directory = _make_test_packages()

        build_directory = tempfile.mkdtemp(suffix="_Run_test_from_package_directory")
        atexit.register(functools.partial(shutil.rmtree, build_directory))

        package_directory = os.path.join(directory, "package_b")

        creator.build(
            finder.get_nearest_rez_package(package_directory),
            build_directory,
            quiet=True,
        )

        result = _test(
            "directory --directory '{directory}' unittest_*".format(
                directory=os.path.join(build_directory, "package_b", "1.1.0"),
            ),
            directory,
            build_directory=build_directory,
        )

        self.assertEqual({"package_a", "package_b-1.1.0"}, result)
    def test_get_context(self):
        """Check that Rez can resolve a context correctly and return it."""
        directory = testing_packaging.make_fake_source_package(
            "some_package",
            textwrap.dedent("""\
                name = "some_package"
                version = "1.0.0"
                requires = [
                    "some_dependency-1",
                ]
                """),
        )
        self.delete_item_later(os.path.dirname(directory))

        package = packages_.get_developer_package(directory)

        dependency_directory = testing_packaging.make_fake_source_package(
            "some_dependency",
            textwrap.dedent("""\
                name = "some_dependency"
                version = "1.1.0"
                build_command = "echo 'foo'"
                """),
        )
        self.delete_item_later(os.path.dirname(dependency_directory))

        dependency_package = packages_.get_developer_package(
            dependency_directory)
        dependency_build_path = tempfile.mkdtemp(
            suffix="_dependency_build_path")
        self.delete_item_later(dependency_build_path)
        dependency_package = creator.build(dependency_package,
                                           dependency_build_path,
                                           quiet=True)
        dependency_path = inspection.get_packages_path_from_package(
            dependency_package)

        context = dict()

        with testing_packaging.override_packages_path([dependency_path],
                                                      prepend=True):
            packaging.SourceResolvedContext.run(package, context)

        self.assertTrue(lint_constant.RESOLVED_SOURCE_CONTEXT in context)
        rez_resolved = context[lint_constant.RESOLVED_SOURCE_CONTEXT]
        self.assertEqual(os.path.join(directory),
                         rez_resolved.get_environ()["REZ_SOME_PACKAGE_ROOT"])
        self.assertEqual(set(), context[lint_constant.DEPENDENT_PACKAGES])
Exemplo n.º 11
0
def make_build_python_package(text, name, version, root):
    """Create a Rez-python package and build it to a temporary directory.

    Args:
        text (str): The contents of the package.py file that will be created.
        name (str): The name of the Rez package.
        version (str): The major, minor, and patch information for the Rez package. e.g. "1.0.0".
        root (str): An absolute folder on-disk where the package.py will be written to.

    Returns:
        :class:`rez.developer_package.DeveloperPackage`: The built package.

    """
    package = make_source_python_package(text, name, version, root)
    build_directory = tempfile.mkdtemp(suffix="_build_python_package")
    source_package = packages_.get_developer_package(os.path.dirname(package))

    return creator.build(source_package, build_directory).filepath
    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 _build_source_that_has_build_method(root, packages_path=None):
    """Create a source Rez package and then build it to some temporary location."""
    with open(os.path.join(root, "package.py"), "w") as handler:
        handler.write(
            textwrap.dedent("""\
            name = "foo"
            version = "1.0.0"
            build_command = "echo 'this command does not need to do anything'"
            """))

    package = packages_.get_developer_package(root)
    build_root = tempfile.mkdtemp(suffix="_build_test")

    return (
        creator.build(package,
                      build_root,
                      packages_path=packages_path,
                      quiet=True),
        build_root,
    )
    def test_build_symlinked(self):
        """Create a built Rez package that symlinks back to a source Rez package.

        The source Rez package is inside of a git repository. And this
        repository will be referenced to create the new documentation +
        pull request.

        Raises:
            RuntimeError: If for whatever reason a built Rez package could not be created.

        """
        package = self._make_symlinkable_source_package()
        package = self._wrap_in_repository(package)

        build_root = tempfile.mkdtemp(suffix="_build_folder")
        self.delete_item_later(build_root)
        build_package = creator.build(package, build_root)

        if not inspection.is_built_package(build_package):
            raise RuntimeError("Package is not built.")

        self._test_package([build_package])
Exemplo n.º 15
0
    def _run_command_with_results(cls, package, arguments):
        """Run the main bump command on a Rez package.

        Args:
            package (:class:`rez.developer_package.DeveloperPackage`):
                The Rez package that will be changed. Any command
                run by this function will do so while cd'ed into the
                directory of this package.

                To be clear, this Rez package represents one of the downstream packages
                whose version + dependenc(ies) will be changed.
            arguments (:class:`argparse.Namespace`):
                The user-provided, plug-in specific arguments.
                Specifically, this should bring in

                - The command that the user wants to run, per-package
                - The name of the ticket that will be used for git branches
                - The option to "raise an exception if any error occurs"
                  or whether it's OK to continue.

        Returns:
            str: Any error message that occurred from this command, if any.

        """
        build_path = tempfile.mkdtemp(suffix="_pre_bump_build_path")
        pre_bump_build = True

        try:
            creator.build(package,
                          build_path,
                          packages_path=arguments.additional_paths)
        except RuntimeError:
            pre_bump_build = False

        pre_bump_tests = _run_test(package, paths=arguments.additional_paths)

        error = ""
        post_bump_build = "Not run"
        post_bump_tests = "Not run"

        try:
            package = cls._bump(package, arguments.new, arguments.packages)
        except Exception:  # pylint: disable=broad-except
            error = 'Bumping package "{package.name}" failed.'.format(
                package=package)
            _LOGGER.warning('Package "%s" bump failed.', package)
        else:
            build_path = tempfile.mkdtemp(suffix="_post_bump_build_path")
            post_bump_build = True

            try:
                creator.build(package,
                              build_path,
                              packages_path=arguments.additional_paths)
            except Exception:  # pylint: disable=broad-except
                post_bump_build = False

            post_bump_tests = _run_test(package,
                                        paths=arguments.additional_paths)

        results = _Results(pre_bump_build, pre_bump_tests, post_bump_build,
                           post_bump_tests)

        return error, results
Exemplo n.º 16
0
    def test_egg(self, _create_pull_request):
        """Bump a released Rez package which only contains a single zipped .egg file."""
        def _create_package(root, name, version, requirements=None):
            text = textwrap.dedent("""\
                name = "{name}"
                version = "{version}"
                description = "A package.py Rez package that won't be converted."
                build_command = "python {{root}}/rezbuild.py"

                def commands():
                    import os

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

            text = text.format(name=name, version=version)

            if requirements:
                text += "\nrequires = {requirements!r}".format(
                    requirements=requirements)

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

            with open(os.path.join(root, "rezbuild.py"), "w") as handler:
                handler.write(
                    textwrap.dedent("""\
                        #!/usr/bin/env python
                        # -*- coding: utf-8 -*-

                        import os
                        import shutil
                        import zipfile


                        def main():
                            source = os.environ["REZ_BUILD_SOURCE_PATH"]
                            build = os.environ["REZ_BUILD_PATH"]

                            python_directory = os.path.join(source, "python")

                            with zipfile.ZipFile(os.path.join(build, "python.egg"), "w") as handler:
                                for root, folders, files in os.walk(python_directory):
                                    relative_root = os.path.relpath(root, python_directory)

                                    for folder in folders:
                                        handler.write(
                                            os.path.join(root, folder),
                                            os.path.join(relative_root, folder),
                                        )

                                    for file_ in files:
                                        handler.write(
                                            os.path.join(root, file_),
                                            os.path.join(relative_root, file_),
                                        )

                            shutil.copy2(
                                handler.filename,
                                os.path.join(
                                    os.environ["REZ_BUILD_INSTALL_PATH"],
                                    os.path.basename(handler.filename)
                                ),
                            )


                        if __name__ == "__main__":
                            main()
                        """))

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

            common.make_files(
                {
                    "something": {
                        "__init__.py": None,
                        "inner_folder": {
                            "__init__.py": None,
                        },
                    },
                    "some_other_package_folder": {
                        "__init__.py": None,
                        "some_module.py": None,
                    },
                },
                python_root,
            )

        def _make_package_with_contents(root, name, version, create_package):
            directory = os.path.join(root, name)
            os.makedirs(directory)

            create_package(directory, name, version)

            return finder.get_nearest_rez_package(directory)

        root = tempfile.mkdtemp(suffix="_test_is_definition_build_package")
        self.delete_item_later(root)

        packages = [
            _make_package_with_contents(root, "another_package", "1.2.0",
                                        _create_package),
            _make_package_with_contents(
                root,
                "some_package",
                "1.2.0",
                functools.partial(_create_package,
                                  requirements=["another_package-1"]),
            ),
        ]

        repository, packages, remote_root = testify.make_fake_repository(
            packages, root)
        self.delete_item_later(repository.working_dir)
        self.delete_item_later(remote_root)

        release_path = tempfile.mkdtemp(
            suffix="_a_release_location_for_testing")
        self.delete_item_later(release_path)

        options, parser = _make_fake_release_data()

        for package in packages:
            creator.release(
                finder.get_package_root(package),
                options,
                parser,
                release_path,
                search_paths=[repository.working_dir],
                quiet=True,
            )

        text = 'PR github-token --packages another_package-1.1 --instructions "Do it!" --new minor'

        text = shlex.split(text)
        sys.argv[1:] = text

        # Simulate the act of a user with a locally built package that
        # they want to test against other existing releases.
        #
        source_root = tempfile.mkdtemp(suffix="_another_source_root")
        source_package = _make_package_with_contents(source_root,
                                                     "another_package",
                                                     "1.3.0", _create_package)
        install_path = tempfile.mkdtemp(suffix="_install_path")
        local_built_package = creator.build(source_package, install_path)

        arguments = _BumpArguments(
            additional_paths=[
                inspection.get_packages_path_from_package(local_built_package)
            ],
            instructions="Do something!",
            new="minor",
            packages=["another_package-1.3"],
            pull_request_name="PR",
            token="github-token",
            ssl_no_verify=False,
            cached_users="",
            fallback_reviewers=None,
            base_url="",
        )

        with rez_configuration.patch_release_packages_path(release_path):
            ran_packages, unfixed, invalids, skips = _get_test_results(
                "bump", arguments=arguments, paths={release_path})

        package_file = next(iter(ran_packages)).filepath

        with open(package_file, "r") as handler:
            text = handler.read()

        expected = textwrap.dedent("""\
            name = "some_package"
            version = "1.3.0"
            description = "A package.py Rez package that won't be converted."
            build_command = "python {root}/rezbuild.py"

            def commands():
                import os

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

            requires = [
                "another_package-1.3",
            ]""")

        self.assertEqual(expected, text)
        self.assertEqual((set(), [], []), (unfixed, invalids, skips))
        self.assertEqual(1, _create_pull_request.call_count)