-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzfs-list-gtk.py
executable file
·272 lines (225 loc) · 8.78 KB
/
zfs-list-gtk.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
105
106
107
108
109
110
111
112
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3
import os
import sys
import argparse
import pickle
import subprocess
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
SIZE_PROPERTIES = ['used', 'usedbychildren', 'usedbydataset',
'usedbysnapshots', 'available', 'referenced']
homedir = os.getenv('HOME')
configdir = os.path.join(homedir, '.config')
if os.path.isdir(configdir):
optsfile = os.path.join(configdir, 'zfs-list-gtk.conf')
else:
optsfile = os.path.join(homedir, '.zfs-list-gtk.conf')
def human_readable(num, binary=False):
if binary:
if abs(num) < 1024:
return '{}B'.format(num)
# ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB']
for unit in ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 10.0:
return '{:.2f}{}'.format(num, unit)
if abs(num) < 1024.0:
return '{:.1f}{}'.format(num, unit)
num = num / 1024.0
return '{:.1f}{}'.format(num, 'YiB')
else:
if abs(num) < 1000:
return '{} B'.format(num)
for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']:
if abs(num) < 10.0:
return '{:.2f} {}'.format(num, unit)
if abs(num) < 1000.0:
return '{:.1f} {}'.format(num, unit)
num = num / 1000.0
return '{:.1f} {}'.format(num, 'YB')
def parse_zfs_list_output(lines, props):
filesystems = []
for line in lines:
if line == '': continue
current_fs_props = {}
line = line.split('\t')
for i, prop in enumerate(props):
current_fs_props[prop] = line[i]
filesystems.append(current_fs_props)
return filesystems
def build_treestore(filesystem, props):
command = ['zfs', 'list', '-Hrpt', 'all', '-o', ','.join(props + ['type'])]
if filesystem:
command.append(filesystem)
try:
output = subprocess.check_output(command)
except:
sys.exit('Error running zfs list')
output = output.decode().split('\n')
if output == ['']:
sys.exit('Error: no zfs filesystems')
filesystems = parse_zfs_list_output(output, props + ['type'])
# Initialize columns in the TreeStore
cols = []
for prop in props:
cols.append(str)
if prop in SIZE_PROPERTIES or prop == 'creation':
cols.append(float)
store = Gtk.TreeStore.new(cols)
for fs in filesystems:
# Add each value as a column
row = []
for prop in props:
if prop in SIZE_PROPERTIES:
if fs[prop] == '-':
row.append('-')
row.append(0)
else:
row.append(human_readable(int(fs[prop]), binary=True))
row.append(float(fs[prop]))
elif prop == 'creation':
row.append(time.strftime('%a %b %d %H:%M %Y',
time.localtime(int(fs[prop]))))
row.append(float(fs[prop]))
else:
row.append(fs[prop])
# Append this item to the store
if fs['type'] == 'filesystem':
li = store.append(None, row)
parent = li
elif fs['type'] == 'snapshot':
li = store.append(parent, row)
return store
class Gui:
def __init__(self, filesystem, props, gui_opts):
self.filesystem = filesystem
self.props = props
self.gui_opts = gui_opts
self.store = build_treestore(
self.filesystem, self.props)
self.tview = Gtk.TreeView(self.store)
self.selecteditem = None
# Create a TreeViewColumn for each property
_index = 0
for c in self.props:
if c in ['name', 'mountpoint', 'creation']:
# Left justify column and header
col = Gtk.TreeViewColumn(
title=c,
cell_renderer=Gtk.CellRendererText(),
text=_index)
else:
# Right justify column and header
col = Gtk.TreeViewColumn(
title=c,
cell_renderer=Gtk.CellRendererText(xalign=1),
text=_index)
col.set_alignment(1)
if c in SIZE_PROPERTIES or c == 'creation':
col.set_sort_column_id(_index + 1)
_index += 1
else:
col.set_sort_column_id(_index)
col.set_resizable(True)
col.set_reorderable(True)
col.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
colwidth = gui_opts.get('column_widths', {}).get(c)
if colwidth: col.set_fixed_width(colwidth)
self.tview.append_column(col)
_index += 1
# Connect signal to selecting row
self.tview.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
self.tview.get_selection().connect('changed', self.on_changed)
self.tview.connect('row-activated', self.on_row_activated)
# Prettify the TreeView
self.tview.set_headers_clickable(True)
self.tview.set_rules_hint(True)
self.tview.set_column_drag_function(None, None)
# Embed the TreeView in a scrolling window
self.scroll = Gtk.ScrolledWindow()
# Make scrollbars appear only when needed
self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC)
self.scroll.set_shadow_type(Gtk.ShadowType.IN)
self.scroll.add(self.tview)
# Put the scrolled window into a Box
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
box.add(self.scroll)
box.set_child_packing(self.scroll, True, True, 0, 0)
# Create box to hold buttons
hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
hbox.set_spacing(5)
hbox.set_layout(Gtk.ButtonBoxStyle.SPREAD)
box.add(hbox)
# Add buttons to the box
btn_refresh = Gtk.Button(label='Refresh')
btn_refresh.connect('clicked', self.on_btn_refresh_clicked)
hbox.add(btn_refresh)
# Make the window
self.window = Gtk.Window()
self.window.add(box)
# Necessary for window.get_size() to work
self.window.connect('delete-event', self.close)
# Make the window look pretty and size it
self.window.set_title(self.store[0][0])
self.window.set_border_width(5)
self.window.set_default_size(self.gui_opts.get('width', 800),
self.gui_opts.get('height', 600))
print('pid: {}'.format(os.getpid()))
self.window.show_all()
Gtk.main()
def refresh_tree(self):
# Remember the currently expanded rows
expanded_rows = []
for i, row in enumerate(self.store):
if self.tview.row_expanded(Gtk.TreePath(i)):
expanded_rows.append(row[0])
# Rebuild the treestore
treestore = build_treestore(
self.filesystem, self.props)
self.tview.set_model(treestore)
# Expand the previously expanded rows
for i, row in enumerate(self.store):
if row[0] in expanded_rows:
self.tview.expand_row(Gtk.TreePath(i), False)
def on_btn_refresh_clicked(self, button):
self.refresh_tree()
def on_changed(self, selection):
(model, paths) = selection.get_selected_rows()
self.selecteditems = []
for i in paths:
self.selecteditems.append(model[i][0])
def on_row_activated(self, a, b, c):
# Called when row is double-clicked
pass
def close(self, widget, data):
# Get the window size
self.gui_opts = {}
self.gui_opts['width'], self.gui_opts['height'] = self.window.get_size()
# Get column widths
self.gui_opts['column_widths'] = {}
cols = self.tview.get_columns()
for c in cols:
self.gui_opts['column_widths'][c.get_title()] = c.get_width()
pickle.dump(self.gui_opts, open(optsfile, 'wb'))
Gtk.main_quit()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-o', nargs='?',
default='name,used,avail,refer,mountpoint')
parser.add_argument('filesystem', nargs='?')
args = parser.parse_args()
try:
gui_opts = pickle.load(open(optsfile, 'rb'))
except:
print('Could not load options from {}'.format(optsfile))
gui_opts = {}
props = args.o.split(',')
if 'name' not in props:
props = ['name'] + props
props = ['available' if p == 'avail' else p for p in props]
props = ['referenced' if p == 'refer' else p for p in props]
props = ['compressratio' if p == 'ratio' else p for p in props]
Gui(args.filesystem, props, gui_opts)