summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2017-10-24 18:57:00 +0200
committerMichał Górny <mgorny@gentoo.org>2017-10-24 18:57:00 +0200
commit0308810307417309e405b02909cece7d1aa08800 (patch)
tree48da1cd1e760e51a4ca3a21f50bbffcbcaf9e380
parent5f1b073c967928624cc8cfb417644df0db152f84 (diff)
downloadgemato-0308810307417309e405b02909cece7d1aa08800.tar.gz
recursiveloader: Support loading sub-Manifests recursively
-rw-r--r--gemato/recursiveloader.py17
-rw-r--r--tests/test_recursiveloader.py36
2 files changed, 47 insertions, 6 deletions
diff --git a/gemato/recursiveloader.py b/gemato/recursiveloader.py
index 8003245..c996944 100644
--- a/gemato/recursiveloader.py
+++ b/gemato/recursiveloader.py
@@ -42,24 +42,29 @@ class ManifestRecursiveLoader(object):
m.load(f)
self.loaded_manifests[relpath] = m
- def _iter_manifests_for_path(self, path):
+ def _iter_manifests_for_path(self, path, recursive=False):
"""
Iterate over loaded Manifests that can apply to path.
- Yields a tuple of (relative_path, manifest).
+ If @recursive is True, returns also Manifests for subdirectories
+ of @path. Yields a tuple of (relative_path, manifest).
"""
for k, v in self.loaded_manifests.items():
d = os.path.dirname(k)
if gemato.util.path_starts_with(path, d):
yield (d, v)
+ elif recursive and gemato.util.path_starts_with(d, path):
+ yield (d, v)
- def load_manifests_for_path(self, path):
+ def load_manifests_for_path(self, path, recursive=False):
"""
Load all Manifests that may apply to the specified path,
- recursively.
+ recursively. If @recursive is True, also loads Manifests
+ for all subdirectories of @path.
"""
+ # TODO: figure out how to avoid confusing uses of 'recursive'
while True:
to_load = []
- for relpath, m in self._iter_manifests_for_path(path):
+ for relpath, m in self._iter_manifests_for_path(path, recursive):
for e in m.entries:
if not isinstance(e, gemato.manifest.ManifestEntryMANIFEST):
continue
@@ -69,6 +74,8 @@ class ManifestRecursiveLoader(object):
mdir = os.path.dirname(mpath)
if gemato.util.path_starts_with(path, mdir):
to_load.append((mpath, e))
+ elif recursive and gemato.util.path_starts_with(mdir, path):
+ to_load.append((mpath, e))
if not to_load:
break
for mpath, e in to_load:
diff --git a/tests/test_recursiveloader.py b/tests/test_recursiveloader.py
index f495c68..d9dfa83 100644
--- a/tests/test_recursiveloader.py
+++ b/tests/test_recursiveloader.py
@@ -13,11 +13,12 @@ import gemato.recursiveloader
class BasicNestingTest(unittest.TestCase):
- DIRS = ['sub', 'sub/deeper']
+ DIRS = ['sub', 'sub/deeper', 'other']
FILES = {
'Manifest': u'''
TIMESTAMP 2017-01-01T01:01:01Z
MANIFEST sub/Manifest 146 MD5 81180715a77069664b4b695e53bb856d
+MANIFEST other/Manifest 0 MD5 d41d8cd98f00b204e9800998ecf8427e
DIST topdistfile-1.txt 0 MD5 d41d8cd98f00b204e9800998ecf8427e
''',
'sub/Manifest': u'''
@@ -30,6 +31,7 @@ DIST subdistfile-1.txt 0 MD5 d41d8cd98f00b204e9800998ecf8427e
DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e
''',
'sub/deeper/test': u'',
+ 'other/Manifest': u'',
}
def setUp(self):
@@ -61,6 +63,7 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e
self.assertNotIn('sub/deeper/Manifest', m.loaded_manifests)
m.load_manifests_for_path('sub/deeper/test')
self.assertIn('sub/deeper/Manifest', m.loaded_manifests)
+ self.assertNotIn('other/Manifest', m.loaded_manifests)
def test_recursive_load_manifest(self):
m = gemato.recursiveloader.ManifestRecursiveLoader(
@@ -70,6 +73,28 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e
m.load_manifests_for_path('sub/deeper/test')
self.assertIn('sub/Manifest', m.loaded_manifests)
self.assertIn('sub/deeper/Manifest', m.loaded_manifests)
+ self.assertNotIn('other/Manifest', m.loaded_manifests)
+
+ def test_load_manifests_recursively(self):
+ m = gemato.recursiveloader.ManifestRecursiveLoader(
+ os.path.join(self.dir, 'Manifest'))
+ self.assertNotIn('sub/Manifest', m.loaded_manifests)
+ self.assertNotIn('sub/deeper/Manifest', m.loaded_manifests)
+ self.assertNotIn('other/Manifest', m.loaded_manifests)
+ m.load_manifests_for_path('', recursive=True)
+ self.assertIn('sub/Manifest', m.loaded_manifests)
+ self.assertIn('sub/deeper/Manifest', m.loaded_manifests)
+ self.assertIn('other/Manifest', m.loaded_manifests)
+
+ def test_load_sub_manifest_recursively(self):
+ m = gemato.recursiveloader.ManifestRecursiveLoader(
+ os.path.join(self.dir, 'Manifest'))
+ self.assertNotIn('sub/Manifest', m.loaded_manifests)
+ self.assertNotIn('sub/deeper/Manifest', m.loaded_manifests)
+ m.load_manifests_for_path('sub', recursive=True)
+ self.assertIn('sub/Manifest', m.loaded_manifests)
+ self.assertIn('sub/deeper/Manifest', m.loaded_manifests)
+ self.assertNotIn('other/Manifest', m.loaded_manifests)
def test_find_timestamp(self):
m = gemato.recursiveloader.ManifestRecursiveLoader(
@@ -188,6 +213,15 @@ TIMESTAMP 2017-01-01T01:01:01Z
self.assertIn('sub/Manifest.a', m.loaded_manifests)
self.assertIn('sub/Manifest.b', m.loaded_manifests)
+ def test_load_manifests_recursively(self):
+ m = gemato.recursiveloader.ManifestRecursiveLoader(
+ os.path.join(self.dir, 'Manifest'))
+ self.assertNotIn('sub/Manifest.a', m.loaded_manifests)
+ self.assertNotIn('sub/Manifest.b', m.loaded_manifests)
+ m.load_manifests_for_path('', recursive=True)
+ self.assertIn('sub/Manifest.a', m.loaded_manifests)
+ self.assertIn('sub/Manifest.b', m.loaded_manifests)
+
def test_find_timestamp(self):
m = gemato.recursiveloader.ManifestRecursiveLoader(
os.path.join(self.dir, 'Manifest'))