Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 5/5] eutils.eclass: Restore the original path_exists function
Date: Thu, 09 Aug 2018 02:20:52
Message-Id: 20180808213418.28823-5-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 1/5] media-plugins/vdr-live: Replace unnecessary path_exists calls by "Michał Górny"
1 The original path_exists function as submitted by me was a trivial
2 function to facilitate for file existence checks with wildcards.
3 However, its purpose was defeated by meaningless featurism.
4 As a result, half of the current uses was entirely mistaken (it's *not*
5 a replacement for trivial -e/-f tests!) and the other half needlessly
6 specified a meaningless -a/-o option which did not affect the result.
7
8 After fixing all the (few) uses, remove the complexity and restore
9 the original API. This also fixes its behavior when it happens to match
10 a multiple of 256 files.
11
12 As a fun fact, the new 'or' default behavior would also keep the old
13 uses working (since the option would be ignored as first unmatched file
14 and the function would proceed with the next parameter).
15
16 Alternatively, we could remove the function entirely and inline its use
17 in the three packages that need it.
18 ---
19 eclass/eutils.eclass | 41 ++++++++++++++----------------
20 eclass/tests/eutils_path_exists.sh | 35 -------------------------
21 2 files changed, 19 insertions(+), 57 deletions(-)
22 delete mode 100755 eclass/tests/eutils_path_exists.sh
23
24 diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass
25 index 9b4767e1874a..98a1bc7bf56e 100644
26 --- a/eclass/eutils.eclass
27 +++ b/eclass/eutils.eclass
28 @@ -182,32 +182,29 @@ make_wrapper() {
29 }
30
31 # @FUNCTION: path_exists
32 -# @USAGE: [-a|-o] <paths>
33 +# @USAGE: <paths>
34 # @DESCRIPTION:
35 -# Check if the specified paths exist. Works for all types of paths
36 -# (files/dirs/etc...). The -a and -o flags control the requirements
37 -# of the paths. They correspond to "and" and "or" logic. So the -a
38 -# flag means all the paths must exist while the -o flag means at least
39 -# one of the paths must exist. The default behavior is "and". If no
40 -# paths are specified, then the return value is "false".
41 +# Check if the specified paths exist. This is intended to be used
42 +# with filename expansion when the built-in bash tests can't work
43 +# and not as a generic replacement.
44 +#
45 +# Returns true if at least one path exists (matched the pattern).
46 +# Returns false otherwise (pattern did not match and was passed
47 +# verbatim or expanded into an empty list).
48 +#
49 +# Example:
50 +# @CODE
51 +# if path_exists "${ROOT}"/etc/foo.d/*.conf; then
52 +# do_something
53 +# fi
54 +# @CODE
55 path_exists() {
56 - local opt=$1
57 - [[ ${opt} == -[ao] ]] && shift || opt="-a"
58 -
59 - # no paths -> return false
60 - # same behavior as: [[ -e "" ]]
61 - [[ $# -eq 0 ]] && return 1
62 -
63 - local p r=0
64 - for p in "$@" ; do
65 - [[ -e ${p} ]]
66 - : $(( r += $? ))
67 + local p
68 + for p; do
69 + [[ -e ${p} ]] && return 0
70 done
71
72 - case ${opt} in
73 - -a) return $(( r != 0 )) ;;
74 - -o) return $(( r == $# )) ;;
75 - esac
76 + return 1
77 }
78
79 # @FUNCTION: use_if_iuse
80 diff --git a/eclass/tests/eutils_path_exists.sh b/eclass/tests/eutils_path_exists.sh
81 deleted file mode 100755
82 index 00a89c7e446d..000000000000
83 --- a/eclass/tests/eutils_path_exists.sh
84 +++ /dev/null
85 @@ -1,35 +0,0 @@
86 -#!/bin/bash
87 -# Copyright 1999-2015 Gentoo Foundation
88 -# Distributed under the terms of the GNU General Public License v2
89 -
90 -source tests-common.sh
91 -
92 -inherit eutils
93 -
94 -test-path_exists() {
95 - local exp=$1; shift
96 - tbegin "path_exists($*) == ${exp}"
97 - path_exists "$@"
98 - [[ ${exp} -eq $? ]]
99 - tend $?
100 -}
101 -
102 -test-path_exists 1
103 -test-path_exists 1 -a
104 -test-path_exists 1 -o
105 -
106 -good="/ . tests-common.sh /bin/bash"
107 -test-path_exists 0 ${good}
108 -test-path_exists 0 -a ${good}
109 -test-path_exists 0 -o ${good}
110 -
111 -bad="/asjdkfljasdlfkja jlakjdsflkasjdflkasdjflkasdjflaskdjf"
112 -test-path_exists 1 ${bad}
113 -test-path_exists 1 -a ${bad}
114 -test-path_exists 1 -o ${bad}
115 -
116 -test-path_exists 1 ${good} ${bad}
117 -test-path_exists 1 -a ${good} ${bad}
118 -test-path_exists 0 -o ${good} ${bad}
119 -
120 -texit
121 --
122 2.18.0

Replies