diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2012-04-20 12:22:30 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2012-04-20 12:26:15 +0000 |
commit | 29495567ce1787e426687d7d59653874e2efdcf4 (patch) | |
tree | 8615b9b52d10bdad6badd48b32aa555da07ca1a3 | |
parent | c84b5c726509c07c3a1803121cb21046a7987ac7 (diff) | |
download | morph-29495567ce1787e426687d7d59653874e2efdcf4.tar.gz |
morphologyfactory: infer build system without list
Detecting the build system is managed by it asking if any files exist
detecting if the file exists is done with a callback function.
This callback can use cat-file.
If list_files existed this could be more efficient as it would not
require the files to be read from the remote server and it only needs
to be one round-trip
-rw-r--r-- | morphlib/morphologyfactory.py | 17 | ||||
-rw-r--r-- | morphlib/morphologyfactory_tests.py | 62 |
2 files changed, 53 insertions, 26 deletions
diff --git a/morphlib/morphologyfactory.py b/morphlib/morphologyfactory.py index e7ee9323..6f9915fb 100644 --- a/morphlib/morphologyfactory.py +++ b/morphlib/morphologyfactory.py @@ -57,14 +57,25 @@ class MorphologyFactory(object): raise NotcachedError(reponame) def _autodetect_text(self, reponame, sha1, filename): + # TODO get lists of files from the cache to reduce round trips if self._lrc.has_repo(reponame): repo = self._lrc.get_repo(reponame) - files = repo.list_files(sha1) + def has_file(filename): + try: + repo.cat(sha1, filename) + return True + except IOError: + return False elif self._rrc is not None: - files = self._rrc.list_files(reponame, sha1) + def has_file(filename): + try: + text = self._rrc.cat_file(reponame, sha1, filename) + return True + except morphlib.remoterepocache.CatFileError: + return False else: raise NotcachedError(reponame) - bs = morphlib.buildsystem.detect_build_system(lambda x: x in files) + bs = morphlib.buildsystem.detect_build_system(has_file) if bs is None: raise AutodetectError(reponame, sha1) # TODO consider changing how morphs are located to be by morph diff --git a/morphlib/morphologyfactory_tests.py b/morphlib/morphologyfactory_tests.py index 5748aca0..0cd9e4b1 100644 --- a/morphlib/morphologyfactory_tests.py +++ b/morphlib/morphologyfactory_tests.py @@ -20,26 +20,27 @@ from morphlib.morph2 import Morphology from morphlib.morphologyfactory import (MorphologyFactory, AutodetectError, NotcachedError) +from morphlib.remoterepocache import CatFileError class FakeRemoteRepoCache(object): def cat_file(self, reponame, sha1, filename): - return '''{ - "name": "remote-foo", - "kind": "chunk", - "build-system": "bar" - }''' - def list_files(self, reponame, sha1): - return ['configure', 'Makefile'] + if filename.endswith('.morph'): + return '''{ + "name": "remote-foo", + "kind": "chunk", + "build-system": "bar" + }''' + return 'text' class FakeLocalRepo(object): def cat(self, sha1, filename): - return '''{ - "name": "local-foo", - "kind": "chunk", - "build-system": "bar" - }''' - def list_files(self, sha1): - return ['configure', 'Makefile'] + if filename.endswith('.morph'): + return '''{ + "name": "local-foo", + "kind": "chunk", + "build-system": "bar" + }''' + return 'text' class FakeLocalRepoCache(object): def __init__(self, lr): @@ -57,8 +58,19 @@ class MorphologyFactoryTests(unittest.TestCase): self.mf = MorphologyFactory(self.lrc, self.rrc) self.lmf = MorphologyFactory(self.lrc, None) - def nosuchfile(self): + def nolocalfile(self, *args): raise IOError('File not found') + def noremotefile(self, *args): + raise CatFileError('reponame', 'ref', 'filename') + def nolocalmorph(self, *args): + if args[-1].endswith('.morph'): + raise IOError('File not found') + return 'text' + def noremotemorph(self, *args): + if args[-1].endswith('.morph'): + raise CatFileError('reponame', 'ref', 'filename') + return 'text' + def doesnothaverepo(self, reponame): return False @@ -74,23 +86,27 @@ class MorphologyFactoryTests(unittest.TestCase): self.assertEqual('remote-foo', morph['name']) def test_autodetects_local_morphology(self): - self.lr.cat = self.nosuchfile + self.lr.cat = self.nolocalmorph morph = self.mf.get_morphology('reponame', 'sha1', 'assumed-local.morph') self.assertEqual('assumed-local', morph['name']) def test_autodetects_remote_morphology(self): self.lrc.has_repo = self.doesnothaverepo - self.rrc.cat_file = self.nosuchfile + self.rrc.cat_file = self.noremotemorph morph = self.mf.get_morphology('reponame', 'sha1', 'assumed-remote.morph') self.assertEqual('assumed-remote', morph['name']) - def test_raises_error_when_fails_detect(self): - self.lr.cat = self.nosuchfile - self.rrc.cat_file = self.nosuchfile - self.lr.list_files = lambda x: ['.'] - self.rrc.list_files = lambda x: ['.'] + def test_raises_error_when_fails_detect_locally(self): + self.lr.cat = self.nolocalfile + self.assertRaises(AutodetectError, self.mf.get_morphology, + 'reponame', 'sha1', 'unreached.morph') + + def test_raises_error_when_fails_detect_remotely(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, 'reponame', 'sha1', 'unreached.morph') @@ -100,7 +116,7 @@ class MorphologyFactoryTests(unittest.TestCase): self.assertEqual('local-foo', morph['name']) def test_autodetects_locally_with_no_remote(self): - self.lr.cat = self.nosuchfile + self.lr.cat = self.nolocalmorph morph = self.lmf.get_morphology('reponame', 'sha1', 'assumed-local.morph') self.assertEqual('assumed-local', morph['name']) |