Esempio n. 1
0
    def test_delete(self) -> None:
        """
        Tests the method which let us delete a file.
        """

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

        expected = False
        actual = file_helper.exists()

        self.assertEqual(expected, actual)

        with open(file_helper.path, "w", encoding="utf-8") as file_stream:
            file_stream.write("")

        expected = True
        actual = file_helper.exists()

        self.assertEqual(expected, actual)

        file_helper.delete()

        expected = False
        actual = file_helper.exists()

        self.assertEqual(expected, actual)
    def start(self) -> "OurInfrastructureUpdater":

        file_helper = FileHelper()
        dir_helper = DirectoryHelper()

        for file in self.FILES_TO_REMOVE:
            file_helper.set_path(
                os.path.join(self.info_manager.WORKSPACE_DIR, file))

            if file_helper.exists():
                logging.info("Starting deletion of %r", file_helper.path)

                file_helper.delete()

                logging.info("Finished deletion of %r", file_helper.path)

        for directory in self.DIRS_TO_REMOVE:
            dir_helper.set_path(
                os.path.join(self.info_manager.WORKSPACE_DIR, directory))

            if dir_helper.exists():
                logging.info("Starting deletion of %r", dir_helper.path)

                dir_helper.delete()

                logging.info("Finished deletion of %r", dir_helper.path)

        return self
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
0
    def migrate(self) -> "InactiveJSON2CSVMigrator":
        """
        Starts the migration.
        """

        file_helper = FileHelper(self.source_file)

        if file_helper.exists():
            self.dataset.set_authorized(True)
            dataset = {
                "idna_subject": None,
                "status": None,
                "status_source": None,
                "checker_type": "AVAILABILITY",
                "destination": None,
                "source": None,
                "tested_at": None,
                "session_id": None,
            }

            delete_file = True

            with file_helper.open("r", encoding="utf-8") as file_stream:
                for line in file_stream:
                    if (self.continuous_integration and
                            self.continuous_integration.is_time_exceeded()):
                        delete_file = False
                        break

                    line = (line.strip().replace('"',
                                                 "").replace(",", "").replace(
                                                     "{",
                                                     "",
                                                 ).replace("}", ""))

                    if ":" not in line:
                        continue

                    index, value = [x.strip() for x in line.rsplit(":", 1)]

                    if not value:
                        if index.isdigit():
                            dataset[
                                "tested_at"] = datetime.datetime.fromtimestamp(
                                    float(index)).isoformat()
                        else:
                            dataset["source"] = os.path.abspath(index)
                            dataset[
                                "destination"] = get_destination_from_origin(
                                    dataset["source"])

                        continue

                    dataset["idna_subject"] = domain2idna.domain2idna(index)
                    dataset["status"] = value

                    if not dataset["tested_at"]:
                        dataset["tested_at"] = datetime.datetime.utcnow(
                        ).isoformat()

                    PyFunceble.facility.Logger.debug("Decoded dataset:\n%r.",
                                                     dataset)

                    self.dataset.update(dataset)

                    if self.print_action_to_stdout:
                        print_single_line()

                    PyFunceble.facility.Logger.info("Added %r into %r",
                                                    dataset["idna_subject"],
                                                    self.dataset)

            if delete_file:
                file_helper.delete()
                self.done = True
        return self
Esempio n. 6
0
    def migrate(self) -> "WhoisJSON2CSVMigrator":
        """
        Provides the migration logic.
        """

        file_helper = FileHelper(self.source_file)

        if file_helper.exists():
            self.dataset.set_authorized(True)
            dataset = {
                "subject": None,
                "idna_subject": None,
                "expiration_date": None,
                "epoch": None,
            }

            delete_file = True

            with file_helper.open("r", encoding="utf-8") as file_stream:
                for line in file_stream:
                    if (self.continuous_integration and
                            self.continuous_integration.is_time_exceeded()):
                        delete_file = False
                        break

                    line = (line.strip().replace('"',
                                                 "").replace(",", "").replace(
                                                     "{",
                                                     "",
                                                 ).replace("}", ""))

                    if ":" not in line:
                        continue

                    index, value = [x.strip() for x in line.split(":")]

                    if not value:
                        dataset["subject"], dataset["idna_subject"] = (
                            index,
                            domain2idna.domain2idna(index),
                        )
                        continue

                    if index == "epoch":
                        dataset["epoch"] = float(value)
                    elif index == "expiration_date":
                        dataset["expiration_date"] = value
                    elif index == "state":
                        PyFunceble.facility.Logger.debug(
                            "Decoded dataset:\n%r.", dataset)

                        self.dataset.update(dataset)

                        if self.print_action_to_stdout:
                            print_single_line()

                        PyFunceble.facility.Logger.info(
                            "Added %r into %r", dataset["idna_subject"],
                            self.dataset)

            if delete_file:
                file_helper.delete()
                self.done = True
        return self