prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  ^C^C during installation may leave pre-commit in a bad state
  There's code which handles the first ^C, however I think the second one (during execution of the finally block) may not be handled well.  I probably need to make the cleanup atomic somehow...
  
  
  </issue>
  <code>
  [start of pre_commit/repository.py]
  1 from __future__ import unicode_literals
  2 
  3 from cached_property import cached_property
  4 
  5 from pre_commit.languages.all import languages
  6 from pre_commit.manifest import Manifest
  7 from pre_commit.prefixed_command_runner import PrefixedCommandRunner
  8 
  9 
  10 class Repository(object):
  11     def __init__(self, repo_config, repo_path_getter):
  12         self.repo_config = repo_config
  13         self.repo_path_getter = repo_path_getter
  14         self.__installed = False
  15 
  16     @classmethod
  17     def create(cls, config, store):
  18         repo_path_getter = store.get_repo_path_getter(
  19             config['repo'], config['sha']
  20         )
  21         return cls(config, repo_path_getter)
  22 
  23     @cached_property
  24     def repo_url(self):
  25         return self.repo_config['repo']
  26 
  27     @cached_property
  28     def sha(self):
  29         return self.repo_config['sha']
  30 
  31     @cached_property
  32     def languages(self):
  33         return set(
  34             (hook['language'], hook['language_version'])
  35             for _, hook in self.hooks
  36         )
  37 
  38     @cached_property
  39     def hooks(self):
  40         # TODO: merging in manifest dicts is a smell imo
  41         return tuple(
  42             (hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
  43             for hook in self.repo_config['hooks']
  44         )
  45 
  46     @cached_property
  47     def manifest(self):
  48         return Manifest(self.repo_path_getter)
  49 
  50     @cached_property
  51     def cmd_runner(self):
  52         return PrefixedCommandRunner(self.repo_path_getter.repo_path)
  53 
  54     def require_installed(self):
  55         if self.__installed:
  56             return
  57 
  58         self.install()
  59         self.__installed = True
  60 
  61     def install(self):
  62         """Install the hook repository."""
  63         for language_name, language_version in self.languages:
  64             language = languages[language_name]
  65             if (
  66                 language.ENVIRONMENT_DIR is None or
  67                 self.cmd_runner.exists(language.ENVIRONMENT_DIR)
  68             ):
  69                 # The language is already installed
  70                 continue
  71             language.install_environment(self.cmd_runner, language_version)
  72 
  73     def run_hook(self, hook, file_args):
  74         """Run a hook.
  75 
  76         Args:
  77             hook - Hook dictionary
  78             file_args - List of files to run
  79         """
  80         self.require_installed()
  81         return languages[hook['language']].run_hook(
  82             self.cmd_runner, hook, file_args,
  83         )
  84
  [end of pre_commit/repository.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/pre_commit/repository.py b/pre_commit/repository.py
  --- a/pre_commit/repository.py
  +++ b/pre_commit/repository.py
  @@ -1,5 +1,7 @@
   from __future__ import unicode_literals
   
  +import shutil
  +
   from cached_property import cached_property
   
   from pre_commit.languages.all import languages
  @@ -64,11 +66,21 @@
               language = languages[language_name]
               if (
                   language.ENVIRONMENT_DIR is None or
  -                self.cmd_runner.exists(language.ENVIRONMENT_DIR)
  +                self.cmd_runner.exists(language.ENVIRONMENT_DIR, '.installed')
               ):
                   # The language is already installed
                   continue
  +            # There's potentially incomplete cleanup from previous runs
  +            # Clean it up!
  +            if self.cmd_runner.exists(language.ENVIRONMENT_DIR):
  +                shutil.rmtree(self.cmd_runner.path(language.ENVIRONMENT_DIR))
  +
               language.install_environment(self.cmd_runner, language_version)
  +            # Touch the .installed file (atomic) to indicate we've installed
  +            open(
  +                self.cmd_runner.path(language.ENVIRONMENT_DIR, '.installed'),
  +                'w',
  +            ).close()
   
       def run_hook(self, hook, file_args):
           """Run a hook.
  
verification_info: {"golden_diff": "diff --git a/pre_commit/repository.py b/pre_commit/repository.py\n--- a/pre_commit/repository.py\n+++ b/pre_commit/repository.py\n@@ -1,5 +1,7 @@\n from __future__ import unicode_literals\n \n+import shutil\n+\n from cached_property import cached_property\n \n from pre_commit.languages.all import languages\n@@ -64,11 +66,21 @@\n             language = languages[language_name]\n             if (\n                 language.ENVIRONMENT_DIR is None or\n-                self.cmd_runner.exists(language.ENVIRONMENT_DIR)\n+                self.cmd_runner.exists(language.ENVIRONMENT_DIR, '.installed')\n             ):\n                 # The language is already installed\n                 continue\n+            # There's potentially incomplete cleanup from previous runs\n+            # Clean it up!\n+            if self.cmd_runner.exists(language.ENVIRONMENT_DIR):\n+                shutil.rmtree(self.cmd_runner.path(language.ENVIRONMENT_DIR))\n+\n             language.install_environment(self.cmd_runner, language_version)\n+            # Touch the .installed file (atomic) to indicate we've installed\n+            open(\n+                self.cmd_runner.path(language.ENVIRONMENT_DIR, '.installed'),\n+                'w',\n+            ).close()\n \n     def run_hook(self, hook, file_args):\n         \"\"\"Run a hook.\n", "issue": "^C^C during installation may leave pre-commit in a bad state\nThere's code which handles the first ^C, however I think the second one (during execution of the finally block) may not be handled well.  I probably need to make the cleanup atomic somehow...\n\n", "before_files": [{"content": "from __future__ import unicode_literals\n\nfrom cached_property import cached_property\n\nfrom pre_commit.languages.all import languages\nfrom pre_commit.manifest import Manifest\nfrom pre_commit.prefixed_command_runner import PrefixedCommandRunner\n\n\nclass Repository(object):\n    def __init__(self, repo_config, repo_path_getter):\n        self.repo_config = repo_config\n        self.repo_path_getter = repo_path_getter\n        self.__installed = False\n\n    @classmethod\n    def create(cls, config, store):\n        repo_path_getter = store.get_repo_path_getter(\n            config['repo'], config['sha']\n        )\n        return cls(config, repo_path_getter)\n\n    @cached_property\n    def repo_url(self):\n        return self.repo_config['repo']\n\n    @cached_property\n    def sha(self):\n        return self.repo_config['sha']\n\n    @cached_property\n    def languages(self):\n        return set(\n            (hook['language'], hook['language_version'])\n            for _, hook in self.hooks\n        )\n\n    @cached_property\n    def hooks(self):\n        # TODO: merging in manifest dicts is a smell imo\n        return tuple(\n            (hook['id'], dict(self.manifest.hooks[hook['id']], **hook))\n            for hook in self.repo_config['hooks']\n        )\n\n    @cached_property\n    def manifest(self):\n        return Manifest(self.repo_path_getter)\n\n    @cached_property\n    def cmd_runner(self):\n        return PrefixedCommandRunner(self.repo_path_getter.repo_path)\n\n    def require_installed(self):\n        if self.__installed:\n            return\n\n        self.install()\n        self.__installed = True\n\n    def install(self):\n        \"\"\"Install the hook repository.\"\"\"\n        for language_name, language_version in self.languages:\n            language = languages[language_name]\n            if (\n                language.ENVIRONMENT_DIR is None or\n                self.cmd_runner.exists(language.ENVIRONMENT_DIR)\n            ):\n                # The language is already installed\n                continue\n            language.install_environment(self.cmd_runner, language_version)\n\n    def run_hook(self, hook, file_args):\n        \"\"\"Run a hook.\n\n        Args:\n            hook - Hook dictionary\n            file_args - List of files to run\n        \"\"\"\n        self.require_installed()\n        return languages[hook['language']].run_hook(\n            self.cmd_runner, hook, file_args,\n        )\n", "path": "pre_commit/repository.py"}]}