PyFunceble/directory_structure.py
def restore(self):
        """
        Restore the 'output/' directory structure based on the `dir_structure.json` file.
        """

        # We get the structure we have to create/apply.
        structure = self._get_structure()

        # We get the list of key which is implicitly the list of directory to recreate.
        list_of_key = list(structure.keys())

        # We move to the content of the parent as we know that we are creating only one directory.
        # Note: if one day we will have to create multiple directory, we will have to change
        # the following.
        structure = structure[list_of_key[0]]

        # We also set the parent directory as we are going to construct its childen.
        parent_path = list_of_key[0]

        if not parent_path.endswith(PyFunceble.directory_separator):
            parent_path += PyFunceble.directory_separator

        # We get if we have to replace `.gitignore` to `.keep` and versa.
        replacement_status = self._restore_replace()

        for directory in structure:
            # We loop through the list of directory to create.

            # We construct the full path.
            base = self.base + parent_path + directory

            if not base.endswith(PyFunceble.directory_separator):
                base += PyFunceble.directory_separator

            # We create the constructed path if it does not exist.
            self._create_directory(base)

            for file in structure[directory]:
                # We loop through the list of files in the currently read directory.

                # We construct the full file path.s
                file_path = base + file

                # We get the file content.
                content_to_write = structure[directory][file]["content"]

                # And its sha512 checksum.
                online_sha = structure[directory][file]["sha512"]

                # We update the content to write by replacing our glue with `\n`.
                content_to_write = Regex(
                    content_to_write, "@@@", escape=True, replace_with="\\n"
                ).replace()

                # We get the file path as .keep.
                git_to_keep = file_path.replace("gitignore", "keep")

                # We get the file path as .gitignore.
                keep_to_git = file_path.replace("keep", "gitignore")

                if replacement_status:
                    # We have to replace every .gitignore to .keep.

                    if (
                        PyFunceble.path.isfile(file_path)
                        and Hash(file_path, "sha512", True).get() == online_sha
                    ):
                        # * The currently read file exist.
                        # and
                        # * Its sha512sum is equal to the one we have in our structure.

                        # We rename the file.
                        PyFunceble.rename(file_path, git_to_keep)

                        # And we disallow the file writing.
                        write = False
                    else:
                        # * The currently read file does not exist.
                        # or
                        # * Its sha512sum is not equal to the one we have in our structure.

                        # We delere the file if it does exist.
                        File(file_path).delete()

                        # We update the file path.
                        file_path = git_to_keep

                        # And we allow the file writing.
                        write = True
                else:
                    # We have to replace every .keep to .gitignore.
                    if (
                        PyFunceble.path.isfile(keep_to_git)
                        and Hash(file_path, "sha512", True).get() == online_sha
                    ):
                        # * The .keep file exist.
                        # and
                        # * Its sha512sum is equal to the one we have in our structure.

                        # We rename the file.
                        PyFunceble.rename(file_path, keep_to_git)

                        # And we disallow the file writing.
                        write = False
                    else:
                        # * The .keep file does not exist.
                        # or
                        # * Its sha512sum is not equal to the one we have in our structure.

                        # We delete the file if it exist.
                        File(keep_to_git).delete()

                        # We update the file path
                        file_path = keep_to_git

                        # And we allow the file writing.
                        write = True

                if write:
                    # The file writing is allowed.

                    # We write our file content into the file path.
                    File(file_path).write(content_to_write + "\n", True)
        self.delete_uneeded()
def restore(self):
        """
        Restore the 'output/' directory structure based on the `dir_structure.json` file.
        """

        # We get the structure we have to create/apply.
        structure = self._get_structure()

        # We get the list of key which is implicitly the list of directory to recreate.
        list_of_key = list(structure.keys())

        # We move to the content of the parent as we know that we are creating only one directory.
        # Note: if one day we will have to create multiple directory, we will have to change
        # the following.
        structure = structure[list_of_key[0]]

        # We also set the parent directory as we are going to construct its childen.
        parent_path = list_of_key[0]

        if not parent_path.endswith(PyFunceble.directory_separator):
            parent_path += PyFunceble.directory_separator

        # We get if we have to replace `.gitignore` to `.keep` and versa.
        replacement_status = self._restore_replace()

        for directory in structure:
            # We loop through the list of directory to create.

            # We construct the full path.
            base = self.base + parent_path + directory

            if not base.endswith(PyFunceble.directory_separator):
                base += PyFunceble.directory_separator

            # We create the constructed path if it does not exist.
            self._create_directory(base)

            for file in structure[directory]:
                # We loop through the list of files in the currently read directory.

                # We construct the full file path.s
                file_path = base + file

                # We get the file content.
                content_to_write = structure[directory][file]["content"]

                # And its sha512 checksum.
                online_sha = structure[directory][file]["sha512"]

                # We update the content to write by replacing our glue with `\n`.
                content_to_write = Regex(
                    content_to_write, "@@@", escape=True, replace_with="\\n"
                ).replace()

                # We get the file path as .keep.
                git_to_keep = file_path.replace("gitignore", "keep")

                # We get the file path as .gitignore.
                keep_to_git = file_path.replace("keep", "gitignore")

                if replacement_status:
                    # We have to replace every .gitignore to .keep.

                    if (
                        PyFunceble.path.isfile(file_path)
                        and Hash(file_path, "sha512", True).get() == online_sha
                    ):
                        # * The currently read file exist.
                        # and
                        # * Its sha512sum is equal to the one we have in our structure.

                        # We rename the file.
                        PyFunceble.rename(file_path, git_to_keep)

                        # And we disallow the file writing.
                        write = False
                    else:
                        # * The currently read file does not exist.
                        # or
                        # * Its sha512sum is not equal to the one we have in our structure.

                        # We delere the file if it does exist.
                        File(file_path).delete()

                        # We update the file path.
                        file_path = git_to_keep

                        # And we allow the file writing.
                        write = True
                else:
                    # We have to replace every .keep to .gitignore.
                    if (
                        PyFunceble.path.isfile(keep_to_git)
                        and Hash(file_path, "sha512", True).get() == online_sha
                    ):
                        # * The .keep file exist.
                        # and
                        # * Its sha512sum is equal to the one we have in our structure.

                        # We rename the file.
                        PyFunceble.rename(file_path, keep_to_git)

                        # And we disallow the file writing.
                        write = False
                    else:
                        # * The .keep file does not exist.
                        # or
                        # * Its sha512sum is not equal to the one we have in our structure.

                        # We delete the file if it exist.
                        File(keep_to_git).delete()

                        # We update the file path
                        file_path = keep_to_git

                        # And we allow the file writing.
                        write = True

                if write:
                    # The file writing is allowed.

                    # We write our file content into the file path.
                    File(file_path).write(content_to_write + "\n", True)
        self.delete_uneeded()
def restore ( self ) : # We get the structure we have to create/apply. structure = self . _get_structure ( ) # We get the list of key which is implicitly the list of directory to recreate. list_of_key = list ( structure . keys ( ) ) # We move to the content of the parent as we know that we are creating only one directory. # Note: if one day we will have to create multiple directory, we will have to change # the following. structure = structure [ list_of_key [ 0 ] ] # We also set the parent directory as we are going to construct its childen. parent_path = list_of_key [ 0 ] if not parent_path . endswith ( PyFunceble . directory_separator ) : parent_path += PyFunceble . directory_separator # We get if we have to replace `.gitignore` to `.keep` and versa. replacement_status = self . _restore_replace ( ) for directory in structure : # We loop through the list of directory to create. # We construct the full path. base = self . base + parent_path + directory if not base . endswith ( PyFunceble . directory_separator ) : base += PyFunceble . directory_separator # We create the constructed path if it does not exist. self . _create_directory ( base ) for file in structure [ directory ] : # We loop through the list of files in the currently read directory. # We construct the full file path.s file_path = base + file # We get the file content. content_to_write = structure [ directory ] [ file ] [ "content" ] # And its sha512 checksum. online_sha = structure [ directory ] [ file ] [ "sha512" ] # We update the content to write by replacing our glue with `\n`. content_to_write = Regex ( content_to_write , "@@@" , escape = True , replace_with = "\\n" ) . replace ( ) # We get the file path as .keep. git_to_keep = file_path . replace ( "gitignore" , "keep" ) # We get the file path as .gitignore. keep_to_git = file_path . replace ( "keep" , "gitignore" ) if replacement_status : # We have to replace every .gitignore to .keep. if ( PyFunceble . path . isfile ( file_path ) and Hash ( file_path , "sha512" , True ) . get ( ) == online_sha ) : # * The currently read file exist. # and # * Its sha512sum is equal to the one we have in our structure. # We rename the file. PyFunceble . rename ( file_path , git_to_keep ) # And we disallow the file writing. write = False else : # * The currently read file does not exist. # or # * Its sha512sum is not equal to the one we have in our structure. # We delere the file if it does exist. File ( file_path ) . delete ( ) # We update the file path. file_path = git_to_keep # And we allow the file writing. write = True else : # We have to replace every .keep to .gitignore. if ( PyFunceble . path . isfile ( keep_to_git ) and Hash ( file_path , "sha512" , True ) . get ( ) == online_sha ) : # * The .keep file exist. # and # * Its sha512sum is equal to the one we have in our structure. # We rename the file. PyFunceble . rename ( file_path , keep_to_git ) # And we disallow the file writing. write = False else : # * The .keep file does not exist. # or # * Its sha512sum is not equal to the one we have in our structure. # We delete the file if it exist. File ( keep_to_git ) . delete ( ) # We update the file path file_path = keep_to_git # And we allow the file writing. write = True if write : # The file writing is allowed. # We write our file content into the file path. File ( file_path ) . write ( content_to_write + "\n" , True ) self . delete_uneeded ( )
Restore the 'output/' directory structure based on the `dir_structure.json` file.
Restore the output / directory structure based on the dir_structure . json file .