import os
import subprocess

from .cache import compute_key


class Rule:
    def __init__(self, name, build_command, export):
        self.name = name
        self.build_command = build_command
        self.export = export

    def cache_key(self, resolver, input_tree):
        return compute_key({
            "input_tree": input_tree,
            "build": self.build_command,
            "export": self.export,
        })

    def do_build(self, path):
        if self.build_command:
            subprocess.check_call(self.build_command, shell=True, cwd=path)

    def get_tree(self, cache, resolver, input_tree):
        key = self.cache_key(resolver, input_tree)
        if key in cache.keyval:
            return cache.keyval[key]

        with cache.tmp_dir() as tmp_dir:
            cache.export_tree(input_tree, tmp_dir)
            self.do_build(tmp_dir)
            export_dir = tmp_dir
            if self.export:
                export_dir = os.path.join(tmp_dir, self.export)
            if not os.path.exists(export_dir):
                raise RuntimeError(
                    "export dir '{}' doesn't exist".format(self.export))
            tree = cache.import_tree(export_dir)

        cache.keyval[key] = tree
        return tree

@@ -53,16 +53,50 @@
 ute_key%0A
+from .error import PrintableError%0A
 %0A%0Aclass 
@@ -491,24 +491,28 @@
 %0A        if 
+not 
 self.build_c
@@ -519,16 +519,48 @@
 ommand:%0A
+            return%0A        try:%0A
         
@@ -626,16 +626,137 @@
 wd=path)
+%0A        except subprocess.CalledProcessError as e:%0A            raise PrintableError(%22Error in build command: %22 + str(e))
 %0A%0A    de

use PrintableError in do_build()