timeview.py 5.46 KB
Newer Older
1
# Copyright (C) 2015-2016 GRNET S.A.
2 3 4 5 6 7 8 9 10 11 12 13 14 15

# 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 3 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, see <http://www.gnu.org/licenses/>.

16 17
from os import listdir, makedirs
from os.path import exists, isdir
18
import os
19 20 21 22 23 24 25 26
from subprocess import Popen, PIPE
import json
import errno


path_types = {'reg', 'dir', 'sym', 'fifo', 'sock', 'chr', 'blk'}


Efthymia Bika's avatar
Efthymia Bika committed
27 28 29 30 31 32
def path_join(base, *args):
    slash = base[0:1]
    if slash != '/':
        slash = ''
    base = slash + base.strip('/') + '/'
    return base + '/'.join(str(x).strip('/') for x in args)
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


def timestamp_rfc822_to_iso8601(string):
    from email.utils import parsedate
    year, month, day, hour, minute, second, _, _, _ = parsedate(string)
    return "%04u-%02u-%02uT%02u:%02u:%02u" % (
        year, month, day, hour, minute, second)


def print_listing(openfile, root, path=()):
    openfile.write(root.get('timestamp', "- -")
                   + ' ' +  root.get('type', '-')
                   + ' ' + '/'.join(path) + '\0\n')
    for name, entry in root['entries'].iteritems():
        print_listing(openfile, entry, path=path + (name,))


def put_timepoint(config, timepoint, data):
    root = {'entries': {}}
    lines = data.split('\0')
    for i, line in enumerate(lines):
        if i & 1 == 0:
            continue
        line.strip('\n').strip('\0')
        if not line:
            continue

Efthymia Bika's avatar
Efthymia Bika committed
60
        parts = line.split(' ', 2)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        entry_type = parts[0]
        if entry_type not in path_types:
            continue

        timestamp = ' '.join(parts[1:-1])
        segments = parts[-1].split('/')
        node = root
        for segment in segments:
            entries = node['entries']
            if segment not in entries:
                entries[segment] = {'entries': {}}
            node = entries[segment]

        node['timestamp'] = timestamp
        node['type'] = entry_type

    datafile = path_join(config['datapath'], timepoint)
    with open(datafile, "w") as f:
        f.write(json.dumps(root, indent=2))


82
def fetch_timepoint(config, timepoint):
83 84 85
    curpath = os.path.dirname(os.path.realpath(__file__))
    duplicity = os.path.join(curpath, 'duplicity')
    args = [duplicity, 'list-current-files', '-t', timepoint,
86 87 88 89
            '--ssl-cacert-file', config['cacert_file'],
            '--archive-dir', config['archive_dir'],
            '--name', config['backup_name'],
            config['target_url']]
90

91 92 93 94 95 96 97 98 99
    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    procout, procerr = proc.communicate()
    proc.wait()
    if proc.returncode != 0:
        raise RuntimeError(procerr)

    put_timepoint(config, timepoint, procout)


100
def get_timepoint(config, timepoint, path):
101 102 103 104 105 106 107 108 109 110 111
    datafile = path_join(config['datapath'], timepoint)
    retries = 1
    while True:
        try:
            with open(datafile) as f:
                data = f.read()
            break
        except IOError as e:
            if e.errno != errno.ENOENT or retries == 0:
                raise

112
        fetch_timepoint(config, timepoint)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        if retries <= 0:
            raise e
        retries -= 1

    node = json.loads(data)
    path = path.strip('/').split('/')
    for i in xrange(len(path)):
        segment = path[i]
        if not segment:
            continue

        entries = node['entries']
        if segment not in entries:
            return []

        node = entries[segment]

    return sorted({'name': name,
                   'type': entry['type'],
                   'timestamp': entry['timestamp']}
                  for name, entry in node['entries'].iteritems())


def ensure_datapath(datapath):
    if not exists(datapath):
        makedirs(datapath)
    elif not isdir(datapath):
        m = "%r not a directory" % datapath
        raise IOError(m)


def get_config():
    with open("timeview.config") as f:
        data = f.read()

    config = literal_eval(data)
    datapath = config['datapath']
    ensure_datapath(datapath)
    return config


def main():
    from sys import argv, stdin, stdout
    def help():
157
        print "Usage: %s <datapath> <target_url> <cacert_file> <archive_dir> <backup_name> [get <absolute_timepoint> <path> | list]" % argv[0]
158 159
        raise SystemExit(1)

160
    if len(argv) < 7:
161 162 163 164 165 166
        help()

    datapath = argv[1]
    ensure_datapath(datapath)
    target_url = argv[2]

167
    cmd = argv[6]
168 169 170
    if cmd not in ['get', 'list']:
        help()

171
    cacert_file = argv[3]
172
    archive_dir = argv[4]
173
    backup_name = argv[5]
174
    #config = get_config()
175 176
    config = {'datapath': datapath,
            'target_url': target_url,
177 178 179
            'cacert_file': cacert_file,
            'archive_dir': archive_dir,
            'backup_name': backup_name}
180 181

    if cmd == 'get':
182
        if len(argv) < 9:
183 184
            help()

185 186 187
        timepoint = argv[7]
        path = unicode(argv[8], encoding='UTF-8')
        r = get_timepoint(config, timepoint, path)
188 189 190 191 192 193 194 195 196 197 198
        print json.dumps(r, indent=2)

    elif cmd == 'list':
        print json.dumps(sorted(listdir(config['datapath'])), indent=2)

    else:
        help()


if __name__ == "__main__":
    main()