summaryrefslogtreecommitdiff
path: root/obnam/oper_show_generations.py
blob: e22d9510f7ad5127f3a80f78db8ffdcf5ad85cab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright (C) 2008  Lars Wirzenius <liw@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Operation to show contents of generations in a backup store."""


import logging
import sys
import time

import obnam


class ShowGenerations(obnam.Operation):

    """Show contents of generations specified by user."""
    
    name = "show-generations"
    
    def format_period(self, start, end):
        """Format time period in a format that is easy to read for humans"""
        start = time.localtime(start)
        end = time.localtime(end)
        if start[0:3] == end[0:3]:
            return "%s %s - %s" % \
                (time.strftime("%Y-%m-%d", start),
                 time.strftime("%H:%M", start),
                 time.strftime("%H:%M", end))
        else:
            return "%s %s - %s %s" % \
                (time.strftime("%Y-%m-%d", start),
                 time.strftime("%H:%M", start),
                 time.strftime("%Y-%m-%d", end),
                 time.strftime("%H:%M", end))

    def format_generation_period(self, gen):
        """Return human readable string to show the period of a generation"""
        start_time = gen.get_start_time()
        end_time = gen.get_end_time()
        return self.format_period(start_time, end_time)

    def show_filelist(self, fl):
        pretty = True
        list = []
        for c in fl.find_by_kind(obnam.cmp.FILE):
            filename = c.first_string_by_kind(obnam.cmp.FILENAME)
            if pretty:
                list.append((obnam.format.inode_fields(c), filename))
            else:
                print " ".join(obnam.format.inode_fields(c)), filename

        if pretty:
            widths = []
            for fields, _ in list:
                for i in range(len(fields)):
                    if i >= len(widths):
                        widths.append(0)
                    widths[i] = max(widths[i], len(fields[i]))
    
            for fields, filename in list:
                cols = []
                for i in range(len(widths)):
                    if i < len(fields):
                        x = fields[i]
                    else:
                        x = ""
                    cols.append("%*s" % (widths[i], x))
                print "  ", " ".join(cols), filename

    def show_dirs_and_filegroups(self, context, gen):
        listing = obnam.format.Listing(context, sys.stdout)
        listing.walk(listing.get_objects(gen.get_dirrefs()), 
                     listing.get_objects(gen.get_filegrouprefs()))
    
    def do_it(self, gen_ids):
        app = self.get_application()
        context = app.get_context()
        host = app.load_host()
        app.get_store().load_maps()
    
        for gen_id in gen_ids:
            gen = obnam.io.get_object(context, gen_id)
            if not gen:
                logging.warning("Can't find generation %s" % gen_id)
                continue
            print "Generation: %s %s" % (gen_id, 
                                         self.format_generation_period(gen))
    
            fl_id = gen.get_filelistref()
            if fl_id:
                fl = obnam.io.get_object(context, fl_id)
                if fl:
                    self.show_filelist(fl)
                else:
                    logging.warning("Can't find file list %s" % fl_id)
            else:
                self.show_dirs_and_filegroups(context, gen)