Skip to content

Commit

Permalink
Parse Int Properties Where Possible
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaScriptDude authored Feb 14, 2023
1 parent 50bf74b commit 6ce7331
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "zfslib"
version = "0.10.0"
version = "0.11.0"
description = "ZFS Utilities For Python3"
license = "MIT"
authors = ["Timothy C. Quinn"]
Expand Down
24 changes: 14 additions & 10 deletions src/zfslib/zfslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
import fnmatch
import pathlib
import inspect
import sys
from collections import OrderedDict
from datetime import datetime, timedelta, date as dt_date

is_py2=(sys.version_info[0] == 2)

class __DEFAULT__(object):pass

class Connection:
Expand Down Expand Up @@ -108,7 +105,6 @@ def lookup(self, name):
# [_test_data_zfs] testing only
# [_test_data_zpool] testing only
def _load(self, get_mounts=True, zfs_props=None, zpool_props=None, _test_data_zfs=None, _test_data_zpool=None):
global is_py2

# setup zfs list properties (zfs list -o <props>)
_zfs_pdef=['name', 'creation']
Expand Down Expand Up @@ -146,11 +142,20 @@ def _load(self, get_mounts=True, zfs_props=None, zpool_props=None, _test_data_zf

def extract_properties(s, zpool:bool=False):
props = zpool_props if zpool else zfs_props
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
if isinstance(s, bytes): s = s.decode('utf-8')
items = s.strip().split( '\t' )
assert len( items ) == len( props ), (props, items)
propvalues = map( lambda x: None if x == '-' else x, items[1:] )
return [ items[ 0 ], zip( props[ 1: ], propvalues ) ]
for i in range(1,len(props)):
v = items[i]
if v == '-':
items[i] = None
elif props[i] in ZFS_INT_PROPS:
try:
items[i] = int(v)
except:
pass

return [ items[ 0 ], zip( props[ 1: ], items[ 1: ] ) ]

# Gather zfs list data
if _test_data_zfs is None:
Expand Down Expand Up @@ -575,7 +580,6 @@ def __init__(self, pool, name, parent=None):
# - M The path has been modified
# - R The path has been renamed
def get_diffs(self, snap_from, snap_to=None, include=None, exclude=None, file_type=None, chg_type=None):
global is_py2
self.assertHaveMounts()
assert self.mounted, "Cannot get diffs for Unmounted Dataset. Verify mounted flag on Dataset before calling"

Expand Down Expand Up @@ -620,7 +624,7 @@ def __tv(k, v):


def __row(s):
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
if isinstance(s, bytes): s = s.decode('utf-8')
return s.strip().split( '\t' )

rows = list(map(lambda s: __row(s), stdout.splitlines()))
Expand Down Expand Up @@ -893,7 +897,7 @@ def __str__(self):

''' END ZFS Entities '''


ZFS_INT_PROPS = set("allocated,available,capacity,checkpoint,createtxg,expandsize,filesystem_count,filesystem_limit,fragmentation,free,freeing,leaked,logicalreferenced,logicalused,objsetid,quota,referenced,refquota,refreservation,reservation,size,snapshot_count,snapshot_limit,used,usedbychildren,usedbydataset,usedbyrefreservation,usedbysnapshots,userrefs,volsize,written".split(','))

''' General Utilities '''

Expand Down
26 changes: 13 additions & 13 deletions tests/test_zfslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,23 @@ def test_zfsitem_get_child_n_properties(self):

pool = poolset.lookup('rpool')
self.assertIsInstance(pool, zfs.Pool)
self.assertEqual(pool.get_property('size'), '249108103168')
self.assertEqual(pool.get_property('allocated'), '156426862592')
self.assertEqual(pool.get_property('free'), '92681240576')
self.assertEqual(pool.get_property('size'), 249108103168)
self.assertEqual(pool.get_property('allocated'), 156426862592)
self.assertEqual(pool.get_property('free'), 92681240576)
self.assertEqual(pool.get_property('checkpoint'), None)
self.assertEqual(pool.get_property('fragmentation'), '50')
self.assertEqual(pool.get_property('capacity'), '62')
self.assertEqual(pool.get_property('fragmentation'), 50)
self.assertEqual(pool.get_property('capacity'), 62)
self.assertEqual(pool.get_property('health'), 'OFFLINE')
self.assertEqual(pool.get_property('readonly'), 'off')

ds = pool.lookup('ROOT')
self.assertIsInstance(ds, zfs.Dataset)
ds2=ds.get_child('ubuntu_n2qr5q')
self.assertIsInstance(ds2, zfs.Dataset)
self.assertEqual(ds2.get_property('used'), '12518498304')
self.assertEqual(ds2.get_property('used'), 12518498304)
ds3=ds2.get_child('srv')
self.assertIsInstance(ds3, zfs.Dataset)
self.assertEqual(ds3.get_property('used'), '327680')
self.assertEqual(ds3.get_property('used'), 327680)
snap = ds3.get_child('autozsys_j57yyo')
self.assertIsInstance(snap, zfs.Snapshot)

Expand All @@ -201,9 +201,9 @@ def test_zfsitem_get_child_n_properties(self):
s_ts='1608446351'
self.assertEqual(ds.get_property('creation'), s_ts)
self.assertEqual(ds.creation, dt_from_creation(s_ts))
self.assertEqual(ds.get_property('used'), '280644734976')
self.assertEqual(ds.get_property('available'), '3564051083264')
self.assertEqual(ds.get_property('referenced'), '266918285312')
self.assertEqual(ds.get_property('used'), 280644734976)
self.assertEqual(ds.get_property('available'), 3564051083264)
self.assertEqual(ds.get_property('referenced'), 266918285312)
self.assertEqual(ds.mountpoint, '/dpool/other')
self.assertEqual(ds.mounted, True)
self.assertEqual(ds.has_mount, True)
Expand All @@ -212,9 +212,9 @@ def test_zfsitem_get_child_n_properties(self):
ds = poolset.lookup('bpool/BOOT')
self.assertIsInstance(ds, zfs.Dataset)
self.assertEqual(ds.get_property('creation'), '1608154061')
self.assertEqual(ds.get_property('used'), '190410752')
self.assertEqual(ds.get_property('available'), '1686265856')
self.assertEqual(ds.get_property('referenced'), '98304')
self.assertEqual(ds.get_property('used'), 190410752)
self.assertEqual(ds.get_property('available'), 1686265856)
self.assertEqual(ds.get_property('referenced'), 98304)
self.assertEqual(ds.mountpoint, 'none')
self.assertEqual(ds.has_mount, False)
self.assertEqual(ds.mounted, False)
Expand Down

0 comments on commit 6ce7331

Please sign in to comment.