From f54788b0e78613379af65acab69dd13f96d79b99 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 26 Jan 2024 23:02:08 +0100 Subject: [PATCH] flat tree comparison --- dotdrop/comparator.py | 13 +++++++++++++ dotdrop/ftree.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 dotdrop/ftree.py diff --git a/dotdrop/comparator.py b/dotdrop/comparator.py index b2462f1..008e1b9 100644 --- a/dotdrop/comparator.py +++ b/dotdrop/comparator.py @@ -10,6 +10,7 @@ import filecmp # local imports from dotdrop.logger import Logger +from dotdrop.ftree import FTreeDir from dotdrop.utils import must_ignore, uniq_list, diff, \ get_file_perm @@ -22,12 +23,24 @@ class Comparator: """constructor @diff_cmd: diff command to use @debug: enable debug + @ignore_missing_in_dotdrop: ignore missing files in dotdrop """ self.diff_cmd = diff_cmd self.debug = debug self.log = Logger(debug=self.debug) self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop + def compare2(self, local_path, deployed_path, ignore=None, mode=None): + """ + diff local_path (dotdrop dotfile) and + deployed_path (destination file) + If mode is None, rights will be read from local_path + """ + local_tree = FTreeDir(local_path, ignores=ignore, debug=self.debug) + deploy_tree = FTreeDir(deployed_path, ignores=ignore, debug=self.debug) + lonly, ronly, common = local_tree.compare(deploy_tree) + # TODO + def compare(self, local_path, deployed_path, ignore=None, mode=None): """ diff local_path (dotdrop dotfile) and diff --git a/dotdrop/ftree.py b/dotdrop/ftree.py new file mode 100644 index 0000000..aa9f68f --- /dev/null +++ b/dotdrop/ftree.py @@ -0,0 +1,45 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2024, deadc0de6 + +filesystem tree for directories +""" + + +import os + +# local imports +from dotdrop.utils import must_ignore + + +class FTreeDir: + + def __init__(self, path, ignores=None, debug=False): + self.path = path + self.ignores + self.debug = debug + self.entries = [] + if os.path.exists(path) and os.path.isdir(path): + self._walk() + + def _walk(self): + for root, _, files in os.walk(self.path): + for f in files: + fpath = os.path.join(root, f) + if must_ignore([fpath], ignores=self.ignores, + debug=self.debug): + continue + self.entries.append(fpath) + self.entries.sort() + + def compare(self, other): + """ + compare two trees and returns + - left_only (only in self) + - right_only (only in other) + - in_both (in both) + """ + left_only = set(self.entries) - set(other.entries) + right_only = set(other.entries) - set(self.entries) + in_both = set(self.entries) & set(other.entries) + return left_only, right_only, in_both