Add should_ignore_file and tests
authorDavid Stainton <dstainton415@gmail.com>
Wed, 9 Sep 2015 13:17:23 +0000 (15:17 +0200)
committerDaira Hopwood <daira@jacaranda.org>
Tue, 15 Sep 2015 17:43:00 +0000 (18:43 +0100)
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
src/allmydata/test/test_magicpath.py

index e99e3417bfe079e7cc4f2eecdc020d8b50fbbfb9..e2728421f6e7c8fbe437dbfa7b1d7840754498e4 100644 (file)
@@ -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
index 8ba5e40fd3cb065e148abc4bf3da6cf562cefe22..2362bc3f3abfac88ff3769898bc27e6ec425ec92 100644 (file)
@@ -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)