/
cmd_list.py
executable file
·104 lines (81 loc) · 2.87 KB
/
cmd_list.py
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
#!/usr/bin/python
#
# Copyright (c) 2010 Liraz Siri <liraz@turnkeylinux.org>
#
# This file is part of TKLBAM (TurnKey Linux BAckup and Migration).
#
# TKLBAM 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.
#
"""
List backup records
By default uses a built-in format, unless a user-specified format is specified.
Format variables:
%backup_id Backup id
%label Descriptive label
%turnkey_version Appliance version code
%server_id Associated server id (- if empty)
%created Date the backup record was created
%updated Date the backup record was last updated
%size Aggregate size of backup, in MBs
%address Backup target address
%key Base64 encoded encrypted keypacket
Examples:
list
list "backup_id=%backup_id label=%label size=%{size}MB"
"""
import sys
import getopt
import string
import hub
from registry import registry
def usage(e=None):
if e:
print >> sys.stderr, "error: " + str(e)
print >> sys.stderr, "Syntax: %s [ <format> ]" % (sys.argv[0])
print >> sys.stderr, __doc__
sys.exit(1)
class Formatter:
def __init__(self, format):
tpl = format.replace('$', '$$')
tpl = tpl.replace('\\n', '\n')
tpl = tpl.replace('\\t', '\t')
tpl = tpl.replace('%%', '__PERCENT__')
tpl = tpl.replace('%', '$')
tpl = tpl.replace('__PERCENT__', '%')
self.tpl = string.Template(tpl)
def __call__(self, hbr):
return self.tpl.substitute(**hbr)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError, e:
usage(e)
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
if args:
if len(args) != 1:
usage("incorrect number of arguments")
format = args[0]
else:
format = None
hb = hub.Backups(registry.sub_apikey)
hbrs = hb.list_backups()
if format:
format = Formatter(format)
for hbr in hbrs:
print format(hbr)
elif hbrs:
print "# ID Created Updated Size (GB) Label"
for hbr in hbrs:
print "%4s %s %-10s %-8.2f %s" % (hbr.backup_id,
hbr.created.strftime("%Y-%m-%d"),
hbr.updated.strftime("%Y-%m-%d")
if hbr.updated else "-",
hbr.size / 1024.0,
hbr.label)
if __name__ == "__main__":
main()