util.listir: simplify and optimize
authorZac Medico <zmedico@gentoo.org>
Thu, 27 Jun 2013 21:31:49 +0000 (14:31 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 27 Jun 2013 21:31:49 +0000 (14:31 -0700)
Use zip() to simplify, and fix "list = list + foo" usage to use
list.extend() or equivalent.

pym/portage/util/listdir.py

index 61a025ae79a9ae8beca3a4219635e1a4d15d2258..2012e145f107aca5fec8eceec31b1cb07c78137e 100644 (file)
@@ -5,6 +5,10 @@ __all__ = ['cacheddir', 'listdir']
 
 import errno
 import stat
+import sys
+
+if sys.hexversion < 0x3000000:
+       from itertools import izip as zip
 
 from portage import os
 from portage.const import VCS_DIRS
@@ -21,7 +25,6 @@ dircache = {}
 
 def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymlinks=True):
        mypath = normalize_path(my_original_path)
-       cached_mtime, list, ftype = -1, [], []
        try:
                pathstat = os.stat(mypath)
                if not stat.S_ISDIR(pathstat.st_mode):
@@ -35,14 +38,14 @@ def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymli
                return [], []
        else:
                try:
-                       list = os.listdir(mypath)
+                       fpaths = os.listdir(mypath)
                except EnvironmentError as e:
                        if e.errno != errno.EACCES:
                                raise
                        del e
                        raise PermissionDenied(mypath)
                ftype = []
-               for x in list:
+               for x in fpaths:
                        try:
                                if followSymlinks:
                                        pathstat = os.stat(mypath+"/"+x)
@@ -60,19 +63,20 @@ def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymli
                        except (IOError, OSError):
                                ftype.append(3)
 
-       ret_list = []
-       ret_ftype = []
-       for x in range(0, len(list)):
-               if list[x] in ignorelist:
-                       pass
-               elif ignorecvs:
-                       if list[x][:2] != ".#" and \
-                               not (ftype[x] == 1 and list[x] in VCS_DIRS):
-                               ret_list.append(list[x])
-                               ret_ftype.append(ftype[x])
-               else:
-                       ret_list.append(list[x])
-                       ret_ftype.append(ftype[x])
+       if ignorelist or ignorecvs:
+               ret_list = []
+               ret_ftype = []
+               for file_path, file_type in zip(fpaths, ftype):
+                       if file_path in ignorelist:
+                               pass
+                       elif ignorecvs:
+                               if file_path[:2] != ".#" and \
+                                       not (file_type == 1 and file_path in VCS_DIRS):
+                                       ret_list.append(file_path)
+                                       ret_ftype.append(file_type)
+       else:
+               ret_list = fpaths
+               ret_ftype = ftype
 
        return ret_list, ret_ftype
 
@@ -101,40 +105,35 @@ def listdir(mypath, recursive=False, filesonly=False, ignorecvs=False, ignorelis
        @return: A list of files and directories (or just files or just directories) or an empty list.
        """
 
-       list, ftype = cacheddir(mypath, ignorecvs, ignorelist, EmptyOnError, followSymlinks)
+       fpaths, ftype = cacheddir(mypath, ignorecvs, ignorelist, EmptyOnError, followSymlinks)
 
-       if list is None:
-               list=[]
+       if fpaths is None:
+               fpaths = []
        if ftype is None:
-               ftype=[]
+               ftype = []
 
        if not (filesonly or dirsonly or recursive):
-               return list
+               return fpaths
 
        if recursive:
-               x=0
-               while x<len(ftype):
-                       if ftype[x] == 1:
-                               l,f = cacheddir(mypath+"/"+list[x], ignorecvs, ignorelist, EmptyOnError,
-                                       followSymlinks)
-
-                               l=l[:]
-                               for y in range(0,len(l)):
-                                       l[y]=list[x]+"/"+l[y]
-                               list=list+l
-                               ftype=ftype+f
-                       x+=1
+               stack = list(zip(fpaths, ftype))
+               fpaths = []
+               ftype = []
+               while stack:
+                       file_path, file_type = stack.pop()
+                       fpaths.append(file_path)
+                       ftype.append(file_type)
+                       if file_type == 1:
+                               subdir_list, subdir_types = cacheddir(
+                                       os.path.join(mypath, file_path), ignorecvs,
+                                       ignorelist, EmptyOnError, followSymlinks)
+                               stack.extend((os.path.join(file_path, x), x_type)
+                                       for x, x_type in zip(subdir_list, subdir_types))
+
        if filesonly:
-               rlist=[]
-               for x in range(0,len(ftype)):
-                       if ftype[x]==0:
-                               rlist=rlist+[list[x]]
+               fpaths = [x for x, x_type in zip(fpaths, ftype) if x_type == 0]
+
        elif dirsonly:
-               rlist = []
-               for x in range(0, len(ftype)):
-                       if ftype[x] == 1:
-                               rlist = rlist + [list[x]]       
-       else:
-               rlist=list
+               fpaths = [x for x, x_type in zip(fpaths, ftype) if x_type == 1]
 
-       return rlist
+       return fpaths