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 <gentoo-commits+bounces-316505-garchives=archives.gentoo.org@lists.gentoo.org>) id 1PorWu-00051p-Ql for garchives@archives.gentoo.org; Mon, 14 Feb 2011 06:03:25 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id EB29EE09D7; Mon, 14 Feb 2011 06:00:30 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 97BA2E09B6 for <gentoo-commits@lists.gentoo.org>; Mon, 14 Feb 2011 06:00:30 +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 1AC221B4221 for <gentoo-commits@lists.gentoo.org>; Mon, 14 Feb 2011 06:00:30 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 8556B8006A for <gentoo-commits@lists.gentoo.org>; Mon, 14 Feb 2011 06:00:29 +0000 (UTC) From: "Brian Dolbec" <brian.dolbec@gmail.com> To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" <brian.dolbec@gmail.com> Message-ID: <cf439af88990de7f7e0ef68faca1581e0480a458.dol-sen@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: layman/ X-VCS-Repository: proj/layman X-VCS-Files: layman/api.py layman/utils.py X-VCS-Directories: layman/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: cf439af88990de7f7e0ef68faca1581e0480a458 Date: Mon, 14 Feb 2011 06:00:29 +0000 (UTC) Precedence: bulk List-Post: <mailto:gentoo-commits@lists.gentoo.org> List-Help: <mailto:gentoo-commits+help@lists.gentoo.org> List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org> List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org> List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org> X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 9fdb21d35b18e1d69813e9206e57240a commit: cf439af88990de7f7e0ef68faca1581e0480a458 Author: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> AuthorDate: Mon Jan 17 06:56:06 2011 +0000 Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> CommitDate: Sun Feb 13 03:39:35 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/layman.git;a=3D= commit;h=3Dcf439af8 overhaul the api, add several functions, delay loading of the db's, etc --- layman/api.py | 235 ++++++++++++++++++++++++++++++++-----------------= ------ layman/utils.py | 2 + 2 files changed, 140 insertions(+), 97 deletions(-) diff --git a/layman/api.py b/layman/api.py index 4dacf99..dfce4a0 100644 --- a/layman/api.py +++ b/layman/api.py @@ -30,7 +30,7 @@ ERROR_REPO_NOT_FOUND =3D -1 ERROR_INTERNAL_ERROR =3D -2 UNKNOWN_REPO_ID =3D "Repo ID '%s' " + \ "is not listed in the current available overlays list" -=20 + # In order to redirect output you need to get a Message class instance w= ith the # stderr, stdout, stddebug directed to where you want. # eg: output =3D Message('layman', err=3Dmystderr, dbg=3Dmydebug, out=3D= myoutput) @@ -53,13 +53,13 @@ class LaymanAPI(object): @param output: optional Message class instance created with your= settings. default is Message(module=3D'layman'= ) other params are defaults. """ - =20 + self.output =3D output if output else OUT - =20 + self.config =3D config if config else BareConfig(output=3Doutput= ) - =20 + self.report_errors =3D report_errors - =20 + # get installed and available dbs self._installed_db =3D None self._installed_ids =3D None @@ -67,30 +67,26 @@ class LaymanAPI(object): 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) - # to simplify some of the code and make it automagic. =20 =20 - def is_repo(self, id): - """validates that the id given is a known repo id - =20 - @param id: repo id - @type id: str + def is_repo(self, ovl): + """validates that the ovl given is a known repo id + + @param ovl: repo id + @type ovl: str @rtype boolean """ - return id in self._available_ids + return ovl in self.get_available() + =20 + def is_installed(self, ovl): + """checks that ovl is a known installed repo id =20 - def is_installed(self, id): - """checks the repo id is a known installed repo id - =20 - @param id: repo id - @type id: str + @param ovl: repo id + @type ovl: str @rtype boolean """ - return id in self._installed_ids + return ovl in self.get_installed() =20 =20 @staticmethod @@ -102,15 +98,12 @@ class LaymanAPI(object): if isinstance(repos, basestring): repos =3D [repos] # else assume it is an iterable, if not it will error - #~ elif not isinstance(repos, list): - #~ self._error(2, "%s(), Unsupported input type: %s" %(calle= r, str(type(repos)))) - #~ return [] return repos =20 =20 def delete_repos(self, repos): """delete the selected repo from the system - =20 + @type repos: list of strings or string @param repos: ['repo-id1', ...] or 'repo-id' @param output: method to handle output if desired @@ -118,20 +111,20 @@ class LaymanAPI(object): """ repos =3D self._check_repo_type(repos, "delete_repo") results =3D [] - for id in repos: - if not self.is_installed(id): + for ovl in repos: + if not self.is_installed(ovl): results.append(True) break - if not self.is_repo(id): - self._error(1, UNKNOWN_REPO_ID %id) + if not self.is_repo(ovl): + self._error(1, UNKNOWN_REPO_ID %ovl) results.append(False) break try: - self._installed_db.delete(self._installed_db.select(id)) + self._get_installed_db().delete(self._get_installed_db()= .select(ovl)) results.append(True) except Exception, e: self._error(ERROR_INTERNAL_ERROR, - "Failed to disable repository '"+id+"':\n"+str(e= )) + "Failed to disable repository '"+ovl+"':\n"+str(= e)) results.append(False) self.get_installed(reload=3DTrue) if False in results: @@ -141,28 +134,28 @@ class LaymanAPI(object): =20 def add_repos(self, repos): """installs the seleted repo id - =20 + @type repos: list of strings or string - @param repos: ['repo-id1', ...] or 'repo-id' + @param repos: ['repo-id', ...] or 'repo-id' @param output: method to handle output if desired @rtype dict """ repos =3D self._check_repo_type(repos, "add_repo") results =3D [] - for id in repos: - if self.is_installed(id): + for ovl in repos: + if self.is_installed(ovl): results.append(True) break - if not self.is_repo(id): - self._error(1, UNKNOWN_REPO_ID %id) + if not self.is_repo(ovl): + self._error(1, UNKNOWN_REPO_ID %ovl) results.append(False) break try: - self._installed_db.add(self._available_db.select(id), qu= iet=3DTrue) + self._get_installed_db().add(self._get_remote_db().selec= t(ovl), quiet=3DTrue) results.append(True) except Exception, e: self._error(ERROR_INTERNAL_ERROR, - "Failed to enable repository '"+id+"' : "+str(e)= ) + "Failed to enable repository '"+ovl+"' : "+str(e= )) results.append(False) self.get_installed(reload=3DTrue) if False in results: @@ -170,15 +163,15 @@ class LaymanAPI(object): return True =20 =20 - def get_all_info(self, repos): + def get_all_info(self, repos, local=3DFalse): """retrieves the recorded information about the repo(s) - specified by id - =20 + specified by repo-id + @type repos: list of strings or string @param repos: ['repo-id1', ...] or 'repo-id' @rtype list of tuples [(str, bool, bool),...] - @return: dictionary of dictionaries =20 - {'id1': + @return: dictionary of dictionaries + {'ovl1': {'name': str, 'owner_name': str, 'owner_email': str, @@ -187,38 +180,44 @@ class LaymanAPI(object): 'src_uris': list of str ['uri1',...] 'src_type': str, 'priority': int, - 'quality': str=20 + 'quality': str 'status':, 'official': bool, 'supported': bool, }, - 'id2': {...} + 'ovl2': {...} } """ - =20 + 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[id] =3D ('', False, False) + if local: + db =3D self._get_installed_db() + else: + db =3D self._get_remote_db() + + for ovl in repos: + if not self.is_repo(ovl): + self._error(1, UNKNOWN_REPO_ID %ovl) + result[ovl] =3D ('', False, False) try: - overlay =3D self._available_db.select(id) + overlay =3D db.select(ovl) except UnknownOverlayException, error: self._error(2, "Error: %s" %str(error)) - result[id] =3D ('', False, False) + result[ovl] =3D ('', False, False) else: - result[id] =3D { + result[ovl] =3D { 'name': overlay.name, 'owner_name': overlay.owner_name, 'owner_email': overlay.owner_email, 'homepage': overlay.homepage, 'description': overlay.description, + #'src_uris': [e.src for e in overlay.sources], 'src_uris': overlay.source_uris(), - 'src_type': overlay.sources[0].type, + 'src_types': [e.type for e in overlay.sources], 'priority': overlay.priority, - 'quality': overlay.quality,=20 + 'quality': overlay.quality, 'status': overlay.status, 'official': overlay.is_official(), 'supported': overlay.is_supported(), @@ -227,40 +226,67 @@ class LaymanAPI(object): return result =20 =20 - def get_info_str(self, repos): - """retirves the string representation of the recorded informatio= n - about the repo(s) specified by id - =20 + def get_info_str(self, repos, local=3DTrue, verbose=3DFalse, width=3D= 0): + """retrieves the string representation of the recorded informati= on + about the repo(s) specified by ovl + @type repos: list of strings or string @param repos: ['repo-id1', ...] or 'repo-id' @rtype list of tuples [(str, bool, bool),...] - @return: dictionary {'id': (info string, official, supported)} + @return: dictionary {'repo-id': (info string, official, support= ed)} """ 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[id] =3D ('', False, False) + if local: + db =3D self._get_installed_db() + else: + db =3D self._get_remote_db() + + for ovl in repos: + if not self.is_repo(ovl): + self._error(1, UNKNOWN_REPO_ID % ovl) + result[ovl] =3D ('', False, False) try: - overlay =3D self._available_db.select(id) + overlay =3D db.select(ovl) + #print "overlay =3D ", ovl + #print overlay except UnknownOverlayException, error: + #print "ERRORS", str(error) self._error(2, "Error: %s" %str(error)) - result[id] =3D ('', False, False) + result[ovl] =3D ('', False, False) else: # Is the overlay supported? - info =3D overlay.__str__() + if verbose: + info =3D overlay.__str__() + else: + info =3D overlay.short_list(width) official =3D overlay.is_official() supported =3D overlay.is_supported() - result[id] =3D (info, official, supported) + result[ovl] =3D (info, official, supported) =20 return result =20 + def get_info_list(self, local=3DTrue, verbose=3DFalse, width=3D0): + """retrieves the string representation of the recorded informati= on + about the repo(s) specified by ovl + + @param local: bool (defaults to True) + @param verbose: bool(defaults to False) + @param width: int (defaults to 0) + @rtype list of tuples [(str, bool, bool),...] + @return: list [(info string, official, supported),...] + """ + + if local: + return self._get_installed_db().list(verbose=3Dverbose, widt= h=3Dwidth) + else: + return self._get_remote_db().list(verbose=3Dverbose, width=3D= width) + =20 def sync(self, repos, output_results=3DTrue): """syncs the specified repo(s) specified by repos - =20 + @type repos: list of strings or string @param repos: ['repo-id1', ...] or 'repo-id' @rtype bool or {'repo-id': bool,...} @@ -269,20 +295,21 @@ class LaymanAPI(object): warnings =3D [] success =3D [] repos =3D self._check_repo_type(repos, "sync") + db =3D self._get_installed_db() =20 - for id in repos: + for ovl in repos: try: - odb =3D self._installed_db.select(id) + odb =3D db.select(ovl) except UnknownOverlayException, error: - self._error(1,"Sync(), failed to select %s overlay. Ori= ginal error was: %s" %(id, str(error))) + self._error(1,"Sync(), failed to select %s overlay. Ori= ginal error was: %s" %(ovl, str(error))) continue =20 try: - ordb =3D self._available_db.select(id) + ordb =3D self._get_remote_db().select(ovl) except UnknownOverlayException: 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)) + 'Please check if it has been renamed and re-add = if necessary.' %ovl + warnings.append((ovl, message)) else: current_src =3D odb.sources[0].src available_srcs =3D set(e.src for e in ordb.sources) @@ -292,9 +319,9 @@ class LaymanAPI(object): candidates =3D ' %s' % tuple(available_srcs)[0] else: plural =3D 's' - candidates =3D '\n'.join((' %d. %s' % (id + 1, = v)) for id, v in enumerate(available_srcs)) + candidates =3D '\n'.join((' %d. %s' % (ovl + 1,= v)) for ovl, v in enumerate(available_srcs)) =20 - warnings.append((id, + warnings.append((ovl, 'The source of the overlay "%(repo_name)s" seems= to have changed.\n' 'You currently sync from\n' '\n' @@ -306,34 +333,34 @@ class LaymanAPI(object): '\n' 'as correct location%(plural)s.\n' 'Please consider removing and re-adding the over= lay.' , { - 'repo_name':id, + 'repo_name':ovl, 'current_src':current_src, 'candidates':candidates, 'plural':plural, })) =20 try: - self._installed_db.sync(id, self.config['quiet']) - success.append((id,'Successfully synchronized overlay "'= + id + '".')) + db.sync(ovl, self.config['quiet']) + success.append((ovl,'Successfully synchronized overlay "= ' + ovl + '".')) except Exception, error: - fatals.append((id, - 'Failed to sync overlay "' + id + '".\nError was: ' + fatals.append((ovl, + 'Failed to sync overlay "' + ovl + '".\nError was: ' + str(error))) =20 if output_results: if success: self.output.info('\nSuccess:\n------\n', 3) - for id, result in success: + for ovl, result in success: self.output.info(result, 3) - =20 + if warnings: self.output.warn('\nWarnings:\n------\n', 2) - for id, result in warnings: + for ovl, result in warnings: self.output.warn(result + '\n', 2) =20 if fatals: self.output.error('\nErrors:\n------\n') - for id, result in fatals: + for ovl, result in fatals: self.output.error(result + '\n') return False =20 @@ -345,28 +372,41 @@ class LaymanAPI(object): def fetch_remote_list(self): """Fetches the latest remote overlay list""" try: - self._available_db.cache() + self._get_remote_db().cache() except Exception, error: self._error(-1,'Failed to fetch overlay list!\n Original Err= or was: ' + str(error)) return False + self.get_available(reload=3DTrue) return True =20 =20 def get_available(self, reload=3DFalse): """returns the list of available overlays""" - if not self._available_db or reload: - self._available_db =3D RemoteDB(self.config) - self._available_ids =3D sorted(self._available_db.overlays) - return self._available_ids[:] + if self._available_ids is None or reload: + self._available_ids =3D sorted(self._get_remote_db(reload).o= verlays) + return self._available_ids[:] or ['None'] =20 =20 def get_installed(self, reload=3DFalse): """returns the list of installed overlays""" + if self._installed_ids is None or reload: + self._installed_ids =3D sorted(self._get_installed_db(reload= ).overlays) + return self._installed_ids[:] + + + def _get_installed_db(self, reload=3DFalse): + """returns the list of installed overlays""" if not self._installed_db or reload: self._installed_db =3D DB(self.config) - self._installed_ids =3D sorted(self._installed_db.overlays) - return self._installed_ids[:] + return self._installed_db + + + def _get_remote_db(self, reload=3DFalse): + """returns the list of installed overlays""" + if self._available_db is None or reload: + self._available_db =3D RemoteDB(self.config) + return self._available_db =20 =20 def reload(self): @@ -389,7 +429,7 @@ class LaymanAPI(object): def get_errors(self): """returns any warning or fatal messages that occurred during an operation and resets it back to None - =20 + @rtype: list @return: list of error strings """ @@ -397,6 +437,7 @@ class LaymanAPI(object): messages =3D self._error_messages[:] self._error_messages =3D [] return messages + return [] =20 =20 def create_fd(): @@ -407,5 +448,5 @@ def create_fd(): w =3D os.fdopen(fd_w, 'w') r =3D os.fdopen(fd_r, 'r') return (r, w, fd_r, fd_w) - =20 - =20 + + diff --git a/layman/utils.py b/layman/utils.py index 79b3c18..67a0ab7 100644 --- a/layman/utils.py +++ b/layman/utils.py @@ -47,6 +47,8 @@ def decode_selection(selection): '''utility function to decode a list of strings accoring to the filesystem encoding ''' + # fix None passed in, return an empty list + selection =3D selection or [] enc =3D sys.getfilesystemencoding() if enc: return [i.decode(enc) for i in selection]