On Wed, Nov 28, 2012 at 02:12:47PM +0000, Jonathan Maw wrote:
This script reads a built baserock system and finds all the chunks
used to build it in the artifact cache
---
scripts/find-artifacts | 118 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 118 insertions(+)
create mode 100755 scripts/find-artifacts
diff --git a/scripts/find-artifacts b/scripts/find-artifacts
new file mode 100755
index 0000000..2b23e7d
--- /dev/null
+++ b/scripts/find-artifacts
@@ -0,0 +1,118 @@
snip
+path = os.path.dirname(os.path.dirname(__file__))
+sys.path.append(path)
+import morphlib
This makes me cringe, I'm of the opinion that it is better to set
PYTHONPATH in your environment before running it if you want to run it
from the git working directory.
+# MountableImage yanked from the TrebuchetPlugin and modified
slightly
I'd prefer if it were refactored into morphlib.util if the modifications
are small enough.
+class FindArtifacts(cliapp.Application):
+
+ def add_settings(self):
+ self.settings.string(['cachedir'], 'Where the cache basedir
is')
+ self.settings.string(['tempdir'], 'Where the tempdir is')
These descriptions don't actually say anything new, you already know
that you need to say where the cache directory is, because the option is
called cachedir.
I'd either copy the descriptions from morphlib.app, or say that the
cachedir is the directory where morph writes to which is used to find
the artifacts.
+
+ def process_args(self, args):
+ # args[0] is the path to the built image.
+ # Mount the image
+ mount_point = None
+ with MountableImage(self, args[0]) as mount_point:
+ # For each meta file:
+ metadir = os.path.join(mount_point, 'factory-run',
'baserock')
+ for metafile in os.listdir(metadir):
I would use glob.iglob() to only use metafiles that end in .meta, in case we
store other files in there in future, but that may never happen and it's
easy enough to if it needs to be.
+ meta = os.path.splitext(metafile)[0]
This is not a very usefully named variable, I have no idea what it is.
+ metafilepath = os.path.join(metadir, metafile)
+ # Read the file as JSON and extract the kind and cache-key
+ metajson = json.load(open(metafilepath))
+ meta_kind = metajson['kind']
+ cache_key = metajson['cache-key']
+ # Look in the artifact cache for this artifact
+ chunk_filename = cache_key + '.' + meta_kind + '.' +
meta
I find it's a bit more readable to use string formatting than
concatenation, so it would be
chunk_filename = "%s.%s.%s" % (cache_key, meta_kind, meta)
+ chunk_path =
os.path.join(self.settings['cachedir'],
+ 'artifacts', chunk_filename)
+ # If it finds the artifact, print its path to stdout
+ # If it can't find the chunk, error and die
+ try:
+ os.path.exists(chunk_path)
+ except:
+ raise
+ print(chunk_path)
Use self.output.write(chunk_path + "\n") in cliapp programs, it allows
the output file to be set by config, rather than output redirection.