55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import re
|
|
import datetime
|
|
|
|
class Snapshot(object):
|
|
def __init__(self, snapshot_string):
|
|
self.snapshot_string = snapshot_string
|
|
self.re_split_tabs = re.compile(r'\t+')
|
|
self.re_name = re.compile(r"[^/]+$")
|
|
self.re_zvol = re.compile(r"^.*[\\/]")
|
|
self.snapshot_tuple = self.parse_string_to_table()
|
|
self.disk_name = self.get_snapshot_name().split("@")[0]
|
|
|
|
def __repr__(self):
|
|
return self.get_snapshot_name()
|
|
|
|
def get_snapshot_string(self):
|
|
return self.snapshot_string
|
|
|
|
def parse_string_to_table(self):
|
|
array = re.split(self.re_split_tabs, self.snapshot_string)
|
|
return tuple(array)
|
|
|
|
def get_snapshot_name(self):
|
|
name_str = self.snapshot_tuple[0]
|
|
name = next(self.re_name.finditer(name_str))
|
|
return name.group()
|
|
|
|
def get_snapshot_zvol(self):
|
|
name_str = self.snapshot_tuple[0]
|
|
zvol = next(self.re_zvol.finditer(name_str))
|
|
return zvol.group()
|
|
|
|
def get_snapshot_creation_time(self):
|
|
time_epoch = int(self.snapshot_tuple[1])
|
|
return str(datetime.datetime.utcfromtimestamp(time_epoch))
|
|
|
|
def get_snapshot_used_size(self):
|
|
used_str = self.snapshot_tuple[2]
|
|
return self.sizeof_fmt(int(used_str))
|
|
|
|
def get_snapshot_referenced_size(self):
|
|
referenced_str = self.snapshot_tuple[3]
|
|
return self.sizeof_fmt(int(referenced_str))
|
|
|
|
def get_disk_name(self):
|
|
return self.disk_name
|
|
|
|
@staticmethod
|
|
def sizeof_fmt(num, suffix='B'):
|
|
## FIXME WOW taken from stack but it's too fucking slow. Probably because of division. Have to profile
|
|
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
|
|
if abs(num) < 1024.0:
|
|
return "%3.1f%s%s" % (num, unit, suffix)
|
|
num /= 1024.0
|
|
return "%.1f%s%s" % (num, 'Yi', suffix) |