diff options
author | Michał Górny <mgorny@gentoo.org> | 2017-10-22 20:29:08 +0200 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2017-10-22 20:29:08 +0200 |
commit | 2b1c43e0a02fe70115dd38f643b09b24be6f4e97 (patch) | |
tree | 619971841358f6a72c0fe961ac3af699866132d8 | |
parent | a2394383ab0ec57c1ba3a5b7860e6da1cddddeb6 (diff) | |
download | gemato-2b1c43e0a02fe70115dd38f643b09b24be6f4e97.tar.gz |
Support dumping Manifest file back
-rw-r--r-- | gemato/manifest.py | 44 | ||||
-rw-r--r-- | tests/test_manifest.py | 33 |
2 files changed, 75 insertions, 2 deletions
diff --git a/gemato/manifest.py b/gemato/manifest.py index 195a4aa..61bb5b5 100644 --- a/gemato/manifest.py +++ b/gemato/manifest.py @@ -33,6 +33,9 @@ class ManifestEntryTIMESTAMP(object): '{} line: expected ISO8601 timestamp, got: {}'.format(l[0], l[1:])) return cls(ts) + def to_list(self): + return ('TIMESTAMP', self.ts.strftime('%Y-%m-%dT%H:%M:%SZ')) + class ManifestPathEntry(object): """ @@ -63,6 +66,9 @@ class ManifestEntryIGNORE(ManifestPathEntry): def from_list(cls, l): return cls(cls.process_path(l)) + def to_list(self): + return ('IGNORE', self.path) + class ManifestEntryOPTIONAL(ManifestPathEntry): """ @@ -78,6 +84,9 @@ class ManifestEntryOPTIONAL(ManifestPathEntry): def from_list(cls, l): return cls(cls.process_path(l)) + def to_list(self): + return ('OPTIONAL', self.path) + class ManifestFileEntry(ManifestPathEntry): """ @@ -119,6 +128,12 @@ class ManifestFileEntry(ManifestPathEntry): return size, checksums + def to_list(self, tag): + ret = [tag, self.path, str(self.size)] + for k, v in sorted(self.checksums.items()): + ret += [k, v] + return ret + class ManifestEntryMANIFEST(ManifestFileEntry): """ @@ -131,6 +146,9 @@ class ManifestEntryMANIFEST(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + return super(ManifestEntryMANIFEST, self).to_list('MANIFEST') + class ManifestEntryDATA(ManifestFileEntry): """ @@ -143,6 +161,9 @@ class ManifestEntryDATA(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + return super(ManifestEntryDATA, self).to_list('DATA') + class ManifestEntryMISC(ManifestFileEntry): """ @@ -155,6 +176,9 @@ class ManifestEntryMISC(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + return super(ManifestEntryMISC, self).to_list('MISC') + class ManifestEntryDIST(ManifestFileEntry): """ @@ -170,6 +194,9 @@ class ManifestEntryDIST(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + return super(ManifestEntryDIST, self).to_list('DIST') + class ManifestEntryEBUILD(ManifestFileEntry): """ @@ -182,6 +209,9 @@ class ManifestEntryEBUILD(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + return super(ManifestEntryEBUILD, self).to_list('EBUILD') + class ManifestEntryAUX(ManifestFileEntry): """ @@ -199,6 +229,12 @@ class ManifestEntryAUX(ManifestFileEntry): size, checksums = cls.process_checksums(l) return cls(path, size, checksums) + def to_list(self): + ret = super(ManifestEntryAUX, self).to_list('AUX') + assert ret[1].startswith('files/') + ret[1] = ret[1][6:] + return ret + MANIFEST_TAG_MAPPING = { 'TIMESTAMP': ManifestEntryTIMESTAMP, @@ -224,6 +260,8 @@ class ManifestFile(object): Create a new instance. If @f is provided, reads the entries from open Manifest file @f (see load()). """ + self.entries = [] + if f is not None: self.load(f) @@ -239,7 +277,7 @@ class ManifestFile(object): if not sl: continue tag = sl[0] - MANIFEST_TAG_MAPPING[tag].from_list(sl) + self.entries.append(MANIFEST_TAG_MAPPING[tag].from_list(sl)) def dump(self, f): @@ -247,4 +285,6 @@ class ManifestFile(object): Dump data into file @f. The file should be open for writing in text mode, and truncated to zero length. """ - pass + + for e in self.entries: + f.write('{}\n'.format(' '.join(e.to_list()))) diff --git a/tests/test_manifest.py b/tests/test_manifest.py index aee5eba..1d29af4 100644 --- a/tests/test_manifest.py +++ b/tests/test_manifest.py @@ -41,6 +41,20 @@ class ManifestTest(unittest.TestCase): m = gemato.manifest.ManifestFile() m.load(io.StringIO(TEST_DEPRECATED_MANIFEST)) + def test_load_and_dump(self): + m = gemato.manifest.ManifestFile() + m.load(io.StringIO(TEST_MANIFEST)) + outf = io.StringIO() + m.dump(outf) + self.assertEqual(outf.getvalue().strip(), TEST_MANIFEST.strip()) + + def test_load_and_dump_deprecated(self): + m = gemato.manifest.ManifestFile() + m.load(io.StringIO(TEST_DEPRECATED_MANIFEST)) + outf = io.StringIO() + m.dump(outf) + self.assertEqual(outf.getvalue().strip(), TEST_DEPRECATED_MANIFEST.strip()) + class ManifestEntryTest(unittest.TestCase): """ @@ -58,6 +72,9 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(gemato.manifest.ManifestEntryTIMESTAMP.from_list( ('TIMESTAMP', '2010-01-01T11:12:13Z')).ts, datetime.datetime(2010, 1, 1, 11, 12, 13)) + self.assertListEqual(list(gemato.manifest.ManifestEntryTIMESTAMP( + datetime.datetime(2010, 1, 1, 11, 12, 13)).to_list()), + ['TIMESTAMP', '2010-01-01T11:12:13Z']) def test_MANIFEST(self): m = gemato.manifest.ManifestEntryMANIFEST.from_list( @@ -65,11 +82,15 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['MANIFEST'] + list(self.file_vals)) def test_IGNORE(self): self.assertEqual(gemato.manifest.ManifestEntryIGNORE.from_list( ('IGNORE', 'test')).path, 'test') + self.assertListEqual(list(gemato.manifest.ManifestEntryIGNORE('test').to_list()), + ['IGNORE', 'test']) def test_DATA(self): m = gemato.manifest.ManifestEntryDATA.from_list( @@ -77,6 +98,8 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['DATA'] + list(self.file_vals)) def test_MISC(self): m = gemato.manifest.ManifestEntryMISC.from_list( @@ -84,11 +107,15 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['MISC'] + list(self.file_vals)) def test_OPTIONAL(self): self.assertEqual(gemato.manifest.ManifestEntryOPTIONAL.from_list( ('OPTIONAL', 'test')).path, 'test') + self.assertListEqual(list(gemato.manifest.ManifestEntryOPTIONAL('test').to_list()), + ['OPTIONAL', 'test']) def test_DIST(self): m = gemato.manifest.ManifestEntryDIST.from_list( @@ -96,6 +123,8 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['DIST'] + list(self.file_vals)) def test_EBUILD(self): m = gemato.manifest.ManifestEntryEBUILD.from_list( @@ -103,6 +132,8 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['EBUILD'] + list(self.file_vals)) def test_AUX(self): m = gemato.manifest.ManifestEntryAUX.from_list( @@ -111,6 +142,8 @@ class ManifestEntryTest(unittest.TestCase): self.assertEqual(m.path, 'files/test') self.assertEqual(m.size, 0) self.assertDictEqual(m.checksums, self.exp_cksums) + self.assertListEqual(list(m.to_list()), + ['AUX'] + list(self.file_vals)) def test_timestamp_invalid(self): self.assertRaises(gemato.manifest.ManifestSyntaxError, |