From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 356381381F3 for ; Fri, 28 Jun 2013 01:46:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 03B50E0964; Fri, 28 Jun 2013 01:46:05 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 7D432E0964 for ; Fri, 28 Jun 2013 01:46:04 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6E6C733E721 for ; Fri, 28 Jun 2013 01:46:03 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 0B6F1E468F for ; Fri, 28 Jun 2013 01:46:02 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1372383942.23145c9f13947eeed17ae25ea80b8a667b0a3f5f.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/util/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/util/__init__.py X-VCS-Directories: pym/portage/util/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 23145c9f13947eeed17ae25ea80b8a667b0a3f5f X-VCS-Branch: master Date: Fri, 28 Jun 2013 01:46:02 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: c1da1041-d3ae-4092-abf7-44cfd695938c X-Archives-Hash: 2f7dec4143daa3892dd40f78f2a2d0f6 commit: 23145c9f13947eeed17ae25ea80b8a667b0a3f5f Author: Zac Medico gentoo org> AuthorDate: Fri Jun 28 01:45:42 2013 +0000 Commit: Zac Medico gentoo org> CommitDate: Fri Jun 28 01:45:42 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=23145c9f getconfig: share recursion code with grablines --- pym/portage/util/__init__.py | 111 +++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index f4d5395..546262e 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -488,25 +488,51 @@ def grabfile_package(myfilename, compatlevel=0, recursive=0, allow_wildcard=Fals def _recursive_basename_filter(f): return not f.startswith(".") and not f.endswith("~") -def grablines(myfilename, recursive=0, remember_source_file=False): - mylines=[] - if recursive and os.path.isdir(myfilename): - if os.path.basename(myfilename) in VCS_DIRS: - return mylines +def _recursive_file_list(path): + # path may be a regular file or a directory + + def onerror(e): + if e.errno == PermissionDenied.errno: + raise PermissionDenied(path) + + stack = [os.path.split(path)] + + while stack: + parent, fname = stack.pop() + fullpath = os.path.join(parent, fname) + try: - dirlist = os.listdir(myfilename) + st = os.stat(fullpath) except OSError as e: - if e.errno == PermissionDenied.errno: - raise PermissionDenied(myfilename) - elif e.errno in (errno.ENOENT, errno.ESTALE): - return mylines - else: - raise - dirlist.sort() - for f in dirlist: - if _recursive_basename_filter(f): - mylines.extend(grablines( - os.path.join(myfilename, f), recursive, remember_source_file)) + onerror(e) + continue + + if stat.S_ISDIR(st.st_mode): + if fname in VCS_DIRS or not _recursive_basename_filter(fname): + continue + try: + children = os.listdir(fullpath) + except OSError as e: + onerror(e) + continue + + # Sort in reverse, since we pop from the end of the stack. + # Include regular files in the stack, so files are sorted + # together with directories. + children.sort(reverse=True) + stack.extend((fullpath, x) for x in children) + + elif stat.S_ISREG(st.st_mode): + if _recursive_basename_filter(fname): + yield fullpath + +def grablines(myfilename, recursive=0, remember_source_file=False): + mylines=[] + if recursive: + for f in _recursive_file_list(myfilename): + mylines.extend(grablines(f, recursive=False, + remember_source_file=remember_source_file)) + else: try: myfile = io.open(_unicode_encode(myfilename, @@ -585,18 +611,6 @@ _invalid_var_name_re = re.compile(r'^\d|\W') def getconfig(mycfg, tolerant=False, allow_sourcing=False, expand=True, recursive=False): - is_dir = False - if recursive: - try: - is_dir = stat.S_ISDIR(os.stat(mycfg).st_mode) - except OSError as e: - if e.errno == PermissionDenied.errno: - raise PermissionDenied(mycfg) - elif e.errno in (errno.ENOENT, errno.ESTALE, errno.EISDIR): - return None - else: - raise - if isinstance(expand, dict): # Some existing variable definitions have been # passed in, for use in substitutions. @@ -606,47 +620,18 @@ def getconfig(mycfg, tolerant=False, allow_sourcing=False, expand=True, expand_map = {} mykeys = {} - if recursive and is_dir: + if recursive: # Emulate source commands so that syntax error messages # can display real file names and line numbers. - def onerror(e): - if e.errno == PermissionDenied.errno: - raise PermissionDenied(mycfg) - - recursive_files = [] - for parent, dirs, files in os.walk(mycfg, onerror=onerror): - try: - parent = _unicode_decode(parent, - encoding=_encodings['fs'], errors='strict') - except UnicodeDecodeError: - continue - for fname_enc in dirs[:]: - try: - fname = _unicode_decode(fname_enc, - encoding=_encodings['fs'], errors='strict') - except UnicodeDecodeError: - dirs.remove(fname_enc) - continue - if fname in VCS_DIRS or not _recursive_basename_filter(fname): - dirs.remove(fname_enc) - for fname in files: - try: - fname = _unicode_decode(fname, - encoding=_encodings['fs'], errors='strict') - except UnicodeDecodeError: - pass - else: - if _recursive_basename_filter(fname): - fname = os.path.join(parent, fname) - if os.path.isfile(fname): - recursive_files.append(fname) - recursive_files.sort() if not expand: expand_map = False - for fname in recursive_files: + fname = None + for fname in _recursive_file_list(mycfg): mykeys.update(getconfig(fname, tolerant=tolerant, allow_sourcing=allow_sourcing, expand=expand_map, recursive=False) or {}) + if fname is None: + return None return mykeys f = None