~ajkavanagh/charms/trusty/memcached/add-spaces-support

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/files.py

  • Committer: Jorge Niedbalski
  • Date: 2016-09-20 18:01:20 UTC
  • mfrom: (72.2.3 memcached.py3)
  • Revision ID: jorge.niedbalski@canonical.com-20160920180120-apb4ut3cugwxcrer
[freyes, r=niedbalski] Fixes bug LP: #1576458

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
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
 
9
#
 
10
#  http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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.
 
17
 
 
18
__author__ = 'Jorge Niedbalski <niedbalski@ubuntu.com>'
 
19
 
 
20
import os
 
21
import subprocess
 
22
 
 
23
 
 
24
def sed(filename, before, after, flags='g'):
 
25
    """
 
26
    Search and replaces the given pattern on filename.
 
27
 
 
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.
 
37
    """
 
38
    expression = r's/{0}/{1}/{2}'.format(before,
 
39
                                         after, flags)
 
40
 
 
41
    return subprocess.check_call(["sed", "-i", "-r", "-e",
 
42
                                  expression,
 
43
                                  os.path.expanduser(filename)])