From 6b4cb5a5a58d5a536369a20612ff7c656cdd0921 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 18 Jun 2014 14:07:14 +0000 Subject: 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. --- morphlib/morphologyfactory.py | 32 ++++++++++++++++---------------- 1 file 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)) -- cgit v1.2.1