~charmers/charms/precise/juju-gui/trunk

« back to all changes in this revision

Viewing changes to hooks/bootstrap_utils.py

  • Committer: Francesco Banconi
  • Date: 2013-10-09 10:48:53 UTC
  • mfrom: (60.13.41 trunk)
  • Revision ID: francesco.banconi@canonical.com-20131009104853-6b8oxx4k1ycmv2yk
Tags: 0.11.0
MergedĀ juju-guiĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This file is part of the Juju GUI, which lets users view and manage Juju
2
 
# environments within a graphical interface (https://launchpad.net/juju-gui).
3
 
# Copyright (C) 2012-2013 Canonical Ltd.
4
 
#
5
 
# This program is free software: you can redistribute it and/or modify it under
6
 
# the terms of the GNU Affero General Public License version 3, as published by
7
 
# the Free Software Foundation.
8
 
#
9
 
# This program is distributed in the hope that it will be useful, but WITHOUT
10
 
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11
 
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
# Affero General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Affero General Public License
15
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
"""
18
 
These are actually maintained in python-shelltoolbox.  Precise does not have
19
 
that package, so we need to bootstrap the process by copying the functions
20
 
we need here.
21
 
"""
22
 
 
23
 
import subprocess
24
 
 
25
 
 
26
 
try:
27
 
    import shelltoolbox
28
 
except ImportError:
29
 
 
30
 
    def run(*args, **kwargs):
31
 
        """Run the command with the given arguments.
32
 
 
33
 
        The first argument is the path to the command to run.
34
 
        Subsequent arguments are command-line arguments to be passed.
35
 
 
36
 
        This function accepts all optional keyword arguments accepted by
37
 
        `subprocess.Popen`.
38
 
        """
39
 
        args = [i for i in args if i is not None]
40
 
        pipe = subprocess.PIPE
41
 
        process = subprocess.Popen(
42
 
            args, stdout=kwargs.pop('stdout', pipe),
43
 
            stderr=kwargs.pop('stderr', pipe),
44
 
            close_fds=kwargs.pop('close_fds', True), **kwargs)
45
 
        stdout, stderr = process.communicate()
46
 
        if process.returncode:
47
 
            exception = subprocess.CalledProcessError(
48
 
                process.returncode, repr(args))
49
 
            # The output argument of `CalledProcessError` was introduced in
50
 
            # Python 2.7. Monkey patch the output here to avoid TypeErrors
51
 
            # in older versions of Python, still preserving the output in
52
 
            # Python 2.7.
53
 
            exception.output = ''.join(filter(None, [stdout, stderr]))
54
 
            raise exception
55
 
        return stdout
56
 
 
57
 
    def install_extra_repositories(*repositories):
58
 
        """Install all of the extra repositories and update apt.
59
 
 
60
 
        Given repositories can contain a "{distribution}" placeholder,
61
 
        that will be replaced by current distribution codename.
62
 
 
63
 
        :raises: subprocess.CalledProcessError
64
 
        """
65
 
        distribution = run('lsb_release', '-cs').strip()
66
 
        # Starting from Oneiric, `apt-add-repository` is interactive by
67
 
        # default, and requires a "-y" flag to be set.
68
 
        assume_yes = None if distribution == 'lucid' else '-y'
69
 
        for repo in repositories:
70
 
            repository = repo.format(distribution=distribution)
71
 
            run('apt-add-repository', assume_yes, repository)
72
 
        run('apt-get', 'clean')
73
 
        run('apt-get', 'update')
74
 
 
75
 
else:
76
 
    install_extra_repositories = shelltoolbox.install_extra_repositories
77
 
    run = shelltoolbox.run