From 6a9b580132b6e82c9505a9f0f8b855a3b8d282ff Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Thu, 25 Feb 2010 05:15:09 +0000 Subject: Move cacheddir and listdir to portage.util.listdir. svn path=/main/trunk/; revision=15450 --- pym/portage/__init__.py | 140 +------------------------------------- pym/portage/dbapi/bintree.py | 3 +- pym/portage/dbapi/porttree.py | 8 ++- pym/portage/dbapi/vartree.py | 13 ++-- pym/portage/util/digraph.py | 2 + pym/portage/util/listdir.py | 153 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 170 insertions(+), 149 deletions(-) create mode 100644 pym/portage/util/listdir.py (limited to 'pym') diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index fa26e7f75..34a71278a 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -122,6 +122,7 @@ try: 'stack_lists,unique_array,varexpand,writedict,writemsg,' + \ 'writemsg_stdout,write_atomic', 'portage.util.digraph:digraph', + 'portage.util.listdir:cacheddir,listdir', 'portage.versions', 'portage.versions:best,catpkgsplit,catsplit,cpv_getkey,' + \ 'cpv_getkey@getCPFromCPV,endversion_keys,' + \ @@ -533,145 +534,6 @@ def abssymlink(symlink): mylink=mydir+"/"+mylink return os.path.normpath(mylink) -dircache = {} -cacheHit=0 -cacheMiss=0 -cacheStale=0 -def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymlinks=True): - global cacheHit,cacheMiss,cacheStale - mypath = normalize_path(my_original_path) - if mypath in dircache: - cacheHit += 1 - cached_mtime, list, ftype = dircache[mypath] - else: - cacheMiss += 1 - cached_mtime, list, ftype = -1, [], [] - try: - pathstat = os.stat(mypath) - if stat.S_ISDIR(pathstat[stat.ST_MODE]): - mtime = pathstat.st_mtime - else: - raise portage.exception.DirectoryNotFound(mypath) - except EnvironmentError as e: - if e.errno == portage.exception.PermissionDenied.errno: - raise portage.exception.PermissionDenied(mypath) - del e - return [], [] - except portage.exception.PortageException: - return [], [] - # Python retuns mtime in seconds, so if it was changed in the last few seconds, it could be invalid - if mtime != cached_mtime or time.time() - mtime < 4: - if mypath in dircache: - cacheStale += 1 - try: - list = os.listdir(mypath) - except EnvironmentError as e: - if e.errno != errno.EACCES: - raise - del e - raise portage.exception.PermissionDenied(mypath) - ftype = [] - for x in list: - try: - if followSymlinks: - pathstat = os.stat(mypath+"/"+x) - else: - pathstat = os.lstat(mypath+"/"+x) - - if stat.S_ISREG(pathstat[stat.ST_MODE]): - ftype.append(0) - elif stat.S_ISDIR(pathstat[stat.ST_MODE]): - ftype.append(1) - elif stat.S_ISLNK(pathstat[stat.ST_MODE]): - ftype.append(2) - else: - ftype.append(3) - except (IOError, OSError): - ftype.append(3) - dircache[mypath] = mtime, list, ftype - - ret_list = [] - ret_ftype = [] - for x in range(0, len(list)): - if list[x] in ignorelist: - pass - elif ignorecvs: - if list[x][:2] != ".#": - ret_list.append(list[x]) - ret_ftype.append(ftype[x]) - else: - ret_list.append(list[x]) - ret_ftype.append(ftype[x]) - - writemsg("cacheddirStats: H:%d/M:%d/S:%d\n" % (cacheHit, cacheMiss, cacheStale),10) - return ret_list, ret_ftype - -_ignorecvs_dirs = ('CVS', 'SCCS', '.svn', '.git') - -def listdir(mypath, recursive=False, filesonly=False, ignorecvs=False, ignorelist=[], followSymlinks=True, - EmptyOnError=False, dirsonly=False): - """ - Portage-specific implementation of os.listdir - - @param mypath: Path whose contents you wish to list - @type mypath: String - @param recursive: Recursively scan directories contained within mypath - @type recursive: Boolean - @param filesonly; Only return files, not more directories - @type filesonly: Boolean - @param ignorecvs: Ignore CVS directories ('CVS','SCCS','.svn','.git') - @type ignorecvs: Boolean - @param ignorelist: List of filenames/directories to exclude - @type ignorelist: List - @param followSymlinks: Follow Symlink'd files and directories - @type followSymlinks: Boolean - @param EmptyOnError: Return [] if an error occurs (deprecated, always True) - @type EmptyOnError: Boolean - @param dirsonly: Only return directories. - @type dirsonly: Boolean - @rtype: List - @returns: A list of files and directories (or just files or just directories) or an empty list. - """ - - list, ftype = cacheddir(mypath, ignorecvs, ignorelist, EmptyOnError, followSymlinks) - - if list is None: - list=[] - if ftype is None: - ftype=[] - - if not (filesonly or dirsonly or recursive): - return list - - if recursive: - x=0 - while x