diff options
-rw-r--r-- | gemato/recursiveloader.py | 38 | ||||
-rw-r--r-- | tests/test_recursiveloader.py | 102 |
2 files changed, 75 insertions, 65 deletions
diff --git a/gemato/recursiveloader.py b/gemato/recursiveloader.py index 81ad5bf..29cb8e5 100644 --- a/gemato/recursiveloader.py +++ b/gemato/recursiveloader.py @@ -426,8 +426,9 @@ class ManifestRecursiveLoader(object): verify_manifests=True): """ Find all file entries that apply to paths starting with @path. - Return a dictionary mapping relative paths to entries. Raises - an exception if multiple entries for file collide. + Returns a nested dictionary that maps directories -> filenames + inside the directory -> entries. Raises an exception if multiple + entries for file collide. If @only_types are specified as a list, only files of specified types will be collected. If it is not specified, then all types @@ -457,12 +458,16 @@ class ManifestRecursiveLoader(object): fullpath = os.path.join(relpath, e.path) if gemato.util.path_starts_with(fullpath, path): - if fullpath in out: + dirpath = os.path.dirname(fullpath) + filename = os.path.basename(e.path) + dirout = out.setdefault(dirpath, {}) + if filename in dirout: # compare the two entries ret, diff = gemato.verify.verify_entry_compatibility( - out[fullpath], e) + dirout[filename], e) if not ret: - raise gemato.exceptions.ManifestIncompatibleEntry(out[fullpath], e, diff) + raise gemato.exceptions.ManifestIncompatibleEntry( + dirout[filename], e, diff) # we need to construct a single entry with both checksums if diff: new_checksums = dict(e.checksums) @@ -470,7 +475,7 @@ class ManifestRecursiveLoader(object): if d2 is None: new_checksums[k] = d1 e = type(e)(e.path, e.size, new_checksums) - out[fullpath] = e + dirout[filename] = e return out def _verify_one_file(self, path, relpath, e, fail_handler, @@ -524,6 +529,7 @@ class ManifestRecursiveLoader(object): # strip dot to avoid matching problems if relpath == '.': relpath = '' + dirdict = entry_dict.get(relpath, {}) skip_dirs = [] for d in dirnames: @@ -532,8 +538,7 @@ class ManifestRecursiveLoader(object): skip_dirs.append(d) continue - dpath = os.path.join(relpath, d) - de = entry_dict.pop(dpath, None) + de = dirdict.pop(d, None) if de is None: syspath = os.path.join(dirpath, d) st = os.stat(syspath) @@ -544,6 +549,7 @@ class ManifestRecursiveLoader(object): if de.tag == 'IGNORE': skip_dirs.append(d) else: + dpath = os.path.join(relpath, d) ret &= self._verify_one_file(os.path.join(dirpath, d), dpath, de, fail_handler, last_mtime) @@ -561,15 +567,17 @@ class ManifestRecursiveLoader(object): # an entry for it if fpath == self.top_level_manifest_filename: continue - fe = entry_dict.pop(fpath, None) + fe = dirdict.pop(f, None) ret &= self._verify_one_file(os.path.join(dirpath, f), fpath, fe, fail_handler, last_mtime) # check for missing files - for relpath, e in entry_dict.items(): - syspath = os.path.join(self.root_directory, relpath) - ret &= self._verify_one_file(syspath, relpath, e, - fail_handler, last_mtime) + for relpath, dirdict in entry_dict.items(): + for f, e in dirdict.items(): + fpath = os.path.join(relpath, f) + syspath = os.path.join(self.root_directory, fpath) + ret &= self._verify_one_file(syspath, fpath, e, + fail_handler, last_mtime) return ret @@ -883,6 +891,7 @@ class ManifestRecursiveLoader(object): # strip dot to avoid matching problems if relpath == '.': relpath = '' + dirdict = entry_dict.get(relpath, {}) skip_dirs = [] for d in dirnames: @@ -891,8 +900,7 @@ class ManifestRecursiveLoader(object): skip_dirs.append(d) continue - dpath = os.path.join(relpath, d) - de = entry_dict.get(dpath, None) + de = dirdict.get(d, None) if de is None: syspath = os.path.join(dirpath, d) st = os.stat(syspath) diff --git a/tests/test_recursiveloader.py b/tests/test_recursiveloader.py index eb7eb48..cf56ee0 100644 --- a/tests/test_recursiveloader.py +++ b/tests/test_recursiveloader.py @@ -189,43 +189,41 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), - frozenset(( - 'other/Manifest', - 'sub/Manifest', - 'sub/deeper/Manifest', - 'sub/deeper/test', - ))) - self.assertEqual(entries['other/Manifest'].path, 'other/Manifest') - self.assertEqual(entries['sub/Manifest'].path, 'sub/Manifest') - self.assertEqual(entries['sub/deeper/Manifest'].path, 'deeper/Manifest') - self.assertEqual(entries['sub/deeper/test'].path, 'test') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + { + 'other': ['Manifest'], + 'sub': ['Manifest'], + 'sub/deeper': ['Manifest', 'test'], + }) + self.assertEqual(entries['other']['Manifest'].path, 'other/Manifest') + self.assertEqual(entries['sub']['Manifest'].path, 'sub/Manifest') + self.assertEqual(entries['sub/deeper']['Manifest'].path, 'deeper/Manifest') + self.assertEqual(entries['sub/deeper']['test'].path, 'test') def test_get_file_entry_dict_only_types(self): m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('', only_types=['MANIFEST']) - self.assertSetEqual(frozenset(entries), - frozenset(( - 'other/Manifest', - 'sub/Manifest', - 'sub/deeper/Manifest', - ))) - self.assertEqual(entries['other/Manifest'].path, 'other/Manifest') - self.assertEqual(entries['sub/Manifest'].path, 'sub/Manifest') - self.assertEqual(entries['sub/deeper/Manifest'].path, 'deeper/Manifest') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + { + 'other': ['Manifest'], + 'sub': ['Manifest'], + 'sub/deeper': ['Manifest'], + }) + self.assertEqual(entries['other']['Manifest'].path, 'other/Manifest') + self.assertEqual(entries['sub']['Manifest'].path, 'sub/Manifest') + self.assertEqual(entries['sub/deeper']['Manifest'].path, 'deeper/Manifest') def test_get_file_entry_dict_only_types_DIST(self): m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('', only_types=['DIST']) - self.assertSetEqual(frozenset(entries), - frozenset(( - 'subdistfile-1.txt', - 'topdistfile-1.txt', - ))) - self.assertEqual(entries['subdistfile-1.txt'].path, 'subdistfile-1.txt') - self.assertEqual(entries['topdistfile-1.txt'].path, 'topdistfile-1.txt') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + { + '': ['subdistfile-1.txt', 'topdistfile-1.txt'], + }) + self.assertEqual(entries['']['subdistfile-1.txt'].path, 'subdistfile-1.txt') + self.assertEqual(entries['']['topdistfile-1.txt'].path, 'topdistfile-1.txt') def test_get_deduplicated_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -252,15 +250,14 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('sub') - self.assertSetEqual(frozenset(entries), - frozenset(( - 'sub/Manifest', - 'sub/deeper/Manifest', - 'sub/deeper/test', - ))) - self.assertEqual(entries['sub/Manifest'].path, 'sub/Manifest') - self.assertEqual(entries['sub/deeper/Manifest'].path, 'deeper/Manifest') - self.assertEqual(entries['sub/deeper/test'].path, 'test') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + { + 'sub': ['Manifest'], + 'sub/deeper': ['Manifest', 'test'], + }) + self.assertEqual(entries['sub']['Manifest'].path, 'sub/Manifest') + self.assertEqual(entries['sub/deeper']['Manifest'].path, 'deeper/Manifest') + self.assertEqual(entries['sub/deeper']['test'].path, 'test') def test_get_deduplicated_file_entry_dict_for_update_for_sub(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -912,8 +909,9 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), frozenset(('test',))) - self.assertEqual(entries['test'].path, 'test') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'': ['test']}) + self.assertEqual(entries['']['test'].path, 'test') def test_get_deduplicated_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -1029,9 +1027,9 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), - frozenset(('sub/test', 'sub/Manifest'))) - self.assertEqual(entries['sub/test'].size, 0) + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'sub': ['Manifest', 'test']}) + self.assertEqual(entries['sub']['test'].size, 0) def test_get_deduplicated_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -1075,8 +1073,9 @@ EBUILD test.ebuild 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), frozenset(('test.ebuild',))) - self.assertEqual(entries['test.ebuild'].path, 'test.ebuild') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'': ['test.ebuild']}) + self.assertEqual(entries['']['test.ebuild'].path, 'test.ebuild') def test_get_deduplicated_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -1123,8 +1122,9 @@ AUX test.patch 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), frozenset(('files/test.patch',))) - self.assertEqual(entries['files/test.patch'].path, 'files/test.patch') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'files': ['test.patch']}) + self.assertEqual(entries['files']['test.patch'].path, 'files/test.patch') def test_get_deduplicated_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( @@ -1236,9 +1236,10 @@ DATA test 0 SHA1 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('') - self.assertSetEqual(frozenset(entries), frozenset(('test',))) - self.assertEqual(entries['test'].path, 'test') - self.assertSetEqual(frozenset(entries['test'].checksums), + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'': ['test']}) + self.assertEqual(entries['']['test'].path, 'test') + self.assertSetEqual(frozenset(entries['']['test'].checksums), frozenset(('MD5', 'SHA1'))) def test_get_deduplicated_file_entry_dict_for_update(self): @@ -1333,8 +1334,9 @@ MISC test.ebuild 0 MD5 d41d8cd98f00b204e9800998ecf8427e m = gemato.recursiveloader.ManifestRecursiveLoader( os.path.join(self.dir, 'Manifest')) entries = m.get_file_entry_dict('', only_types=['DATA']) - self.assertListEqual(sorted(entries), ['test.ebuild']) - self.assertEqual(entries['test.ebuild'].tag, 'DATA') + self.assertDictEqual(dict([(k, sorted(v)) for k, v in entries.items()]), + {'': ['test.ebuild']}) + self.assertEqual(entries['']['test.ebuild'].tag, 'DATA') def test_deduplicated_get_file_entry_dict_for_update(self): m = gemato.recursiveloader.ManifestRecursiveLoader( |