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 CE18B138262 for ; Sat, 21 May 2016 12:46:08 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id EE4F3234005; Sat, 21 May 2016 12:46:00 +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 EDFBD21C012 for ; Sat, 21 May 2016 12:45:59 +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 ACEF4340AC7; Sat, 21 May 2016 12:45:58 +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 13:45:54 +0100 Message-Id: <1463834754-31730-1-git-send-email-aidecoe@gentoo.org> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1463606702-16578-1-git-send-email-aidecoe@gentoo.org> References: <1463606702-16578-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: 72d81920-6a73-40a4-8c14-f7703a2f3d7f X-Archives-Hash: 7c61abf4b31528f73d1323578f7589c5 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 | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 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..b06f377 --- /dev/null +++ b/eclass/tests/eutils_eawk.sh @@ -0,0 +1,65 @@ +#!/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 "$(emktemp -d)" + +cat <'test.txt' +testme1 +testme2 +testme3 +EOF + +cat <'test_expected.txt' +testme1 +foo +testme3 +EOF + +chmod 704 'test.txt' +eumask_push 000 +eawk 'test.txt' '/^testme2$/ {print "foo"; next;} 1' +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 "$(emktemp -d)" + +cat <'test.txt' +testme1 +testme2 +testme3 +EOF + +cat 'test.txt' >'test_expected.txt' + +eawk 'test.txt' '/^testme2$/ print "foo"; next;} 1' 2>/dev/null +diff 'test.txt' 'test_expected.txt' + +tend $? + +texit -- 2.8.2