From 554391c3df84e9df6565317b1d6725e536569297 Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Wed, 25 Jun 2014 16:13:13 +0100 Subject: Add chatty status messages to aid the user --- morphlib/morphologyfactory.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/morphlib/morphologyfactory.py b/morphlib/morphologyfactory.py index 8a0b047a..891a2de7 100644 --- a/morphlib/morphologyfactory.py +++ b/morphlib/morphologyfactory.py @@ -63,7 +63,7 @@ class NoStratumBuildDependsError(StratumError): class MorphologyFactory(object): - '''An way of creating morphologies which will provide a default''' + '''A way of creating morphologies which will provide a default''' def __init__(self, local_repo_cache, remote_repo_cache=None, app=None): self._lrc = local_repo_cache @@ -78,22 +78,29 @@ class MorphologyFactory(object): text = None if self._lrc.has_repo(reponame): + self.status(msg="Looking for %s in local repo cache" % filename, + chatty=True) repo = self._lrc.get_repo(reponame) file_list = repo.ls_tree(sha1) if filename in file_list: text = repo.cat(sha1, filename) elif self._rrc is not None: + self.status(msg="Looking for %s in remote repo cache" % filename, + chatty=True) file_list = self._rrc.ls_tree(reponame, sha1) if filename in file_list: - self.status(msg="Retrieving %(reponame)s %(sha1)s %(filename)s" - " from the remote artifact cache.", - reponame=reponame, sha1=sha1, filename=filename, - chatty=True) + self.status(msg='Retrieving %s %s %s' + 'from the remote artifact cache.' + % (reponame, sha1, filename), chatty=True) text = self._rrc.cat_file(reponame, sha1, filename) else: raise NotcachedError(reponame) if text is None: + self.status( + msg="File %s doesn't exist: " + "attempting to infer chunk morph from repo's build system" + % filename, chatty=True) bs = morphlib.buildsystem.detect_build_system(file_list) if bs is None: raise AutodetectError(reponame, sha1, filename) -- cgit v1.2.1 From f1409e9fe46cc68b8ff4f7c9a8fc6d8ecb11225c Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Wed, 25 Jun 2014 17:06:30 +0100 Subject: Replace AutodetectError with MorphologyNotFoundError The error message: ERROR: Failed to determine the build system of repo file://foo/bar/baz at ref 59713cf997385a094091443fdcce9d5c17313f39: was looking for distbuild-system-x86-64.morph is confusing since our system morph has nothing to do with build systems, the fact that build system autodetection is executed when looking for distbuild-system-x86-64.morph is an implementation detail that shouldn't be exposed to the user. This patch replaces this error message with: ERROR: Couldn't find morphology: distbuild-system-x86-64.morph This is still not ideal, since there are cases where we may not be able find the morph because build system autodection has failed, but out of the two user typos/mistakes are probably more likely. Differentiating between user error and build system detection failure would require more substantial changes. This patch also splits the get_morphology function into two functions _get_morphology_text, that actually fetches the morph text from the cache or otherwise infers it from the build system, and get_morphology which constructs a Morphology object from the text. --- morphlib/morphologyfactory.py | 48 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/morphlib/morphologyfactory.py b/morphlib/morphologyfactory.py index 891a2de7..cd59d4ec 100644 --- a/morphlib/morphologyfactory.py +++ b/morphlib/morphologyfactory.py @@ -22,11 +22,10 @@ class MorphologyFactoryError(cliapp.AppException): pass -class AutodetectError(MorphologyFactoryError): - def __init__(self, repo_name, ref, filename): +class MorphologyNotFoundError(MorphologyFactoryError): + def __init__(self, filename): MorphologyFactoryError.__init__( - self, "Failed to determine the build system of repo %s at " - "ref %s: was looking for %s" % (repo_name, ref, filename)) + self, "Couldn't find morphology: %s" % filename) class NotcachedError(MorphologyFactoryError): @@ -74,43 +73,44 @@ class MorphologyFactory(object): if self._app is not None: self._app.status(*args, **kwargs) - def get_morphology(self, reponame, sha1, filename): - - text = None + def _get_morphology_text(self, reponame, sha1, filename): if self._lrc.has_repo(reponame): self.status(msg="Looking for %s in local repo cache" % filename, chatty=True) repo = self._lrc.get_repo(reponame) file_list = repo.ls_tree(sha1) + if filename in file_list: - text = repo.cat(sha1, filename) + return repo.cat(sha1, filename) elif self._rrc is not None: self.status(msg="Looking for %s in remote repo cache" % filename, chatty=True) file_list = self._rrc.ls_tree(reponame, sha1) + if filename in file_list: self.status(msg='Retrieving %s %s %s' 'from the remote artifact cache.' % (reponame, sha1, filename), chatty=True) - text = self._rrc.cat_file(reponame, sha1, filename) + return self._rrc.cat_file(reponame, sha1, filename) else: raise NotcachedError(reponame) - if text is None: - self.status( - msg="File %s doesn't exist: " - "attempting to infer chunk morph from repo's build system" - % filename, chatty=True) - bs = morphlib.buildsystem.detect_build_system(file_list) - if bs is None: - raise AutodetectError(reponame, sha1, filename) - # TODO consider changing how morphs are located to be by morph - # name rather than filename, it would save creating a - # filename only to strip it back to its morph name again - # and would allow future changes like morphologies being - # stored as git metadata instead of as a file in the repo - morph_name = filename[:-len('.morph')] - text = bs.get_morphology_text(morph_name) + self.status(msg="File %s doesn't exist: " + "attempting to infer chunk morph from repo's build system" + % filename, chatty=True) + bs = morphlib.buildsystem.detect_build_system(file_list) + if bs is None: + raise MorphologyNotFoundError(filename) + # TODO consider changing how morphs are located to be by morph + # name rather than filename, it would save creating a + # filename only to strip it back to its morph name again + # and would allow future changes like morphologies being + # stored as git metadata instead of as a file in the repo + morph_name = filename[:-len('.morph')] + return bs.get_morphology_text(morph_name) + + def get_morphology(self, reponame, sha1, filename): + text = self._get_morphology_text(reponame, sha1, filename) try: morphology = morphlib.morph2.Morphology(text) -- cgit v1.2.1 From 244feb97ef207ba724f080826d6b4ad3a2d7cd33 Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Wed, 25 Jun 2014 16:19:09 +0100 Subject: Make morph factory tests expect MorphologyNotFoundError --- morphlib/morphologyfactory_tests.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/morphlib/morphologyfactory_tests.py b/morphlib/morphologyfactory_tests.py index 39092857..47bf3153 100644 --- a/morphlib/morphologyfactory_tests.py +++ b/morphlib/morphologyfactory_tests.py @@ -19,7 +19,7 @@ import unittest import morphlib from morphlib.morph2 import Morphology from morphlib.morphologyfactory import (MorphologyFactory, - AutodetectError, + MorphologyNotFoundError, NotcachedError) from morphlib.remoterepocache import CatFileError @@ -234,16 +234,15 @@ class MorphologyFactoryTests(unittest.TestCase): 'assumed-remote.morph') self.assertEqual('assumed-remote', morph['name']) - def test_raises_error_when_fails_detect_locally(self): + def test_raises_error_when_no_local_morph(self): self.lr.cat = self.nolocalfile - self.assertRaises(AutodetectError, self.mf.get_morphology, + self.assertRaises(MorphologyNotFoundError, self.mf.get_morphology, 'reponame', 'sha1', 'unreached.morph') - def test_raises_error_when_fails_detect_remotely(self): + def test_raises_error_when_fails_no_remote_morph(self): self.lrc.has_repo = self.doesnothaverepo self.rrc.cat_file = self.noremotefile -# self.mf.get_morphology('reponame', 'sha1', 'unreached.morph') - self.assertRaises(AutodetectError, self.mf.get_morphology, + self.assertRaises(MorphologyNotFoundError, self.mf.get_morphology, 'reponame', 'sha1', 'unreached.morph') def test_raises_error_when_name_mismatches(self): -- cgit v1.2.1