[PATCH 05/17] Add a 'morph content-manifest' command to the image analysis plugin

Jonathan Maw jonathan.maw at codethink.co.uk
Thu Jan 24 18:45:10 GMT 2013


From: Jannis Pohlmann <jannis.pohlmann at codethink.co.uk>

---
 morphlib/plugins/image_analysis_plugin.py | 63 ++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 2 deletions(-)

diff --git a/morphlib/plugins/image_analysis_plugin.py b/morphlib/plugins/image_analysis_plugin.py
index 6cb1035..a1c2aaf 100644
--- a/morphlib/plugins/image_analysis_plugin.py
+++ b/morphlib/plugins/image_analysis_plugin.py
@@ -15,9 +15,9 @@
 
 
 import cliapp
+import glob
+import json
 import os
-import tempfile
-import gzip
 
 import morphlib
 
@@ -30,6 +30,9 @@ class ImageAnalysisPlugin(cliapp.Plugin):
         self.app.add_subcommand('run-in-system-image',
                                 self.run_in_image,
                                 arg_synopsis='IMAGE CMD')
+        self.app.add_subcommand('content-manifest',
+                                self.content_manifest,
+                                arg_synopsis='IMAGE')
 
     def disable(self):
         pass
@@ -48,3 +51,59 @@ class ImageAnalysisPlugin(cliapp.Plugin):
         with MountableImage(self.app, filename) as mount_point:
             output = self.app.runcmd(cmd, cwd=mount_point)
             self.app.output.write(output)
+
+    def content_manifest(self, args):
+        '''Generate a content manifest for a system image.'''
+
+        if len(args) != 1:
+            raise cliapp.AppException('content-manifest requires a '
+                                      'system image')
+
+        with MountableImage(self.app, args[0]) as mount_point:
+            metadir = os.path.join(mount_point, 'factory', 'baserock')
+            artifacts = []
+            for basename in glob.glob(os.path.join(metadir, '*.meta')):
+                metafile = os.path.join(metadir, basename)
+                metadata = json.load(open(metafile))
+
+                artifacts.append({
+                    'cache-key': metadata['cache-key'],
+                    'name': metadata['artifact-name'],
+                    'kind': metadata['kind'],
+                    'sha1': metadata['sha1'],
+                    'repo': metadata['repo'],
+                    'morphology': metadata['morphology']
+                })
+
+            fmt = self._generate_output_format(artifacts)
+
+            self._print_artifacts(fmt, artifacts, 'system')
+            self._print_artifacts(fmt, artifacts, 'stratum')
+            self._print_artifacts(fmt, artifacts, 'chunk')
+
+    def _generate_output_format(self, artifacts):
+        colwidths = {}
+        for artifact in artifacts:
+            for key, value in artifact.iteritems():
+                colwidths[key] = max(colwidths.get(key, 0), len(value))
+
+        colwidths['first'] = sum([colwidths['cache-key'],
+                                  colwidths['kind'],
+                                  colwidths['name']])
+
+        return '%%-%is\t%%-%is\t%%-%is\t%%-%is\n' % (
+                colwidths['first'],
+                colwidths['repo'],
+                colwidths['sha1'],
+                colwidths['morphology'])
+
+    def _print_artifacts(self, fmt, artifacts, kind):
+        for artifact in sorted(artifacts, key=lambda x: x['name']):
+            if artifact['kind'] == kind:
+                self.app.output.write(fmt % (
+                    '%s.%s.%s' % (artifact['cache-key'],
+                                  artifact['kind'],
+                                  artifact['name']),
+                    artifact['repo'],
+                    artifact['sha1'],
+                    artifact['morphology']))
-- 
1.7.11.7





More information about the baserock-dev mailing list