diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2014-06-18 14:07:14 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2014-06-19 16:21:37 +0000 |
commit | 6b4cb5a5a58d5a536369a20612ff7c656cdd0921 (patch) | |
tree | 38d7bbb4248d9eb709d59830f8b254889c0a0283 | |
parent | d9bd0a5781b30663da240e8c8152ffb4843db2ca (diff) | |
download | morph-6b4cb5a5a58d5a536369a20612ff7c656cdd0921.tar.gz |
Don't check if a file exists before trying to read it
We used to check whether a file existed before trying to read it. We
used to be able to get away with only looking at the top-level
directory, which made using ls-files before trying to cat-file it
better.
Unfortunately, we need to look at the files in subdirectories now, so
this no longer works.
We could make it include files in subdirectories, but for repositories
with many files, you would end up reading a file listing longer than
the morphology, so even in the slow case of needing to read the entire
morphology file, it would be faster to attempt to read the file first.
So now we beg forgiveness rather than asking permission.
-rw-r--r-- | morphlib/morphologyfactory.py | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/morphlib/morphologyfactory.py b/morphlib/morphologyfactory.py index 8a0b047a..9f0877ce 100644 --- a/morphlib/morphologyfactory.py +++ b/morphlib/morphologyfactory.py @@ -14,6 +14,8 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import os + import morphlib import cliapp @@ -76,20 +78,24 @@ class MorphologyFactory(object): def get_morphology(self, reponame, sha1, filename): - text = None + morph_name = os.path.splitext(os.path.basename(filename))[0] if self._lrc.has_repo(reponame): repo = self._lrc.get_repo(reponame) - file_list = repo.ls_tree(sha1) - if filename in file_list: + try: text = repo.cat(sha1, filename) + except IOError: + text = None + file_list = repo.ls_tree(sha1) elif self._rrc is not None: - 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 %(reponame)s %(sha1)s %(filename)s" + " from the remote artifact cache.", + reponame=reponame, sha1=sha1, filename=filename, + chatty=True) + try: text = self._rrc.cat_file(reponame, sha1, filename) + except morphlib.remoterepocache.CatFileError: + text = None + file_list = self._rrc.ls_tree(reponame, sha1) else: raise NotcachedError(reponame) @@ -97,12 +103,6 @@ class MorphologyFactory(object): 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) try: @@ -111,7 +111,7 @@ class MorphologyFactory(object): raise morphlib.Error("Error parsing %s: %s" % (filename, str(e))) - if filename != morphology['name'] + '.morph': + if morph_name != morphology['name']: raise morphlib.Error( "Name %s does not match basename of morphology file %s" % (morphology['name'], filename)) |