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

Subscribers

People subscribed via source and target branches