From 3f03960384881dfb41050599f9cb224f3a134a9e Mon Sep 17 00:00:00 2001 From: Michał Górny Date: Fri, 27 Oct 2017 21:24:08 +0200 Subject: recursiveloader: Support saving Manifests --- gemato/recursiveloader.py | 17 +++++++++++++++++ tests/test_recursiveloader.py | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/gemato/recursiveloader.py b/gemato/recursiveloader.py index 96a5365..16c188b 100644 --- a/gemato/recursiveloader.py +++ b/gemato/recursiveloader.py @@ -60,6 +60,23 @@ class ManifestRecursiveLoader(object): self.loaded_manifests[relpath] = m return m + def save_manifest(self, relpath): + """ + Save a single Manifest file whose relative path within Manifest + tree is @relpath. The Manifest must already be loaded. + If the name indicates compression, it will be compressed + transparently. If it was OpenPGP-signed, a new signature + will be created. + """ + m = self.loaded_manifests[relpath] + path = os.path.join(self.root_directory, relpath) + with gemato.compression.open_potentially_compressed_path( + path, 'w', encoding='utf8') as f: + if m.openpgp_signed: + raise NotImplementedError( + 'Manifest signing not implemented yet') + m.dump(f) + def _iter_manifests_for_path(self, path, recursive=False): """ Iterate over loaded Manifests that can apply to path. diff --git a/tests/test_recursiveloader.py b/tests/test_recursiveloader.py index 29bfb0f..3debeaa 100644 --- a/tests/test_recursiveloader.py +++ b/tests/test_recursiveloader.py @@ -284,6 +284,17 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e os.path.join(self.dir, 'other')]), 1) + def test_save_manifest(self): + """ + Test if saving the (unmodified) Manifest works. + """ + m = gemato.recursiveloader.ManifestRecursiveLoader( + os.path.join(self.dir, 'Manifest')) + m.save_manifest('Manifest') + with io.open(os.path.join(self.dir, 'Manifest'), + 'r', encoding='utf8') as f: + self.assertEqual(f.read(), self.FILES['Manifest'].lstrip()) + class MultipleManifestTest(TempDirTestCase): DIRS = ['sub'] @@ -1045,6 +1056,14 @@ DATA test 0 MD5 d41d8cd98f00b204e9800998ecf8427e gemato.cli.main(['gemato', 'verify', self.dir]), 0) + def test_save_manifest(self): + m = gemato.recursiveloader.ManifestRecursiveLoader( + os.path.join(self.dir, 'Manifest.gz')) + m.save_manifest('Manifest.gz') + with gemato.compression.open_potentially_compressed_path( + os.path.join(self.dir, 'Manifest.gz'), 'rb') as f: + self.assertEqual(f.read(), self.MANIFEST.lstrip()) + class CompressedSubManifestTest(TempDirTestCase): """ -- cgit v1.2.3