Example #1
0
    def restore(self):
        """
        Restore the 'output/' directory structure based on the `dir_structure.json` file.
        """

        if not Settings.quiet:
            print('Creation of non existant files and directories', end=" ")

        structure = Helpers.Dict().from_json(
            Helpers.File(self.structure).read())

        structure = structure['output']
        replace = self.restore_replace()

        for directory in structure:
            if not path.isdir(self.base + self.path + directory):
                self.travis_permissions()
                mkdir(self.base + self.path + directory)
                self.travis_permissions()

            for file in structure[directory]:
                file_path = self.path + directory + directory_separator + file

                content_to_write = structure[directory][file]['content']
                online_sha = structure[directory][file]['sha512']
                content_to_write = Helpers.Regex(content_to_write,
                                                 '@@@',
                                                 escape=True,
                                                 replace_with='\\n').replace()

                git_to_keep = file_path.replace('gitignore', 'keep')
                keep_to_git = file_path.replace('keep', 'gitignore')

                if replace:
                    if path.isfile(file_path) and Hash(
                            file_path, 'sha512', True).get() == online_sha:
                        rename(file_path, git_to_keep)
                        write = False
                    else:
                        Helpers.File(file_path).delete()
                        file_path = git_to_keep
                        write = True
                else:
                    if path.isfile(keep_to_git) and Hash(
                            file_path, 'sha512', True).get() == online_sha:
                        rename(file_path, keep_to_git)
                        write = False
                    else:
                        Helpers.File(keep_to_git).delete()
                        file_path = keep_to_git
                        write = True

                if write:
                    Helpers.File(file_path).write(content_to_write + '\n',
                                                  True)

        if not Settings.quiet:
            print(Settings.done)
    def update(self):
        """
        Update the content of the `iana-domains-db` file.
        """

        if self.download():
            Helpers.Dict(self.get_valid_extensions()).to_json(self.destination)
            # for extension in extensions:
            #     Helpers.File(self.destination).write(extension + '\n')
            if not Settings.quiet:
                print(Settings.done)
        else:
            if not Settings.quiet:
                print(Settings.error)
            exit(1)
    def backup(self):
        """
        Backup the developer state of `output/` in order to make it restorable
            and portable for user.
        """

        result = {'output': {}}

        if not Settings.quiet:
            print('Generation of dir-structure.json', end=" ")

        for root, _, files in walk(self.path):
            directories = root.split(self.path)[1]

            local_result = result['output']

            for file in files:
                file_path = root + directory_separator + file
                file_hash = Hash(file_path, 'sha512', True).get()

                lines_in_list = [line.rstrip('\n') for line in open(file_path)]
                formated_content = ''

                for line in lines_in_list:
                    if line != lines_in_list[-1]:
                        formated_content += line + '@@@'
                    else:
                        formated_content += line
                local_result = local_result.setdefault(
                    directories,
                    {file: {
                        'sha512': file_hash,
                        'content': formated_content
                    }})

            Helpers.Dict(result).to_json(self.structure)

        if not Settings.quiet:
            print(Settings.done)