2
# -*- coding: utf-8 -*-
4
# Copyright 2014-2015 Canonical Limited.
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
18
__author__ = 'Jorge Niedbalski <niedbalski@ubuntu.com>'
24
def sed(filename, before, after, flags='g'):
26
Search and replaces the given pattern on filename.
28
:param filename: relative or absolute file path.
29
:param before: expression to be replaced (see 'man sed')
30
:param after: expression to replace with (see 'man sed')
31
:param flags: sed-compatible regex flags in example, to make
32
the search and replace case insensitive, specify ``flags="i"``.
33
The ``g`` flag is always specified regardless, so you do not
34
need to remember to include it when overriding this parameter.
35
:returns: If the sed command exit code was zero then return,
36
otherwise raise CalledProcessError.
38
expression = r's/{0}/{1}/{2}'.format(before,
41
return subprocess.check_call(["sed", "-i", "-r", "-e",
43
os.path.expanduser(filename)])