Gentoo Archives: gentoo-catalyst

From: Felix Bier <Felix.Bier@×××××××××××××.com>
To: "gentoo-catalyst@l.g.o" <gentoo-catalyst@l.g.o>
Subject: [gentoo-catalyst] [PATCH] Enable recursive globbing for clear_path
Date: Sat, 13 Feb 2021 23:18:26
Message-Id: 45ddcea2d3a7de79e99f7c4eb4b8262c3c52c7b4.camel@rohde-schwarz.com
1 This commit enables recursive globbing in clear_path, allowing the
2 usage of '**' to match an arbitrary number of sub-directories.
3
4 Before this commit, clear_path used only non-recursive globbing. This
5 allowed to use '*' to expand names within one directory, e.g. '/a/*/c'
6 can expand to '/a/b/c', but not '/a/b/b/c'. With this commit, '/a/**/c'
7 can be used to expand to '/a/b/c', '/a/b/b/c', '/a/b/b/b/c' etc.
8
9 This is motivated by wanting to recursively delete all occurences of a
10 filename with the 'stage4/rm' entry of a spec file. The '/rm' entries
11 are processed with 'clear_path' in the existing code.
12
13 Additionally, 'glob.glob' is replaced with 'glob.iglob',
14 which returns the same files as 'glob.glob', but as an iterator
15 instead of as a list (so it no longer necessary to hold
16 all matches in memory at once).
17
18 Recursive globbing has been added in Python 3.5.
19
20 References:
21 https://docs.python.org/3/library/glob.html#glob.glob
22 https://docs.python.org/3/library/glob.html#glob.iglob
23 ---
24 catalyst/fileops.py | 2 +-
25 1 file changed, 1 insertion(+), 1 deletion(-)
26
27 diff --git a/catalyst/fileops.py b/catalyst/fileops.py
28 index 5c6f5cd8..59525420 100644
29 --- a/catalyst/fileops.py
30 +++ b/catalyst/fileops.py
31 @@ -99,7 +99,7 @@ def clear_dir(target, mode=0o755, remove=False,
32
33 def clear_path(target_path):
34 """Nuke |target_path| regardless of it being a dir, file or glob."""
35 - targets = glob.glob(target_path)
36 + targets = glob.iglob(target_path, recursive=True)
37 for path in targets:
38 clear_dir(path, remove=True)
39
40 --
41 2.30.1

Replies