From patchwork Sun Feb 1 09:57:53 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Whitcroft X-Patchwork-Id: 435215 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 70A8B14029C; Sun, 1 Feb 2015 20:58:14 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1YHrI9-0001at-34; Sun, 01 Feb 2015 09:58:09 +0000 Received: from mail-wi0-f171.google.com ([209.85.212.171]) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1YHrI2-0001aJ-2j for kernel-team@lists.ubuntu.com; Sun, 01 Feb 2015 09:58:02 +0000 Received: by mail-wi0-f171.google.com with SMTP id l15so10971386wiw.4 for ; Sun, 01 Feb 2015 01:58:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GlpUhslTJ4PDn704McmyoGvzlLLGb5tPph0ZXONAJmw=; b=XBVVJ6vfwJYo4CxcAsnGA6qly/b635fm+8hudq6osUYvvrlBSlgD8T7Yuqz+Yujc1L 6o1pvm2AZa1NwmlS4aU50K8GO6Wxif7Wpzw8UV7G5ywltir5lUan7I07FI28ay9odd/s /gOJqX2Fo9TTWRie4dlvORl2pP+1QXLih3arzE6OKhHBaaI/hLu38rGzeF2MmEr5WiNf ZrwJffRf4htTGKQZjsjo3SIvAoEJRBe8/hMvMx43HjhnQhfJStAg/se0tQ3QO8/7EQrG Y/L1Dpydat4MiCJTHnTSj4wonZ91z9gf+Y4AC4UIBTNkv88X39HhPtttJGemXyphB2WJ dG+w== X-Gm-Message-State: ALoCoQlsshPptz2KrnWEqyjAF5v8GNfJ8Mzh9KTipzMV7QLylz0t2wl0opWnm/gedI0Jib8VUNt2 X-Received: by 10.181.13.206 with SMTP id fa14mr13053086wid.57.1422784681956; Sun, 01 Feb 2015 01:58:01 -0800 (PST) Received: from localhost ([41.193.54.210]) by mx.google.com with ESMTPSA id u17sm14688649wij.2.2015.02.01.01.58.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Feb 2015 01:58:01 -0800 (PST) From: Andy Whitcroft To: kernel-team@lists.ubuntu.com Subject: [trusty/utopic 1/1] UBUNTU: hyper-v -- fix comment handing in /etc/network/interfaces Date: Sun, 1 Feb 2015 11:57:53 +0200 Message-Id: <1422784673-14136-2-git-send-email-apw@canonical.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1422784673-14136-1-git-send-email-apw@canonical.com> References: <1422784673-14136-1-git-send-email-apw@canonical.com> Cc: Andy Whitcroft X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: kernel-team-bounces@lists.ubuntu.com We are duplicating the opening comment marker every time we rebuild the file, such that we end up with multiple of those comments: # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface # The following stanza(s) added by hv_set_ifconfig # The following stanza(s) added by hv_set_ifconfig # The following stanza(s) added by hv_set_ifconfig auto eth0 iface eth0 inet static address 10.100.20.108 gateway 10.100.20.1 dns-nameservers 8.8.4.4 #End of hv_set_ifconfig stanzas Fix handling of these such that we only insert new markers if they do not already exist. Where they do, simply inject the new stanzas at the end of the block before the end marker. At the same time deduplicate sequential begin and end markers to clean up previously dammaged files. BugLink: http://bugs.launchpad.net/bugs/1413020 Signed-off-by: Andy Whitcroft --- debian/cloud-tools/hv_set_ifconfig | 40 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/debian/cloud-tools/hv_set_ifconfig b/debian/cloud-tools/hv_set_ifconfig index b351776..783a150 100755 --- a/debian/cloud-tools/hv_set_ifconfig +++ b/debian/cloud-tools/hv_set_ifconfig @@ -127,8 +127,6 @@ else: if6_count += 1 output = ["auto "+" ".join(autolist)] + output -output=["# The following stanza(s) added by hv_set_ifconfig"] + output -output+=["#End of hv_set_ifconfig stanzas"] print "===================================" print output print "===================================" @@ -136,6 +134,10 @@ print "===================================" ''' Time to clean out the existing interface file''' +# Markers. +start_mark = "# The following stanza(s) added by hv_set_ifconfig" +end_mark = "#End of hv_set_ifconfig stanzas" + f=open(if_filename,"r") flines=f.readlines() f.close() @@ -143,6 +145,7 @@ newfile=[] pitchstanza=0 inastanza=0 stanza=[] +prev_line=None for line in flines: if line.startswith("auto"): if inastanza: @@ -173,6 +176,16 @@ for line in flines: pitchstanza=1 if not pitchstanza: stanza+=[line.strip()] + elif line.strip() in (start_mark, end_mark): + if inastanza: + if not pitchstanza: + newfile.extend(stanza) + stanza=[] + inastanza = 0 + pitchstanza = 0 + # Deduplicate markers. + if line != prev_line: + newfile += [line.strip()] else: if inastanza: if not pitchstanza: @@ -180,21 +193,26 @@ for line in flines: else: if not pitchstanza: newfile += [line.strip()] + prev_line=line -for line in newfile: - print line -for line in output: - print line +def emit(line): + print(line) + os.write(fd, line + "\n") +# Insert the new output at the end and inside the existing markers if found. +emitted = False fd, path = tempfile.mkstemp() for line in newfile: - os.write(fd,line) - os.write(fd,"\n") -for line in output: - os.write(fd,line) - os.write(fd,"\n") + if line == end_mark: + emit("\n".join(output)) + emitted = True + emit(line) +if not emitted: + emit(start_mark) + emit("\n".join(output)) + emit(end_mark) os.close(fd) shutil.copy(path,if_filename)