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 68982138262 for ; Sat, 21 May 2016 13:49:57 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id AF8F0234022; Sat, 21 May 2016 13:49:48 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id D546623401A for ; Sat, 21 May 2016 13:49:47 +0000 (UTC) Received: from localhost (cpc92302-cmbg19-2-0-cust189.5-4.cable.virginm.net [82.1.208.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: aidecoe) by smtp.gentoo.org (Postfix) with ESMTPSA id 1B3AA340CD2; Sat, 21 May 2016 13:49:45 +0000 (UTC) From: aidecoe@gentoo.org To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Amadeusz=20=C5=BBo=C5=82nowski?= Subject: [gentoo-dev] [PATCH] eutils.eclass: Add awk wrapper - eawk - edit file in place Date: Sat, 21 May 2016 14:49:41 +0100 Message-Id: <1463838581-3973-1-git-send-email-aidecoe@gentoo.org> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1463834754-31730-1-git-send-email-aidecoe@gentoo.org> References: <1463834754-31730-1-git-send-email-aidecoe@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 152993e9-6315-43ac-8a8a-6d2a5d4e90aa X-Archives-Hash: 13363305a648332160df90879fdc49a6 From: Amadeusz Żołnowski awk doesn't have the -i option like sed and if editing file in place is desired, additional steps are required. eawk uses tmp file to make it look to the caller editing happens in place. New version of gawk (not stabilized yet) does support editing in place but forcing user to install specific awk implementation is not desired. --- eclass/eutils.eclass | 16 +++++++++++ eclass/tests/eutils_eawk.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100755 eclass/tests/eutils_eawk.sh diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass index dbedffe..963a692 100644 --- a/eclass/eutils.eclass +++ b/eclass/eutils.eclass @@ -20,6 +20,22 @@ _EUTILS_ECLASS=1 inherit multilib toolchain-funcs +# @FUNCTION: eawk +# @USAGE: +# @DESCRIPTION: +# Edit file in place with awk. Pass all arguments following to +# awk. +eawk() { + local f="$1"; shift + local tmpf="$(emktemp)" + + awk "$@" "${f}" >"${tmpf}" || die -n 'awk failed' || return + # Following commands should always succeed unless something weird is going + # on. + cat "${tmpf}" >"${f}" || die 'failed to replace source file' || return + rm "${tmpf}" || die "failed to remove temporary file" +} + # @FUNCTION: eqawarn # @USAGE: [message] # @DESCRIPTION: diff --git a/eclass/tests/eutils_eawk.sh b/eclass/tests/eutils_eawk.sh new file mode 100755 index 0000000..7e0d1d4 --- /dev/null +++ b/eclass/tests/eutils_eawk.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# Copyright 1999-2016 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +source tests-common.sh + +inherit eutils + +# Mock die so it doesn't break tests. +die() { + echo "die: $*" 1>&2 + return 1 +} + +tbegin "preserves permissions" + +cd "${tmpdir}" || tend $? + +cat <'test.txt' +testme1 +testme2 +testme3 +EOF + +cat <'test_expected.txt' +testme1 +foo +testme3 +EOF + +chmod 704 'test.txt' || tend $? +eumask_push 000 +eawk 'test.txt' '/^testme2$/ {print "foo"; next;} 1' || tend $? +eumask_pop + +diff 'test.txt' 'test_expected.txt' +expected=$? + +[[ $(stat -c '%a' 'test.txt') = 704 ]] +perms=$? + +[[ ${expected}${perms} = 00 ]] + +tend $? + + +tbegin "doesn't alter file on failure" + +cd "${tmpdir}" || tend $? + +cat <'test.txt' +testme1 +testme2 +testme3 +EOF + +cat 'test.txt' >'test_expected.txt' + +# eawk should file because of syntax error. +eawk 'test.txt' '/^testme2$/ print "foo"; next;} 1' 2>/dev/null && tend 1 +diff 'test.txt' 'test_expected.txt' + +tend $? + +texit -- 2.8.2