Merge lp:~niedbalski/charm-helpers/sed-support into lp:charm-helpers

Proposed by Jorge Niedbalski
Status: Merged
Merged at revision: 410
Proposed branch: lp:~niedbalski/charm-helpers/sed-support
Merge into: lp:charm-helpers
Diff against target: 86 lines (+77/-0)
2 files modified
charmhelpers/core/files.py (+45/-0)
tests/core/test_files.py (+32/-0)
To merge this branch: bzr merge lp:~niedbalski/charm-helpers/sed-support
Reviewer Review Type Date Requested Status
Felipe Reyes (community) Approve
charmers Pending
Review via email: mp+265532@code.launchpad.net

This proposal supersedes a proposal from 2015-07-15.

Description of the change

Add a 'sed' function for search/replace patterns on files.

To post a comment you must log in.
Revision history for this message
Felipe Reyes (freyes) wrote : Posted in a previous version of this proposal

I had to apply this patch[0] to be able to run the unit tests, it seems to be a recent regression/change according to this[1] stackoverflow question found by @niedbalski.

[0]
=== modified file 'test_requirements.txt'
--- test_requirements.txt 2014-11-25 15:07:02 +0000
+++ test_requirements.txt 2015-07-15 16:57:37 +0000
@@ -3,7 +3,7 @@
 pip
 distribute
 coverage>=3.6
-mock>=1.0.1
+mock>=1.0.1,<1.1.0
 nose>=1.3.1
 flake8
 testtools==0.9.14 # Before dependent on modern 'six'

[1] http://stackoverflow.com/questions/31417964/importerror-cannot-import-name-wraps

Revision history for this message
Felipe Reyes (freyes) wrote : Posted in a previous version of this proposal

> I had to apply this patch[0] to be able to run the unit tests, it seems to be
> a recent regression/change according to this[1] stackoverflow question found
> by @niedbalski.
sent MP https://code.launchpad.net/~freyes/charm-helpers/mock/+merge/264884 to fix it

Revision history for this message
Felipe Reyes (freyes) wrote : Posted in a previous version of this proposal

@niedbalski, I found a few minor issues, besides those lgtm

review: Needs Fixing
Revision history for this message
Felipe Reyes (freyes) wrote :

thanks @niedbalski!, lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'charmhelpers/core/files.py'
--- charmhelpers/core/files.py 1970-01-01 00:00:00 +0000
+++ charmhelpers/core/files.py 2015-07-22 13:55:55 +0000
@@ -0,0 +1,45 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright 2014-2015 Canonical Limited.
5#
6# This file is part of charm-helpers.
7#
8# charm-helpers is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License version 3 as
10# published by the Free Software Foundation.
11#
12# charm-helpers is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
19
20__author__ = 'Jorge Niedbalski <niedbalski@ubuntu.com>'
21
22import os
23import subprocess
24
25
26def sed(filename, before, after, flags='g'):
27 """
28 Search and replaces the given pattern on filename.
29
30 :param filename: relative or absolute file path.
31 :param before: expression to be replaced (see 'man sed')
32 :param after: expression to replace with (see 'man sed')
33 :param flags: sed-compatible regex flags in example, to make
34 the search and replace case insensitive, specify ``flags="i"``.
35 The ``g`` flag is always specified regardless, so you do not
36 need to remember to include it when overriding this parameter.
37 :returns: If the sed command exit code was zero then return,
38 otherwise raise CalledProcessError.
39 """
40 expression = r's/{0}/{1}/{2}'.format(before,
41 after, flags)
42
43 return subprocess.check_call(["sed", "-i", "-r", "-e",
44 expression,
45 os.path.expanduser(filename)])
046
=== added file 'tests/core/test_files.py'
--- tests/core/test_files.py 1970-01-01 00:00:00 +0000
+++ tests/core/test_files.py 2015-07-22 13:55:55 +0000
@@ -0,0 +1,32 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4from charmhelpers.core import files
5
6import mock
7import unittest
8import tempfile
9import os
10
11
12class FileTests(unittest.TestCase):
13
14 @mock.patch("subprocess.check_call")
15 def test_sed(self, check_call):
16 files.sed("/tmp/test-sed-file", "replace", "this")
17 check_call.assert_called_once_with(
18 ['sed', '-i', '-r', '-e', 's/replace/this/g',
19 '/tmp/test-sed-file']
20 )
21
22 def test_sed_file(self):
23 tmp = tempfile.NamedTemporaryFile(delete=False)
24 tmp.write("IPV6=yes")
25 tmp.close()
26
27 files.sed(tmp.name, "IPV6=.*", "IPV6=no")
28
29 with open(tmp.name) as tmp:
30 self.assertEquals(tmp.read(), "IPV6=no")
31
32 os.unlink(tmp.name)

Subscribers

People subscribed via source and target branches