~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/alternatives.py

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
''' Helper for managing alternatives for file conflict resolution '''
 
18
 
 
19
import subprocess
 
20
import shutil
 
21
import os
 
22
 
 
23
 
 
24
def install_alternative(name, target, source, priority=50):
 
25
    ''' Install alternative configuration '''
 
26
    if (os.path.exists(target) and not os.path.islink(target)):
 
27
        # Move existing file/directory away before installing
 
28
        shutil.move(target, '{}.bak'.format(target))
 
29
    cmd = [
 
30
        'update-alternatives', '--force', '--install',
 
31
        target, name, source, str(priority)
 
32
    ]
 
33
    subprocess.check_call(cmd)