From: David Stainton Date: Wed, 9 Sep 2015 13:17:23 +0000 (+0200) Subject: Add should_ignore_file and tests X-Git-Url: https://git.rkrishnan.org/pf/content/quickstart.html?a=commitdiff_plain;h=ba9b26b1f74f5e4b8be24b2bc45eabaeaaec783d;p=tahoe-lafs%2Ftahoe-lafs.git Add should_ignore_file and tests This helper function will be used by magic-folder to determine when to drop a file from the upload queue... it ignores hidden files and certain suffixed files. --- diff --git a/src/allmydata/magicpath.py b/src/allmydata/magicpath.py index e99e3417..e2728421 100644 --- a/src/allmydata/magicpath.py +++ b/src/allmydata/magicpath.py @@ -1,9 +1,30 @@ import re - +import os.path def path2magic(path): return re.sub(ur'[/@]', lambda m: {u'/': u'@_', u'@': u'@@'}[m.group(0)], path) def magic2path(path): return re.sub(ur'@[_@]', lambda m: {u'@_': u'/', u'@@': u'@'}[m.group(0)], path) + + +IGNORE_SUFFIXES = ['.backup', '.tmp', '.conflicted'] +IGNORE_PREFIXES = ['.'] + +def should_ignore_file(path_u): + for suffix in IGNORE_SUFFIXES: + if path_u.endswith(suffix): + return True + while True: + head, tail = os.path.split(path_u) + if tail != "": + for prefix in IGNORE_PREFIXES: + if tail.startswith(prefix): + return True + else: + path_u = head + else: + if head == "": + return False + return False diff --git a/src/allmydata/test/test_magicpath.py b/src/allmydata/test/test_magicpath.py index 8ba5e40f..2362bc3f 100644 --- a/src/allmydata/test/test_magicpath.py +++ b/src/allmydata/test/test_magicpath.py @@ -18,3 +18,11 @@ class MagicPath(unittest.TestCase): def test_magic2path(self): for expected, test in self.tests.items(): self.failUnlessEqual(magicpath.magic2path(test), expected) + + def test_should_ignore(self): + self.failUnlessEqual(magicpath.should_ignore_file(".bashrc"), True) + self.failUnlessEqual(magicpath.should_ignore_file("bashrc."), False) + self.failUnlessEqual(magicpath.should_ignore_file("forest/tree/branch/.bashrc"), True) + self.failUnlessEqual(magicpath.should_ignore_file("forest/tree/.branch/bashrc"), True) + self.failUnlessEqual(magicpath.should_ignore_file("forest/.tree/branch/bashrc"), True) + self.failUnlessEqual(magicpath.should_ignore_file("forest/tree/branch/bashrc"), False)