Gentoo Archives: gentoo-dev

From: Christopher Schwan <cschwan@××××××××××××××××××.de>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] RFC: sed script redundancy
Date: Sun, 29 May 2011 10:45:47
Message-Id: 201105291244.46610.cschwan@students.uni-mainz.de
In Reply to: Re: [gentoo-dev] RFC: sed script redundancy by Fabian Groffen
1 Thank you for that script. I experimented a bit with it and have a number of
2 corrections and suggestions:
3
4 - alias does not work because my_sed is not declared at this stage. I removed
5 the whole alias line because I want to selectively enable my_sed
6 - oargs must be an array in order to make quoting work:
7
8 local oargs=( "${@}" )
9
10 - In the ewarn line ${oargs} should be changed to ${nargs[@]} (!?)
11 - is it correct to treat -e and -f alike ? I am not sure about that, because
12 the latter expects a file
13 - If no "-e" is given, the first non-option argument is treated as the sed-
14 script-expression, therefore I added hade=yes in the if-branch
15
16 The new function now reads:
17
18 my_sed() {
19 local oargs=( "$@" )
20 local arg
21 local nargs=()
22 local hadi=
23 local hade=
24
25 while [[ -n $1 ]] ; do
26 case "$1" in
27 -i|--in-place)
28 # ignore this flag
29 hadi=yes
30 ;;
31 -e|--expression)
32 shift
33 nargs+=( "-e" "$1" )
34 hade=yes
35 ;;
36 -f|--file)
37 shift
38 nargs+=( "-f" "$1" )
39 hade=yes
40 ;;
41 -*)
42 nargs+=( "$1" )
43 ;;
44 *)
45 if [[ -z ${hade} ]] ; then
46 nargs+=( "-e" "$1" )
47 hade=yes
48 elif [[ -z ${hadi} ]] ; then
49 # there is no inline replacing, not much we can do
50 break
51 else
52 sed "${nargs[@]}" "$1" | diff -q "$1" - > /dev/null && \
53 ewarn "sed ${nargs[@]} has no effect on $1"
54 fi
55 ;;
56 esac
57 shift
58 done
59
60 sed "${oargs[@]}"
61 }
62
63 As you can see, I added support for long-options. However, testing the
64 individual sed commands remains to be done. This could be especially difficult
65 if input is taken from stdin (e.g. in cat foo | sed "s:a:b:g").
66
67 I tested my_sed within our sage ebuild[1]. This ebuild contains 39 sed
68 commands and I was able to spot one useless sed.
69
70
71
72 [1] https://github.com/cschwan/sage-on-gentoo/blob/master/sci-
73 mathematics/sage/sage-4.7.ebuild
74
75
76 On Sunday 22 May 2011 12:50:43 Fabian Groffen wrote:
77 > On 21-05-2011 19:34:34 +0200, Jeroen Roovers wrote:
78 > > On Fri, 20 May 2011 17:56:00 +0200
79 > >
80 > > Fabian Groffen <grobian@g.o> wrote:
81 > > > sed -e "<pattern>" "${file}" | diff "${file}" -
82 > > >
83 > > > followed by the actual sed -i -e ...
84 > > >
85 > > > This way I didn't need to write an intermediate file.
86 > >
87 > > The problem there is that sed might be called just once on any one file,
88 > > but in the tree it is often invoked with multiple scripts, so this
89 > > simple implementation lacks a way to evaluate which sed scripts are
90 > > useful.
91 > >
92 > > Also, how do I ensure the sed replacement works only on invocations
93 > > inside the ebuild, and not, say, in portage's internals?
94 >
95 > (not tested, but as proof of concept)
96 >
97 > alias sed my_sed
98 > my_sed() {
99 > local oargs="${@}"
100 > local arg
101 > local nargs=()
102 > local hadi=
103 > local hade=
104 > while [[ -n $1 ]] ; do
105 > case "$1" in
106 > -i)
107 > # ignore this flag
108 > hadi=yes
109 > ;;
110 > -e|-f)
111 > shift
112 > nargs+=( "-e$1" )
113 > hade=yes
114 > ;;
115 > -*)
116 > nargs+=( "$1" )
117 > hade=yes
118 > ;;
119 > *)
120 > if [[ -z ${hade} ]] ; then
121 > nargs+=( "$1" )
122 > elif [[ -z ${hadi} ]] ; then
123 > # there is no inline replacing, not much we can do
124 > break
125 > else
126 > sed "${nargs[@]}" "$1" | diff -q "$1" - > /dev/null \
127 > && ewarn "sed ${oargs} has no effect on $1"
128 > fi
129 > ;;
130 > esac
131 > shift
132 > done
133 >
134 > \sed "${oargs}"
135 > }

Replies

Subject Author
Re: [gentoo-dev] RFC: sed script redundancy Fabian Groffen <grobian@g.o>