summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2017-10-24 18:34:29 +0200
committerMichał Górny <mgorny@gentoo.org>2017-10-24 18:34:29 +0200
commit5f1b073c967928624cc8cfb417644df0db152f84 (patch)
treedc4d561c4e2747577c943ac85446752b056f8b5b
parentbad53c86d171ed892df985ecc73aa5fdb8710d50 (diff)
downloadgemato-5f1b073c967928624cc8cfb417644df0db152f84.tar.gz
Abstract out path prefix comparisons
-rw-r--r--gemato/manifest.py8
-rw-r--r--gemato/recursiveloader.py7
-rw-r--r--gemato/util.py21
-rw-r--r--tests/test_util.py46
4 files changed, 76 insertions, 6 deletions
diff --git a/gemato/manifest.py b/gemato/manifest.py
index 065b852..c1a605e 100644
--- a/gemato/manifest.py
+++ b/gemato/manifest.py
@@ -6,6 +6,8 @@
import datetime
import os.path
+import gemato.util
+
class ManifestSyntaxError(Exception):
def __init__(self, message):
@@ -257,7 +259,7 @@ class ManifestEntryAUX(ManifestFileEntry):
def to_list(self):
ret = super(ManifestEntryAUX, self).to_list(self.tag)
- assert ret[1].startswith('files/')
+ assert gemato.util.path_inside_dir(ret[1], 'files')
ret[1] = ret[1][6:]
return ret
@@ -335,7 +337,7 @@ class ManifestFile(object):
if isinstance(e, ManifestEntryIGNORE):
# ignore matches recursively, so we process it separately
# py<3.5 does not have os.path.commonpath()
- if (path + '/').startswith(e.path + '/'):
+ if gemato.util.path_starts_with(path, e.path):
return e
elif isinstance(e, ManifestEntryDIST):
# distfiles are not local files, so skip them
@@ -367,7 +369,7 @@ class ManifestFile(object):
for e in self.entries:
if isinstance(e, ManifestEntryMANIFEST):
mdir = os.path.dirname(e.path)
- if path.startswith(mdir + '/'):
+ if gemato.util.path_inside_dir(path, mdir):
yield e
diff --git a/gemato/recursiveloader.py b/gemato/recursiveloader.py
index 4c20d6d..8003245 100644
--- a/gemato/recursiveloader.py
+++ b/gemato/recursiveloader.py
@@ -8,6 +8,7 @@ import os.path
import weakref
import gemato.manifest
+import gemato.util
import gemato.verify
@@ -48,7 +49,7 @@ class ManifestRecursiveLoader(object):
"""
for k, v in self.loaded_manifests.items():
d = os.path.dirname(k)
- if not d or (path + '/').startswith(d + '/'):
+ if gemato.util.path_starts_with(path, d):
yield (d, v)
def load_manifests_for_path(self, path):
@@ -66,7 +67,7 @@ class ManifestRecursiveLoader(object):
if mpath in self.loaded_manifests:
continue
mdir = os.path.dirname(mpath)
- if not mdir or path.startswith(mdir + '/'):
+ if gemato.util.path_starts_with(path, mdir):
to_load.append((mpath, e))
if not to_load:
break
@@ -99,7 +100,7 @@ class ManifestRecursiveLoader(object):
# ignore matches recursively, so we process it separately
# py<3.5 does not have os.path.commonpath()
fullpath = os.path.join(relpath, e.path)
- if (path + '/').startswith(fullpath + '/'):
+ if gemato.util.path_starts_with(path, fullpath):
return e
elif isinstance(e, gemato.manifest.ManifestEntryDIST):
# distfiles are not local files, so skip them
diff --git a/gemato/util.py b/gemato/util.py
new file mode 100644
index 0000000..e136c5c
--- /dev/null
+++ b/gemato/util.py
@@ -0,0 +1,21 @@
+# gemato: Utility functions
+# vim:fileencoding=utf-8
+# (c) 2017 Michał Górny
+# Licensed under the terms of 2-clause BSD license
+
+
+def path_starts_with(path, prefix):
+ """
+ Returns True if the specified @path starts with the @prefix,
+ performing component-wide comparison. Otherwise returns False.
+ """
+ return prefix == "" or (path + "/").startswith(prefix.rstrip("/") + "/")
+
+
+def path_inside_dir(path, directory):
+ """
+ Returns True if the specified @path is inside @directory,
+ performing component-wide comparison. Otherwise returns False.
+ """
+ return ((directory == "" and path != "")
+ or path.rstrip("/").startswith(directory.rstrip("/") + "/"))
diff --git a/tests/test_util.py b/tests/test_util.py
new file mode 100644
index 0000000..41aa34c
--- /dev/null
+++ b/tests/test_util.py
@@ -0,0 +1,46 @@
+# gemato: Utility function tests
+# vim:fileencoding=utf-8
+# (c) 2017 Michał Górny
+# Licensed under the terms of 2-clause BSD license
+
+import unittest
+
+import gemato.util
+
+
+class UtilityTestCase(unittest.TestCase):
+ def test_path_starts_with(self):
+ self.assertTrue(gemato.util.path_starts_with("", ""))
+ self.assertTrue(gemato.util.path_starts_with("foo", ""))
+ self.assertTrue(gemato.util.path_starts_with("foo/", ""))
+ self.assertTrue(gemato.util.path_starts_with("foo/bar", ""))
+ self.assertTrue(gemato.util.path_starts_with("bar", ""))
+ self.assertTrue(gemato.util.path_starts_with("bar/", ""))
+ self.assertTrue(gemato.util.path_starts_with("bar/bar", ""))
+ self.assertTrue(gemato.util.path_starts_with("foo", "foo"))
+ self.assertTrue(gemato.util.path_starts_with("foo/", "foo"))
+ self.assertTrue(gemato.util.path_starts_with("foo/bar", "foo"))
+ self.assertFalse(gemato.util.path_starts_with("bar", "foo"))
+ self.assertFalse(gemato.util.path_starts_with("fooo", "foo"))
+ self.assertFalse(gemato.util.path_starts_with("foo.", "foo"))
+ self.assertTrue(gemato.util.path_starts_with("foo", "foo/"))
+ self.assertTrue(gemato.util.path_starts_with("foo/", "foo/"))
+ self.assertTrue(gemato.util.path_starts_with("foo/bar", "foo/bar/"))
+
+ def test_path_inside_dir(self):
+ self.assertFalse(gemato.util.path_inside_dir("", ""))
+ self.assertTrue(gemato.util.path_inside_dir("foo", ""))
+ self.assertTrue(gemato.util.path_inside_dir("foo/", ""))
+ self.assertTrue(gemato.util.path_inside_dir("foo/bar", ""))
+ self.assertTrue(gemato.util.path_inside_dir("bar", ""))
+ self.assertTrue(gemato.util.path_inside_dir("bar/", ""))
+ self.assertTrue(gemato.util.path_inside_dir("bar/bar", ""))
+ self.assertFalse(gemato.util.path_inside_dir("foo", "foo"))
+ self.assertFalse(gemato.util.path_inside_dir("foo/", "foo"))
+ self.assertTrue(gemato.util.path_inside_dir("foo/bar", "foo"))
+ self.assertFalse(gemato.util.path_inside_dir("bar", "foo"))
+ self.assertFalse(gemato.util.path_inside_dir("fooo", "foo"))
+ self.assertFalse(gemato.util.path_inside_dir("foo.", "foo"))
+ self.assertFalse(gemato.util.path_inside_dir("foo", "foo/"))
+ self.assertFalse(gemato.util.path_inside_dir("foo/", "foo/"))
+ self.assertFalse(gemato.util.path_inside_dir("foo/bar", "foo/bar/"))