From 8212379d03ed69326dc17d649f2f2ea0cd0ddf56 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 19 Feb 2022 18:29:57 +0100 Subject: zfssnap: ... --- modules/zfssnap/zfssnap.py | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'modules') diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index 4e7188f9..9a536258 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -46,10 +46,9 @@ def _get_items(): with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: text_stdout = io.TextIOWrapper(proc.stdout) reader = csv.reader(text_stdout, delimiter='\t', quoting=csv.QUOTE_NONE) - for row in reader: - name = row[0] - setting = bool(strtobool(row[1])) - items[name] = setting + Row = namedtuple('Row', ['name', 'setting']) + for row in map(Row._make, reader): + items[row.name] = bool(strtobool(row.setting)) return items @@ -62,16 +61,15 @@ def prune(config, dry_run, keep_newest): with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: text_stdout = io.TextIOWrapper(proc.stdout) reader = csv.reader(text_stdout, delimiter='\t', quoting=csv.QUOTE_NONE) - for row in reader: - name = row[0] - timestamp = int(row[1]) - creation = datetime.fromtimestamp(timestamp, timezone.utc) - base_name, _, _ = name.rpartition('@') + Row = namedtuple('Row', ['name', 'timestamp']) + for row in map(Row._make, reader): + creation = datetime.fromtimestamp(int(row.timestamp), timezone.utc) + base_name, _, _ = row.name.rpartition('@') expected_name = _snap_name(base_name, time=creation) - if expected_name != name: - # logger.debug(f'Skipping ‘{name}’ since it does not conform to naming scheme') + if expected_name != row.name: + # logger.debug(f'Skipping ‘{row.name}’ since it does not conform to naming scheme') continue - items[base_name].append(Snap(name=name, creation=creation)) + items[base_name].append(Snap(name=row.name, creation=creation)) keep = set() kept_count = defaultdict(lambda: defaultdict(lambda: 0)) @@ -162,29 +160,28 @@ def rename(snapshots, destroy=False): with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: text_stdout = io.TextIOWrapper(proc.stdout) reader = csv.reader(text_stdout, delimiter='\t', quoting=csv.QUOTE_NONE) - for row in reader: - name = row[0] - timestamp = int(row[1]) - creation = datetime.fromtimestamp(timestamp, timezone.utc) - base_name, _, _ = name.rpartition('@') + Row = namedtuple('Row', ['name', 'timestamp']) + for row in map(Row._make, reader): + creation = datetime.fromtimestamp(int(row.timestamp), timezone.utc) + base_name, _, _ = row.name.rpartition('@') new_name = _snap_name(base_name, time=creation) - if new_name == name: - logger.debug(f'Not renaming ‘{name}’ since name is already correct') + if new_name == row.name: + logger.debug(f'Not renaming ‘{row.name}’ since name is already correct') continue if new_name in renamed_to: if destroy: - logger.warning(f'Destroying ‘{name}’ since ‘{new_name}’ was already renamed to') - args = ['zfs', 'destroy', name] + logger.warning(f'Destroying ‘{row.name}’ since ‘{new_name}’ was already renamed to') + args = ['zfs', 'destroy', row.name] _log_cmd(*args) subprocess.run(args, check=True) else: - logger.info(f'Skipping ‘{name}’ since ‘{new_name}’ was already renamed to') + logger.info(f'Skipping ‘{row.name}’ since ‘{new_name}’ was already renamed to') continue - logger.info(f'Renaming ‘{name}’ to ‘{new_name}’') - args = ['zfs', 'rename', name, new_name] + logger.info(f'Renaming ‘{row.name}’ to ‘{new_name}’') + args = ['zfs', 'rename', row.name, new_name] _log_cmd(*args) subprocess.run(args, check=True) renamed_to.add(new_name) -- cgit v1.2.3