From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1PorX9-00057Q-Ik for garchives@archives.gentoo.org; Mon, 14 Feb 2011 06:03:40 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A44A3E0960; Mon, 14 Feb 2011 06:00:26 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 645C1E0960 for ; Mon, 14 Feb 2011 06:00:26 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 0ABCE1B4094 for ; Mon, 14 Feb 2011 06:00:26 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 6F5C48006E for ; Mon, 14 Feb 2011 06:00:25 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: Subject: [gentoo-commits] proj/layman:master commit in: layman/ X-VCS-Repository: proj/layman X-VCS-Files: layman/api.py X-VCS-Directories: layman/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: af8c821f1b6c1a48b5ce2f6ba328ba9ff8807a9d Date: Mon, 14 Feb 2011 06:00:25 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 8bb61382394253e8b26452e49f03fd34 commit: af8c821f1b6c1a48b5ce2f6ba328ba9ff8807a9d Author: Brian Dolbec gmail com> AuthorDate: Sun Jul 11 04:20:44 2010 +0000 Commit: Brian Dolbec gmail com> CommitDate: Fri Feb 11 10:49:14 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/layman.git;a=3D= commit;h=3Daf8c821f Modify the API to be more compatible with a c interface. Make all main repo functions accept either a string or a list of strings. Change most functions to return True/False for success/failure and save e= rror messages for separate retrieval. Change other functions to return a dictionary instead of tuples or lists. Add a get_errors() for retrieving and resetting error messages. Change sync to either save or print the success, warnings, fatals info. --- layman/api.py | 124 ++++++++++++++++++++++++++++++++++++++++-----------= ------ 1 files changed, 87 insertions(+), 37 deletions(-) diff --git a/layman/api.py b/layman/api.py index f163f86..e17d70e 100644 --- a/layman/api.py +++ b/layman/api.py @@ -58,6 +58,8 @@ class LaymanAPI(object): self._installed_ids =3D None self._available_db =3D None self._available_ids =3D None + self._error_messages =3D [] + self.sync_results =3D [] # call reload() for now to initialize the 2 db's self.reload() # change it to delayed loading (similar to delayed imports) @@ -84,6 +86,16 @@ class LaymanAPI(object): return id in self._installed_ids =20 =20 + @staticmethod + def _check_repo_type( repos, caller): + if isinstance(repos, str): + repos =3D [repos] + elif not isinstance(repos, list): + self._error(2, "%s(), Unsupported input type: %s" %(caller, = str(type(repos)))) + return [] + return repos + + def delete_repo(self, repos): """delete the selected repo from the system =20 @@ -92,24 +104,27 @@ class LaymanAPI(object): @param output: method to handle output if desired @rtype dict """ - results =3D {} + repos =3D self._check_repo_type(repos, "delete_repo") + results =3D [] for id in repos: if not self.is_installed(id): - results[id] =3D True + results.append(True) break if not self.is_repo(id): - self.error(1, UNKNOWN_REPO_ID %id) - results[id] =3D False + self._error(1, UNKNOWN_REPO_ID %id) + results.append(False) break try: self._installed_db.delete(self._installed_db.select(id)) - results[id] =3D True + results.append(True) except Exception, e: - self.error(ERROR_INTERNAL_ERROR, + self._error(ERROR_INTERNAL_ERROR, "Failed to disable repository '"+id+"':\n"+str(e= )) - results[id] =3D False + results.append(False) self.get_installed(reload=3DTrue) - return results + if False in results: + return False + return True =20 =20 def add_repo(self, repos): @@ -120,24 +135,27 @@ class LaymanAPI(object): @param output: method to handle output if desired @rtype dict """ - results =3D {} + repos =3D self._check_repo_type(repos, "add_repo") + results =3D [] for id in repos: if self.is_installed(id): - results[id] =3D True + results.append(True) break if not self.is_repo(id): - self.error(1, UNKNOWN_REPO_ID %id) - results[id] =3D False + self._error(1, UNKNOWN_REPO_ID %id) + results.append(False) break try: self._installed_db.add(self._available_db.select(id), qu= iet=3DTrue) - results[id] =3D True + results.append(True) except Exception, e: - self.error(ERROR_INTERNAL_ERROR, + self._error(ERROR_INTERNAL_ERROR, "Failed to enable repository '"+id+"' : "+str(e)= ) - results[id] =3D False + results.append(False) self.get_installed(reload=3DTrue) - return results + if False in results: + return False + return True =20 =20 def get_info(self, repos): @@ -146,53 +164,55 @@ class LaymanAPI(object): @type repos: list @param repos: ['repo-id1', ...] @rtype list of tuples [(str, bool, bool),...] - @return: (info, official, supported) + @return: dictionary {'id': (info, official, supported)} """ - result =3D [] + repos =3D self._check_repo_type(repos, "get_info") + result =3D {} =20 for id in repos: if not self.is_repo(id): - self.error(1, UNKNOWN_REPO_ID %id) - result.append(('', False, False)) + self._error(1, UNKNOWN_REPO_ID %id) + result[id] =3D ('', False, False)) try: overlay =3D self._available_db.select(id) except UnknownOverlayException, error: - self.error(2, "Error: %s" %str(error)) - result.append(('', False, False)) + self._error(2, "Error: %s" %str(error)) + result[id] =3D ('', False, False)) else: # Is the overlay supported? info =3D overlay.__str__() official =3D overlay.is_official() supported =3D overlay.is_supported() - result.append((info, official, supported)) + result[id] =3D (info, official, supported) =20 return result =20 =20 - def sync(self, repos): + def sync(self, repos, output_results=3DTrue): """syncs the specified repo(s) specified by repos =20 - @type repos: list - @param repos: ['repo-id1', ...] - @rtype bool + @type repos: list or string + @param repos: ['repo-id1', ...] or 'repo-id' + @rtype bool or {'repo-id': bool,...} """ - =20 - fatals =3D [] + fatal =3D [] warnings =3D [] success =3D [] + repos =3D self._check_repo_type(repos, "sync") + for id in repos: try: odb =3D self._installed_db.select(id) except UnknownOverlayException, error: - fatals.append((id, str(error))) + self._error(1,"Sync(), failed to select %s overlay. Ori= ginal error was: %s" %(id, str(error))) continue =20 try: ordb =3D self._available_db.select(id) except UnknownOverlayException: - warnings.append((id, - 'Overlay "%s" could not be found in the remote lists= .\n' - 'Please check if it has been renamed and re-add if n= ecessary.', {'repo_name':id})) + message =3D 'Overlay "%s" could not be found in the remo= te lists.\n' + 'Please check if it has been renamed and re-add = if necessary.' %id + warnings.append((id, message)) else: current_src =3D odb.sources[0].src available_srcs =3D set(e.src for e in ordb.sources) @@ -230,7 +250,26 @@ class LaymanAPI(object): 'Failed to sync overlay "' + id + '".\nError was: ' + str(error))) =20 - return (warnings, success, fatals) + if output_results: + if success: + self.output.info('\nSuccess:\n------\n', 3) + for result in success: + self.output.info(result, 3) + =20 + if warnings: + self.output.warn('\nWarnings:\n------\n', 2) + for result in warnings: + self.output.warn(result + '\n', 2) + + if fatals: + self.output.error('\nErrors:\n------\n') + for result in fatals: + self.output.error(result + '\n') + return False + else: + self.sync_results =3D (success, warnings, fatals) + + return True =20 =20 def fetch_remote_list(self): @@ -238,7 +277,7 @@ class LaymanAPI(object): try: self._available_db.cache() except Exception, error: - self.error(-1,'Failed to fetch overlay list!\n Original Erro= r was: ' + self._error(-1,'Failed to fetch overlay list!\n Original Err= or was: ' + str(error)) return False return True @@ -266,13 +305,24 @@ class LaymanAPI(object): result =3D self.get_installed(reload=3DTrue) =20 =20 - def error(self, num, message): + def _error(self, num, message): """outputs the error to the pre-determined output defaults to stderr. This method may be removed, is here for now due to code taken from the packagekit backend. """ + m =3D "Error: %d," %num, message + self._error_messages.append(m) if self.report_errors: - print >>stderr, "Error: %d," %num, message + print >>stderr, m + + + def get_errors(self): + """returns any warning or fatal messages that occurred during + an operation and resets it back to None + """ + if self._error_messages: + return self._error_messages[:] + self._error_messages =3D [] =20 =20 class Output(Message):