summaryrefslogtreecommitdiff
path: root/morphlib/execute.py
blob: 9a279591e5b1e5f23a1d782a85480cc4c4aa2280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (C) 2011  Codethink Limited
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import logging
import os
import subprocess

import morphlib


class CommandFailure(Exception):

    pass


class Execute(object):

    '''Execute commands for morph.'''
    
    def __init__(self, dirname, msg):
        self._setup_env()
        self.dirname = dirname
        self.msg = msg

    def _setup_env(self):
        self.env = dict(os.environ)

    def run(self, commands):
        '''Execute a list of commands.
        
        If a command fails (returns non-zero exit code), the rest are
        not run, and CommandFailure is returned.
        
        '''

        stdouts = []
        for command in commands:
            self.msg('# %s' % command)
            p = subprocess.Popen([command], shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=self.env,
                                 cwd=self.dirname)
            out, err = p.communicate()
            logging.debug('Exit code: %d' % p.returncode)
            logging.debug('Standard output:\n%s' % morphlib.util.indent(out))
            logging.debug('Standard error:\n%s' % morphlib.util.indent(err))
            if p.returncode != 0:
                raise CommandFailure('Command failed: %s\n%s' % 
                                      (command, morphlib.util.indent(err)))
            stdouts.append(out)
        return stdouts

    def runv(self, argv):
        '''Run a command given as a list of argv elements.
        
        Return standard output. Raise ``CommandFailure`` if the command
        fails. Log standard output and error in any case.
        
        '''

        self.msg('# %s' % ' '.join(argv))
        p = subprocess.Popen(argv, stdout=subprocess.PIPE, 
                             stderr=subprocess.PIPE)
        out, err = p.communicate()
        
        logging.debug('Exit code: %d' % p.returncode)
        logging.debug('Standard output:\n%s' % morphlib.util.indent(out))
        logging.debug('Standard error:\n%s' % morphlib.util.indent(err))
        if p.returncode != 0:
            raise CommandFailure('Command failed: %s\n%s' % 
                                  (argv, morphlib.util.indent(err)))
        return out