directory tree for comparison
| 14 | |
| 15 | |
| 16 | class FTreeDir: |
| 17 | """ |
| 18 | directory tree for comparison |
| 19 | """ |
| 20 | |
| 21 | def __init__(self, path, ignores=None, debug=False): |
| 22 | self.path = path |
| 23 | self.ignores = ignores |
| 24 | self.debug = debug |
| 25 | self.entries = [] |
| 26 | self.log = Logger(debug=self.debug) |
| 27 | if os.path.exists(path) and os.path.isdir(path): |
| 28 | self._walk() |
| 29 | |
| 30 | def _walk(self): |
| 31 | """ |
| 32 | index directory |
| 33 | ignore empty directory |
| 34 | test for ignore pattern |
| 35 | """ |
| 36 | for root, dirs, files in os.walk(self.path, followlinks=True): |
| 37 | for file in files: |
| 38 | fpath = os.path.join(root, file) |
| 39 | if must_ignore([fpath], ignores=self.ignores, |
| 40 | debug=self.debug, strict=True): |
| 41 | self.log.dbg(f'ignoring file {fpath}') |
| 42 | continue |
| 43 | self.log.dbg(f'added file to list of {self.path}: {fpath}') |
| 44 | self.entries.append(fpath) |
| 45 | for dname in dirs: |
| 46 | dpath = os.path.join(root, dname) |
| 47 | if dir_empty(dpath): |
| 48 | # ignore empty directory |
| 49 | self.log.dbg(f'ignoring empty dir {dpath}') |
| 50 | continue |
| 51 | # appending "/" allows to ensure pattern |
| 52 | # like "*/dir/*" will match the content of the directory |
| 53 | # but also the directory itself |
| 54 | dpath += os.path.sep |
| 55 | if must_ignore([dpath], ignores=self.ignores, |
| 56 | debug=self.debug, strict=True): |
| 57 | self.log.dbg(f'ignoring dir {dpath}') |
| 58 | continue |
| 59 | self.log.dbg(f'added dir to list of {self.path}: {dpath}') |
| 60 | self.entries.append(dpath) |
| 61 | |
| 62 | def get_entries(self): |
| 63 | """return all entries""" |
| 64 | return self.entries |
| 65 | |
| 66 | def compare(self, other): |
| 67 | """ |
| 68 | compare two trees and returns |
| 69 | - left_only (only in self) |
| 70 | - right_only (only in other) |
| 71 | - in_both (in both) |
| 72 | the relative path are returned |
| 73 | """ |
no outgoing calls
no test coverage detected