~juju-qa/juju-ci-tools/trunk

« back to all changes in this revision

Viewing changes to concurrently.py

  • Committer: Curtis Hovey
  • Date: 2017-02-02 00:54:53 UTC
  • mto: This revision was merged to the branch mainline in revision 1871.
  • Revision ID: curtis@canonical.com-20170202005453-z3fcnz7bi83bzx9p
Log errors and out to the same log.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    def __init__(self, name, command, log_dir='.'):
31
31
        self.name = name
32
32
        self.command = command
33
 
        self.out_log_name = os.path.join(
34
 
            log_dir, '{}-out.log'.format(self.name))
35
 
        self.err_log_name = os.path.join(
36
 
            log_dir, '{}-err.log'.format(self.name))
 
33
        self.log_name = os.path.join(log_dir, '{}.log'.format(self.name))
37
34
        self.returncode = None
38
35
        self.proc = None
39
36
 
46
43
            return False
47
44
        return (self.name == other.name and
48
45
                self.command == other.command and
49
 
                self.out_log_name == other.out_log_name and
50
 
                self.err_log_name == other.err_log_name)
 
46
                self.log_name == other.log_name)
51
47
 
52
48
    @contextmanager
53
49
    def start(self):
54
50
        """Yield the running proc, then wait to set the returncode."""
55
 
        with open(self.out_log_name, 'ab') as out_log:
56
 
            with open(self.err_log_name, 'ab') as err_log:
57
 
                self.proc = subprocess.Popen(
58
 
                    self.command, stdout=out_log, stderr=err_log)
59
 
                log.debug('Started {}'.format(self.name))
60
 
                yield self.proc
 
51
        with open(self.log_name, 'ab') as out_log:
 
52
            self.proc = subprocess.Popen(
 
53
                self.command, stdout=out_log, stderr=out_log)
 
54
            log.debug('Started {}'.format(self.name))
 
55
            yield self.proc
61
56
 
62
57
    def finish(self):
63
58
        log.debug('Waiting for {} to finish'.format(self.name))
89
84
        for task in tasks:
90
85
            if task.returncode != 0:
91
86
                log.error('{} failed with {}\nSee {}'.format(
92
 
                          task.name, task.returncode, task.err_log_name))
 
87
                          task.name, task.returncode, task.log_name))
93
88
    return failed_count
94
89
 
95
90