From ace87a2035f378ba4365209bdd3f3a276503e68d Mon Sep 17 00:00:00 2001 From: David Stainton Date: Wed, 9 Sep 2015 15:17:23 +0200 Subject: [PATCH] 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. --- src/allmydata/magicpath.py | 23 ++++++++++++++++++++++- src/allmydata/test/test_magicpath.py | 8 ++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) 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) -- 2.45.2