Exemplo n.º 1
0
    def createArchive(self, show_progress=False):
        def blankLine():
            # TODO: use term width
            print('\r' + 75 * ' ', end='')

        arc_name = self.name + '_' + self.date() + cfg.arc_ext
        arc_path = os.path.join(cfg.saves_path, arc_name)
        if not os.path.isfile(arc_path):
            if self._checked_files is False:
                self.files_dirs = self._getFilesDirs()

            # sort file list
            if cfg.sort_files == 'age':
                self.files_dirs = self._sortByAge(self.files_dirs)
            elif cfg.sort_files == 'smart':
                self.files_dirs = arcsort(self.files_dirs, id_unknown=True)

            try:
                with tarfile.open(arc_path, 'w:xz') as tar:

                    if cfg.rel_path is True:
                        for i in range(len(self.files_dirs)):
                            self.files_dirs[i] = (self.files_dirs[i], re.sub('^' + os.environ['HOME'], '', self.files_dirs[i]))

                    total = len(self.files_dirs)
                    count = 0
                    width = len(str(total))

                    if show_progress:
                        blankLine()

                    for fname in self.files_dirs:
                        count += 1

                        if show_progress:
                            print("\r{} {:.<{col2}} {}{:{w}}/{}".format(
                                self.name, '', cfg.yellow('files: '), cfg.yellow(count), cfg.yellow(total), col2=cfg.col1-len(self.name), w=width), end='')

                        if cfg.rel_path is True:
                            tar.add(fname[0], arcname=fname[1])
                        else:
                            tar.add(fname)

                    if show_progress:
                        blankLine()
                        print("\r{} {:.<{col2}} {}".format(self.name, '', cfg.cyan('created'), col2=cfg.col1-len(self.name)))
                self.saves.append(arc_name)
            except KeyboardInterrupt as e:
                os.remove(arc_path)
                print("\nKeyboardInterrupt caught")
                exit(1)
Exemplo n.º 2
0
def main():
    cfg.init()

    print("Config file:", cfg.ini_file)
    print("Saves path:", cfg.saves_path)

    for i in cfg.states:
        print("{} {:.<{col2}} ".format(i.name, '', col2=cfg.col1-len(i.name)), end='')
        if i.epoch() == 0:
            print(cfg.red("missing\n'{}'".format(i.path)))
        elif i.epoch() > i.save_epoch:
            print(cfg.cyan("creating new archive"), end='')
            i.createArchive(show_progress=True)
        else:
            print(cfg.green("current"))
        if len(i.saves) > cfg.arc_keep:
            i.deleteArchives()

    return 0