from __future__ import absolute_import

import os

from subprocess import PIPE, Popen

from ds.exceptions import CommandError


class Workspace(object):
    def __init__(self, path, logbuffer=PIPE):
        self.path = path
        self.logbuffer = logbuffer

    def run(self, command, *args, **kwargs):
        kwargs.setdefault('cwd', self.path)

        env = os.environ.copy()
        for key, value in kwargs.pop('env', {}).iteritems():
            env[key] = value

        kwargs['env'] = env

        kwargs['stdout'] = self.logbuffer
        kwargs['stderr'] = self.logbuffer

        self.logbuffer.write('>> Running {}\n'.format(command))
        proc = Popen(command, *args, **kwargs)
        (stdout, stderr) = proc.communicate()
        if proc.returncode != 0:
            raise CommandError(args[0], proc.returncode, stdout, stderr)
        return stdout

@@ -72,18 +72,20 @@
 rt P
-IPE, Popen
+open, STDOUT
 %0A%0Afr
@@ -190,13 +190,8 @@
 ffer
-=PIPE
 ):%0A 
@@ -561,30 +561,22 @@
 err'%5D = 
-self.logbuffer
+STDOUT
 %0A%0A      

Send STDERR to STDOUT