diff options
author | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2014-06-18 15:40:21 +0000 |
---|---|---|
committer | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2014-06-19 08:44:32 +0000 |
commit | f45dcd6e8465371f70b4f15ea60a8f1f33dba5f5 (patch) | |
tree | d240067438ebc321f3f82c4eada8a09b17cb9366 | |
parent | d5ee8bdc636f5830f897b1846522b64bd5f06ebf (diff) | |
download | morph-f45dcd6e8465371f70b4f15ea60a8f1f33dba5f5.tar.gz |
wip: Add wrapper for a manifest of refs in definitions
-rw-r--r-- | morphlib/refmanifest.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/morphlib/refmanifest.py b/morphlib/refmanifest.py new file mode 100644 index 00000000..05a83231 --- /dev/null +++ b/morphlib/refmanifest.py @@ -0,0 +1,71 @@ +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import cliapp +import os +import yaml + +import morphlib + + +class RefManifest(object): + + """Class to represent the ref-manifest file.""" + + def __init__(self, contents, location): + self.chunks = contents + self.location = location + + @classmethod + def load_from_file(cls, filepath): + with open(filepath, 'r') as f: + return RefManifest(yaml.load(f), filepath) + + def _get_chunk(self, chunk_name): + if not chunk_name in self.chunks: + raise morphlib.Error( + 'Chunk %s is not in the ref-manifest' % chunk_name) + return self.chunks[chunk_name] + + def chunk_has_ref(self, chunk_name, ref): + chunk = self._get_chunk(chunk_name) + for pair in chunk: + if ref in pair: + return True + return False + + def chunk_ref_different(self, chunk_name, ref, sha): + chunk = self._get_chunk(chunk_name) + if {ref: sha} in chunk: + return True + return False + + def get_chunk_ref(self, chunk_name, ref): + chunk = self._get_chunk(chunk_name) + for pair in chunk: + if ref in pair: + return pair + + def set_chunk_ref(self, chunk_name, ref, sha): + chunk = self._get_chunk(chunk_name) + if self.chunk_has_ref(chunk_name, ref): + chunk.remove(self.get_chunk_ref(chunk_name, ref)) + chunk.append({ref: sha}) + + def save_to_file(self, path=self.location): + with open(path, 'w') as ref_manifest: + ref_manifest.write( + yaml.dump(self.chunks, default_flow_style=False)) |