Load functions file if it's valid. Check that the functions file is valid. If so, import it and return the function object.
00740 : '''Load functions file if it's valid. Check that the functions file is valid. If so, import it and return the function object. ''' app = gnome.program_get() try: filename = app.locate_file(gnome.FILE_DOMAIN_DATADIR, os.path.join(PROGRAMNAME, 'data', self.functionFile), True) except AttributeError: # We were unable to get a gnome program. Try to find a # functions file in the same directory as the checklist. filename = os.path.join(self.fileLoadDir, self.functionFile) else: try: filename = filename except IndexError: ### FIXME: This pathway is untested. Won't be tested until # we support checklists in user defined locations. # (ie: User specifies to load custom checklist from home # directory. Custom checklist has a function file in that # directory.) filename = os.path.join(self.fileLoadDir, self.functionFile) try: functionFile = file(filename) except IOError: raise error.InvalidFunctions, ('Unable to open the functions' ' file %s.' % (filename)) # Make sure functionFile matches the hash. if self.functionHashType == 'sha1': import sha as thehash elif self.functionHashType == 'md5': import md5 as thehash elif self.functionHashType == 'nullhash': thehash = NullHash() else: raise error.UnknownHashType, ('The checklist says the functions' ' file has hash %s of type %s which is an unknown type' % (self.functionHash, self.functionHashType)) hasher = thehash.new(''.join(functionFile.readlines())) if hasher.hexdigest() != self.functionHash: raise error.InvalidFunctions, ('Function file %s does not match' ' recorded hash' % (self.functionFile)) # If so, load the file sys.path.append(os.path.dirname(filename)) exec ('import ' + self.functionFile[0:-3] + ' as functions') return functions.QAFunctions(self) # # Helpers to read a checklist