Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: aidecoe@g.o
Cc: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH] eutils.eclass: Add awk wrapper - eawk - edit file in place
Date: Sat, 21 May 2016 20:39:05
Message-Id: 20160521223847.0783fb4d.mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH] eutils.eclass: Add awk wrapper - eawk - edit file in place by aidecoe@gentoo.org
1 On Sat, 21 May 2016 14:49:41 +0100
2 aidecoe@g.o wrote:
3
4 > From: Amadeusz Żołnowski <aidecoe@g.o>
5 >
6 > awk doesn't have the -i option like sed and if editing file in place is
7 > desired, additional steps are required. eawk uses tmp file to make it
8 > look to the caller editing happens in place.
9 >
10 > New version of gawk (not stabilized yet) does support editing in place
11 > but forcing user to install specific awk implementation is not desired.
12 > ---
13 > eclass/eutils.eclass | 16 +++++++++++
14 > eclass/tests/eutils_eawk.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++
15 > 2 files changed, 82 insertions(+)
16 > create mode 100755 eclass/tests/eutils_eawk.sh
17 >
18 > diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass
19 > index dbedffe..963a692 100644
20 > --- a/eclass/eutils.eclass
21 > +++ b/eclass/eutils.eclass
22 > @@ -20,6 +20,22 @@ _EUTILS_ECLASS=1
23 >
24 > inherit multilib toolchain-funcs
25 >
26 > +# @FUNCTION: eawk
27 > +# @USAGE: <file> <args>
28 > +# @DESCRIPTION:
29 > +# Edit file <file> in place with awk. Pass all arguments following <file> to
30 > +# awk.
31 > +eawk() {
32 > + local f="$1"; shift
33 > + local tmpf="$(emktemp)"
34 > +
35 > + awk "$@" "${f}" >"${tmpf}" || die -n 'awk failed' || return
36
37 You can't use 'die -n' before EAPI 6. So you either need a conditional
38 there, or make the function available only in EAPI 6.
39
40 > + # Following commands should always succeed unless something weird is going
41 > + # on.
42 > + cat "${tmpf}" >"${f}" || die 'failed to replace source file' || return
43 > + rm "${tmpf}" || die "failed to remove temporary file"
44
45 Any reason not to use mv here? Aside to making this simpler, it would
46 also prevent this from rewriting contents of hardlinked files.
47
48 > +}
49 > +
50 > # @FUNCTION: eqawarn
51 > # @USAGE: [message]
52 > # @DESCRIPTION:
53 > diff --git a/eclass/tests/eutils_eawk.sh b/eclass/tests/eutils_eawk.sh
54 > new file mode 100755
55 > index 0000000..7e0d1d4
56 > --- /dev/null
57 > +++ b/eclass/tests/eutils_eawk.sh
58 > @@ -0,0 +1,66 @@
59 > +#!/bin/bash
60 > +# Copyright 1999-2016 Gentoo Foundation
61 > +# Distributed under the terms of the GNU General Public License v2
62 > +# $Id$
63 > +
64 > +source tests-common.sh
65 > +
66 > +inherit eutils
67 > +
68 > +# Mock die so it doesn't break tests.
69 > +die() {
70 > + echo "die: $*" 1>&2
71 > + return 1
72 > +}
73 > +
74 > +tbegin "preserves permissions"
75 > +
76 > +cd "${tmpdir}" || tend $?
77
78 This doesn't terminate tests. So you'll end up creating files
79 in the wrong directory.
80
81 > +
82 > +cat <<EOF >'test.txt'
83
84 Then, you aren't checking for failure here.
85
86 > +testme1
87 > +testme2
88 > +testme3
89 > +EOF
90 > +
91 > +cat <<EOF >'test_expected.txt'
92 > +testme1
93 > +foo
94 > +testme3
95 > +EOF
96 > +
97 > +chmod 704 'test.txt' || tend $?
98 > +eumask_push 000
99 > +eawk 'test.txt' '/^testme2$/ {print "foo"; next;} 1' || tend $?
100 > +eumask_pop
101 > +
102 > +diff 'test.txt' 'test_expected.txt'
103 > +expected=$?
104 > +
105 > +[[ $(stat -c '%a' 'test.txt') = 704 ]]
106 > +perms=$?
107 > +
108 > +[[ ${expected}${perms} = 00 ]]
109 > +
110 > +tend $?
111 > +
112 > +
113 > +tbegin "doesn't alter file on failure"
114 > +
115 > +cd "${tmpdir}" || tend $?
116 > +
117 > +cat <<EOF >'test.txt'
118 > +testme1
119 > +testme2
120 > +testme3
121 > +EOF
122 > +
123 > +cat 'test.txt' >'test_expected.txt'
124 > +
125 > +# eawk should file because of syntax error.
126 > +eawk 'test.txt' '/^testme2$/ print "foo"; next;} 1' 2>/dev/null && tend 1
127 > +diff 'test.txt' 'test_expected.txt'
128 > +
129 > +tend $?
130 > +
131 > +texit
132
133
134
135 --
136 Best regards,
137 Michał Górny
138 <http://dev.gentoo.org/~mgorny/>

Replies