Пример #1
0
    def setUp(self):
        """
        Setups everything needed for the test.
        """

        self.helper = CommandHelper()

        return super().setUp()
Пример #2
0
    def get_remote_destination():
        """
        Provides the remote destination to use.

        :raise PyFunceble.cli.continuous_integration.exceptions.RemoteURLNotFound:
            When we could not determine the remote destination.
        """

        regex = r"(?:[a-z]+(?:\s+|\t+))(.*)(?:(?:\s+|\t+)\([a-z]+\))"
        remote_of_interest = [
            x for x in CommandHelper("git remote -v").execute().splitlines()
            if "(fetch)" in x
        ][0]

        filtered = RegexHelper(regex).match(remote_of_interest,
                                            return_match=True,
                                            group=1)

        if filtered:
            if "@" in filtered:
                return filtered[filtered.find("@") + 1:]
            if "//" in filtered:
                return filtered[filtered.find("//") + 2:]

        raise PyFunceble.cli.continuous_integration.exceptions.RemoteURLNotFound(
        )
    def launch_marker_in_last_commit() -> bool:
        """
        Check if the launch marker is into the last commit message.
        """

        return RegexHelper(infrastructure.MARKERS["launch"]).match(
            CommandHelper("git log -1").execute(), return_match=False)
Пример #4
0
    def test_run(self) -> None:
        """
        Tests the method which let us run a command.
        """

        if PlatformUtility.is_unix():
            expected = ["Hello, World!"]
            actual = list(CommandHelper("echo 'Hello, World!'").run())

            self.assertEqual(expected, actual[:1])
Пример #5
0
    def test_run_to_stdout(self) -> None:
        """
        Tests the method which let us run a command an output to stdout.
        """

        if PlatformUtility.is_unix():
            expected = "Hello, World!\n"

            CommandHelper("echo 'Hello, World!'").run_to_stdout()

            actual = sys.stdout.getvalue()

            self.assertEqual(expected, actual)
Пример #6
0
    def test_set_encoding_through_init(self) -> None:
        """
        Tests the overwritting of the encoding to use through the class
        constructor.
        """

        given = "latin-1"
        expected = "latin-1"

        helper = CommandHelper(encoding=given)
        actual = helper.encoding

        self.assertEqual(expected, actual)
Пример #7
0
    def test_set_command_through_init(self) -> None:
        """
        Tests the overwritting of the command to work through the class
        constructor.
        """

        given = "echo 'Hello, World'"
        expected = "echo 'Hello, World'"

        helper = CommandHelper(given)
        actual = helper.command

        self.assertEqual(expected, actual)
Пример #8
0
    def update_documentation() -> "ProductionPrep":
        """
        Updates the code documentation.

        :raise RuntimeError:
            When one of the wanted directory is not found.
        """

        PyFunceble.facility.Logger.info(
            "Started to update and generate the documentation.",
        )

        docs_dir_helper = DirectoryHelper("docs")
        source_code_dir_helper = DirectoryHelper("PyFunceble")

        if not docs_dir_helper.exists():
            raise RuntimeError(f"{docs_dir_helper.realpath!r} not found.")

        if not source_code_dir_helper.exists():
            raise RuntimeError(f"{source_code_dir_helper.realpath!r} not found.")

        header = "Code Documentation"
        source_code_destination = os.path.join(docs_dir_helper.realpath, "code")

        CommandHelper(
            f"sphinx-apidoc -d 5 -f -H {header!r} -o "
            f"{source_code_destination!r} {source_code_dir_helper.realpath}"
        ).execute(raise_on_error=True)

        docs_destination = os.path.join(docs_dir_helper.realpath, "_build", "html")

        CommandHelper(
            f"sphinx-build -a -Q {docs_dir_helper.realpath!r} {docs_destination!r}"
        ).execute(raise_on_error=False)

        PyFunceble.facility.Logger.info(
            "Finished to update and generate the documentation.",
        )
Пример #9
0
    def test_execute_error(self, communicate_patch) -> None:
        """
        Tests the method which executes a command for the case that an
        error is produced by the command.
        """

        if PlatformUtility.is_unix():
            expected = "This is an error."

            communicate_patch.return_value = (b"", b"This is an error.")

            given = "echo 'This is an error.' 1>&2"
            actual = CommandHelper(given).execute()

            self.assertEqual(expected, actual)
Пример #10
0
    def bypass(self) -> None:
        """
        Stops everything if the latest commit message match any of those:

            - :code:`[PyFunceble skip]` (case insensitive)
            - :code:`[PyFunceble-skip]` (case insensitive)
            - :attr:`~PyFunceble.cli.continuous_integration.base.end_commit_marker`
        """

        our_marker = [
            "[pyfunceble skip]", "[pyfunceble-skip]", self.end_commit_marker
        ]
        latest_commit = CommandHelper("git log -1").execute().lower()

        if any(x.lower() in latest_commit for x in our_marker):
            PyFunceble.facility.Logger.info(
                "Bypass marker caught. Saving and stopping process.")

            raise PyFunceble.cli.continuous_integration.exceptions.StopExecution(
            )
Пример #11
0
    def exec_command(command: str, allow_stdout: bool) -> None:
        """
        Exceutes the given command.

        :param command:
            The command to execute.

        :param allow_stdout:
            Allows us to return the command output to stdout.
        """

        PyFunceble.facility.Logger.debug("Executing %r", command)

        command_helper = CommandHelper(command)

        if allow_stdout:
            command_helper.run_to_stdout()
        else:
            command_helper.execute()
Пример #12
0
class TestCommandHelper(StdoutBase):
    """
    Tests of the command helper.
    """

    def setUp(self):
        """
        Setups everything needed for the test.
        """

        self.helper = CommandHelper()

        return super().setUp()

    def tearDown(self):
        """
        Destroys everything needed for the test.
        """

        del self.helper

        return super().tearDown()

    def test_set_command_return(self) -> None:
        """
        Tests the response from the method which let us set the command to work
        with.
        """

        given = "echo 'Hello, World'"

        actual = self.helper.set_command(given)

        self.assertIsInstance(actual, CommandHelper)

    def test_set_command_method(self) -> None:
        """
        Tests the method which let us set the command to work with.
        """

        given = "echo 'Hello, World'"
        expected = "echo 'Hello, World'"

        self.helper.set_command(given)

        actual = self.helper.command

        self.assertEqual(expected, actual)

    def test_set_command_attribute(self) -> None:
        """
        Tests overwritting of the :code:`command` attribute.
        """

        given = "echo 'Hello, World'"
        expected = "echo 'Hello, World'"

        self.helper.command = given
        actual = self.helper.command

        self.assertEqual(expected, actual)

    def test_set_command_through_init(self) -> None:
        """
        Tests the overwritting of the command to work through the class
        constructor.
        """

        given = "echo 'Hello, World'"
        expected = "echo 'Hello, World'"

        helper = CommandHelper(given)
        actual = helper.command

        self.assertEqual(expected, actual)

    def test_set_command_list_given(self) -> None:
        """
        Tests the method which let us set the command to execute for
        the case that a list is given.
        """

        given = ["echo", "'Hello, World'"]
        expected = "echo 'Hello, World'"

        self.helper.set_command(given)
        actual = self.helper.command

        self.assertEqual(expected, actual)

    def test_set_command_not_list_or_str(self) -> None:
        """
        Tests the method which let us set the command to execute for the case
        that a list nor a str is given.
        """

        given = True

        self.assertRaises(TypeError, lambda: self.helper.set_command(given))

    def test_set_encoding_return(self) -> None:
        """
        Tests the response from the method which let us set the encoding to use.
        """

        given = "latin-1"

        actual = self.helper.set_encoding(given)

        self.assertIsInstance(actual, CommandHelper)

    def test_set_encoding_method(self) -> None:
        """
        Tests the method which let us set the encoding to use.
        """

        given = "latin-1"
        expected = "latin-1"

        self.helper.set_encoding(given)

        actual = self.helper.encoding

        self.assertEqual(expected, actual)

    def test_set_encoding_attribute(self) -> None:
        """
        Tests overwritting of the :code:`encoding` attribute.
        """

        given = "latin-1"
        expected = "latin-1"

        self.helper.encoding = given
        actual = self.helper.encoding

        self.assertEqual(expected, actual)

    def test_set_encoding_through_init(self) -> None:
        """
        Tests the overwritting of the encoding to use through the class
        constructor.
        """

        given = "latin-1"
        expected = "latin-1"

        helper = CommandHelper(encoding=given)
        actual = helper.encoding

        self.assertEqual(expected, actual)

    def test_set_encoding_not_str(self) -> None:
        """
        Tests the method which let us set  the encoding to use for the case
        that the given encoding is a string.
        """

        given = ["hello", "world"]

        self.assertRaises(TypeError, lambda: self.helper.set_encoding(given))

    def test_execute(self) -> None:
        """
        Tests the method which executes a command.
        """

        if PlatformUtility.is_unix():
            given = "echo 'Hello, World!'"
            expected = "Hello, World!\n"
            actual = self.helper.set_command(given).execute()

            self.assertEqual(expected, actual)

    def test_execute_empty_output(self) -> None:
        """
        Tests the method which executes a command for the case that the given
        command is empty.
        """

        if PlatformUtility.is_unix():
            expected = ""
            actual = self.helper.set_command("printf ''").execute()

            self.assertEqual(expected, actual)

    @unittest.mock.patch.object(Popen, "communicate")
    def test_execute_error(self, communicate_patch) -> None:
        """
        Tests the method which executes a command for the case that an
        error is produced by the command.
        """

        if PlatformUtility.is_unix():
            expected = "This is an error."

            communicate_patch.return_value = (b"", b"This is an error.")

            given = "echo 'This is an error.' 1>&2"
            actual = CommandHelper(given).execute()

            self.assertEqual(expected, actual)

    @unittest.mock.patch.object(Popen, "communicate")
    def test_execute_error_exception(self, communicate_patch) -> None:
        """
        Tests the method which executes a command for the case that an
        error is produced by the command and the end-user want an exception when
        such cases happen.
        """

        if PlatformUtility.is_unix():
            communicate_patch.return_value = (b"", b"This is an error.")

            given = "echo 'This is an error.' 1>&2"
            self.helper.set_command(given)

            self.assertRaises(
                RuntimeError, lambda: self.helper.execute(raise_on_error=True)
            )

    def test_run(self) -> None:
        """
        Tests the method which let us run a command.
        """

        if PlatformUtility.is_unix():
            expected = ["Hello, World!"]
            actual = list(CommandHelper("echo 'Hello, World!'").run())

            self.assertEqual(expected, actual[:1])

    def test_run_to_stdout(self) -> None:
        """
        Tests the method which let us run a command an output to stdout.
        """

        if PlatformUtility.is_unix():
            expected = "Hello, World!\n"

            CommandHelper("echo 'Hello, World!'").run_to_stdout()

            actual = sys.stdout.getvalue()

            self.assertEqual(expected, actual)
    def convert_data_for_system(data: dict) -> dict:
        """
        Given the content of the info file, we convert it into something our
        system may understand.

        .. warning::
            This method also delete the keys that are scheduled or declared
            for deletion

        :param data:
            The data to work with.
        """

        result = dict()

        for key, value in dict(data).items():
            if key in infrastructure.ADMINISTRATION_INDEXES["delete"]:
                continue

            if key == "name":
                result[key] = (
                    CommandHelper("basename $(git rev-parse --show-toplevel)"
                                  ).execute().strip())
            elif key in infrastructure.ADMINISTRATION_INDEXES["bool"]:
                result[key] = bool(int(value))
            elif key in infrastructure.ADMINISTRATION_INDEXES["int"]:
                result[key] = int(value)
            elif key in infrastructure.ADMINISTRATION_INDEXES["dict"]:
                result[key] = dict(value)
            elif key in infrastructure.ADMINISTRATION_INDEXES["datetime"]:
                try:
                    result[key] = datetime.fromisoformat(value)
                except ValueError:
                    result[key] = datetime.utcnow()
            elif key in infrastructure.ADMINISTRATION_INDEXES["epoch"]:
                result[key] = datetime.fromtimestamp(value)
            else:
                result[key] = value

        if "pyfunceble" not in result:
            result["pyfunceble"] = {
                "config": {},
            }

        for sanitize_type, keys in infrastructure.ADMINISTRATION_INDEXES.items(
        ):
            if sanitize_type == "delete":
                continue

            for key in keys:
                if key not in result:
                    if sanitize_type == "bool":
                        local_result = bool(None)
                    elif sanitize_type == "int":
                        local_result = 0
                    elif sanitize_type == "dict":
                        local_result = dict()
                    elif sanitize_type == "datetime":
                        local_result = datetime.utcnow() - timedelta(
                            days=365.25)
                    elif sanitize_type == "epoch":
                        local_result = datetime.utcnow() - timedelta(
                            days=365.25)
                    else:
                        local_result = None

                    result[key] = local_result

        return result
Пример #14
0
    "infrastructure-monitoring",
    "dev-center",
]

if __name__ == "__main__":
    dir_helper = DirectoryHelper()
    file_helper = FileHelper()

    gh = Github(USER_TOKEN)

    if not dir_helper.set_path(CLONE_DIR).exists():
        dir_helper.create()

    dir_helper.set_path(DEPLOYMENT_DIR)

    CommandHelper(f"git config --global user.email {USER_GIT_EMAIL!r}").execute()
    CommandHelper(f"git config --global user.name {USER_GIT_NAME!r}").execute()
    CommandHelper("git config --global push.default simple").execute()
    CommandHelper("git config --global pull.rebase false").execute()

    for repo in gh.get_organization(ORG_NAME).get_repos():
        if repo.name in REPOS_TO_IGNORE:
            continue

        print("Starting to handle:", repo.name)

        repo_slug = f"{ORG_NAME}/{repo.name}"
        clone_url = CLONE_TEMPLATE.replace("%slug%", repo_slug)
        clone_destination = f"{CLONE_DIR}/{repo.name}"

        if not DirectoryHelper(clone_destination).exists():