示例#1
0
    def test_move(self) -> None:
        """
        Tests of the method which let us move a file to another location.
        """

        file_helper = FileHelper(tempfile.gettempdir())
        file_helper.set_path(file_helper.join_path(secrets.token_hex(8)))

        destination_file_helper = FileHelper(tempfile.gettempdir())
        destination_file_helper.set_path(
            destination_file_helper.join_path(secrets.token_hex(8)))

        expected = False
        actual = file_helper.exists()
        actual_destination = destination_file_helper.exists()

        self.assertEqual(expected, actual)
        self.assertEqual(expected, actual_destination)

        file_helper.write("Hello, World!")

        expected = True
        actual = file_helper.exists()

        self.assertEqual(expected, actual)

        expected = False
        actual_destination = destination_file_helper.exists()

        self.assertEqual(expected, actual_destination)

        file_helper.move(destination_file_helper.path)

        expected = True
        actual_destination = destination_file_helper.exists()

        self.assertEqual(expected, actual_destination)

        expected = "Hello, World!"
        actual = destination_file_helper.read()

        self.assertEqual(expected, actual)

        expected = False
        actual = file_helper.exists()

        self.assertEqual(expected, actual)

        expected = True
        actual_destination = destination_file_helper.exists()

        self.assertEqual(expected, actual_destination)
示例#2
0
    def start(self) -> "PyFuncebleConfigLocationUpdater":
        for file in self.FILES_TO_MOVE:
            source_file = FileHelper(
                os.path.join(self.info_manager.WORKSPACE_DIR, file))
            destination_file = FileHelper(
                os.path.join(self.info_manager.PYFUNCEBLE_CONFIG_DIR, file))

            if source_file.exists():
                logging.info(
                    "Starting to move %r into %r.",
                    source_file.path,
                    destination_file.path,
                )

                destination_file.delete()
                source_file.move(destination_file.path)

                logging.info(
                    "Finished to move %r into %r",
                    source_file.path,
                    destination_file.path,
                )
            else:
                logging.info(
                    "Did not moved move %r into %r: It does not exists.",
                    source_file.path,
                    destination_file.path,
                )

            if (all(
                    FileHelper(
                        os.path.join(self.info_manager.PYFUNCEBLE_CONFIG_DIR,
                                     x)).exists()
                    for x in self.INACTIVE_FILES_TO_DELETE) or FileHelper(
                        os.path.join(
                            self.info_manager.PYFUNCEBLE_CONFIG_DIR,
                            self.INACTIVE_FILES_TO_DELETE[0],
                        )).exists()):
                logging.info("Starting to delete inactive files.", )

                for inactive_file in self.INACTIVE_FILES_TO_DELETE:
                    FileHelper(
                        os.path.join(self.info_manager.PYFUNCEBLE_CONFIG_DIR,
                                     inactive_file)).delete()

                logging.info("Finished to delete inactive files.", )

        return self
示例#3
0
    def start(self) -> "InfrastructureCleaner":
        analytic_directory = DirectoryHelper(
            os.path.join(outputs.OUTPUT_ROOT_DIRECTORY, "json"))

        if analytic_directory.exists():
            for element in os.listdir(outputs.OUTPUT_ROOT_DIRECTORY):
                if any(x in element for x in self.STD_FILES_TO_IGNORE):
                    continue

                dir_helper = DirectoryHelper(
                    os.path.join(outputs.OUTPUT_ROOT_DIRECTORY, element))

                if dir_helper.exists():
                    dir_helper.delete()

                    logging.info("Deleted: %r", dir_helper.path)

        del analytic_directory

        for file in self.FILES_TO_REMOVE:
            if not isinstance(file, list):
                file_helper = FileHelper(
                    os.path.join(outputs.CURRENT_DIRECTORY, file))
            else:
                file_helper = FileHelper(
                    os.path.join(outputs.CURRENT_DIRECTORY, *file))

            if file_helper.exists():
                file_helper.delete()
                logging.info("Deleted: %r", file_helper.path)

        for file in self.FILES_TO_MOVE_TO_PYFUNCEBLE_CONFIG:
            file_helper = FileHelper(
                os.path.join(outputs.CURRENT_DIRECTORY, file))

            if file_helper.exists():
                file_helper.move(
                    os.path.join(outputs.PYFUNCEBLE_CONFIG_DIRECTORY, file))

                logging.info("Moved: %r", file_helper.path)

        return self