prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Wrong hook executed when a tilde-suffixed file of the same name exists
  - Cookiecutter version: 1.4.0
  - Template project url: https://github.com/thorgate/django-project-template
  - Python version: 3.4
  - Operating System: Ubuntu 15.10 wily
  ### Description:
  
  When using gedit or some other text editor that pollutes the directory with backup files ending with a tilde, cookiecutter mistakes that for the "real" hook it should run. This resulted in cookiecutter running a ridiculously outdated version of my pre-gen hook.
  
  The obvious solution is to just remove `pre_gen_project.py~`, which works, but I believe ideally cookiecutter shouldn't be running it in the first place.
  ### What I've run:
  
  ```
  gedit django-template/hooks/pre_gen_project.py
  cookiecutter django-template
  ```
  
  
  </issue>
  <code>
  [start of cookiecutter/hooks.py]
  1 # -*- coding: utf-8 -*-
  2 
  3 """
  4 cookiecutter.hooks
  5 ------------------
  6 
  7 Functions for discovering and executing various cookiecutter hooks.
  8 """
  9 
  10 import io
  11 import logging
  12 import os
  13 import subprocess
  14 import sys
  15 import tempfile
  16 
  17 from jinja2 import Template
  18 
  19 from cookiecutter import utils
  20 from .exceptions import FailedHookException
  21 
  22 
  23 _HOOKS = [
  24     'pre_gen_project',
  25     'post_gen_project',
  26     # TODO: other hooks should be listed here
  27 ]
  28 EXIT_SUCCESS = 0
  29 
  30 
  31 def find_hooks():
  32     """
  33     Must be called with the project template as the current working directory.
  34     Returns a dict of all hook scripts provided.
  35     Dict's key will be the hook/script's name, without extension, while
  36     values will be the absolute path to the script.
  37     Missing scripts will not be included in the returned dict.
  38     """
  39     hooks_dir = 'hooks'
  40     r = {}
  41     logging.debug('hooks_dir is {0}'.format(hooks_dir))
  42     if not os.path.isdir(hooks_dir):
  43         logging.debug('No hooks/ dir in template_dir')
  44         return r
  45     for f in os.listdir(hooks_dir):
  46         basename = os.path.splitext(os.path.basename(f))[0]
  47         if basename in _HOOKS:
  48             r[basename] = os.path.abspath(os.path.join(hooks_dir, f))
  49     return r
  50 
  51 
  52 def run_script(script_path, cwd='.'):
  53     """
  54     Executes a script from a working directory.
  55 
  56     :param script_path: Absolute path to the script to run.
  57     :param cwd: The directory to run the script from.
  58     """
  59     run_thru_shell = sys.platform.startswith('win')
  60     if script_path.endswith('.py'):
  61         script_command = [sys.executable, script_path]
  62     else:
  63         script_command = [script_path]
  64 
  65     utils.make_executable(script_path)
  66 
  67     proc = subprocess.Popen(
  68         script_command,
  69         shell=run_thru_shell,
  70         cwd=cwd
  71     )
  72     exit_status = proc.wait()
  73     if exit_status != EXIT_SUCCESS:
  74         raise FailedHookException(
  75             "Hook script failed (exit status: %d)" % exit_status)
  76 
  77 
  78 def run_script_with_context(script_path, cwd, context):
  79     """
  80     Executes a script after rendering with it Jinja.
  81 
  82     :param script_path: Absolute path to the script to run.
  83     :param cwd: The directory to run the script from.
  84     :param context: Cookiecutter project template context.
  85     """
  86     _, extension = os.path.splitext(script_path)
  87 
  88     contents = io.open(script_path, 'r', encoding='utf-8').read()
  89 
  90     with tempfile.NamedTemporaryFile(
  91         delete=False,
  92         mode='wb',
  93         suffix=extension
  94     ) as temp:
  95         output = Template(contents).render(**context)
  96         temp.write(output.encode('utf-8'))
  97 
  98     run_script(temp.name, cwd)
  99 
  100 
  101 def run_hook(hook_name, project_dir, context):
  102     """
  103     Try to find and execute a hook from the specified project directory.
  104 
  105     :param hook_name: The hook to execute.
  106     :param project_dir: The directory to execute the script from.
  107     :param context: Cookiecutter project context.
  108     """
  109     script = find_hooks().get(hook_name)
  110     if script is None:
  111         logging.debug('No hooks found')
  112         return
  113     run_script_with_context(script, project_dir, context)
  114
  [end of cookiecutter/hooks.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/cookiecutter/hooks.py b/cookiecutter/hooks.py
  --- a/cookiecutter/hooks.py
  +++ b/cookiecutter/hooks.py
  @@ -37,16 +37,20 @@
       Missing scripts will not be included in the returned dict.
       """
       hooks_dir = 'hooks'
  -    r = {}
  +    hooks = {}
       logging.debug('hooks_dir is {0}'.format(hooks_dir))
  +
       if not os.path.isdir(hooks_dir):
           logging.debug('No hooks/ dir in template_dir')
  -        return r
  +        return hooks
  +
       for f in os.listdir(hooks_dir):
  -        basename = os.path.splitext(os.path.basename(f))[0]
  -        if basename in _HOOKS:
  -            r[basename] = os.path.abspath(os.path.join(hooks_dir, f))
  -    return r
  +        filename = os.path.basename(f)
  +        basename = os.path.splitext(filename)[0]
  +
  +        if basename in _HOOKS and not filename.endswith('~'):
  +            hooks[basename] = os.path.abspath(os.path.join(hooks_dir, f))
  +    return hooks
   
   
   def run_script(script_path, cwd='.'):
  
verification_info: {"golden_diff": "diff --git a/cookiecutter/hooks.py b/cookiecutter/hooks.py\n--- a/cookiecutter/hooks.py\n+++ b/cookiecutter/hooks.py\n@@ -37,16 +37,20 @@\n     Missing scripts will not be included in the returned dict.\n     \"\"\"\n     hooks_dir = 'hooks'\n-    r = {}\n+    hooks = {}\n     logging.debug('hooks_dir is {0}'.format(hooks_dir))\n+\n     if not os.path.isdir(hooks_dir):\n         logging.debug('No hooks/ dir in template_dir')\n-        return r\n+        return hooks\n+\n     for f in os.listdir(hooks_dir):\n-        basename = os.path.splitext(os.path.basename(f))[0]\n-        if basename in _HOOKS:\n-            r[basename] = os.path.abspath(os.path.join(hooks_dir, f))\n-    return r\n+        filename = os.path.basename(f)\n+        basename = os.path.splitext(filename)[0]\n+\n+        if basename in _HOOKS and not filename.endswith('~'):\n+            hooks[basename] = os.path.abspath(os.path.join(hooks_dir, f))\n+    return hooks\n \n \n def run_script(script_path, cwd='.'):\n", "issue": "Wrong hook executed when a tilde-suffixed file of the same name exists\n- Cookiecutter version: 1.4.0\n- Template project url: https://github.com/thorgate/django-project-template\n- Python version: 3.4\n- Operating System: Ubuntu 15.10 wily\n### Description:\n\nWhen using gedit or some other text editor that pollutes the directory with backup files ending with a tilde, cookiecutter mistakes that for the \"real\" hook it should run. This resulted in cookiecutter running a ridiculously outdated version of my pre-gen hook.\n\nThe obvious solution is to just remove `pre_gen_project.py~`, which works, but I believe ideally cookiecutter shouldn't be running it in the first place.\n### What I've run:\n\n```\ngedit django-template/hooks/pre_gen_project.py\ncookiecutter django-template\n```\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n\"\"\"\ncookiecutter.hooks\n------------------\n\nFunctions for discovering and executing various cookiecutter hooks.\n\"\"\"\n\nimport io\nimport logging\nimport os\nimport subprocess\nimport sys\nimport tempfile\n\nfrom jinja2 import Template\n\nfrom cookiecutter import utils\nfrom .exceptions import FailedHookException\n\n\n_HOOKS = [\n    'pre_gen_project',\n    'post_gen_project',\n    # TODO: other hooks should be listed here\n]\nEXIT_SUCCESS = 0\n\n\ndef find_hooks():\n    \"\"\"\n    Must be called with the project template as the current working directory.\n    Returns a dict of all hook scripts provided.\n    Dict's key will be the hook/script's name, without extension, while\n    values will be the absolute path to the script.\n    Missing scripts will not be included in the returned dict.\n    \"\"\"\n    hooks_dir = 'hooks'\n    r = {}\n    logging.debug('hooks_dir is {0}'.format(hooks_dir))\n    if not os.path.isdir(hooks_dir):\n        logging.debug('No hooks/ dir in template_dir')\n        return r\n    for f in os.listdir(hooks_dir):\n        basename = os.path.splitext(os.path.basename(f))[0]\n        if basename in _HOOKS:\n            r[basename] = os.path.abspath(os.path.join(hooks_dir, f))\n    return r\n\n\ndef run_script(script_path, cwd='.'):\n    \"\"\"\n    Executes a script from a working directory.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    \"\"\"\n    run_thru_shell = sys.platform.startswith('win')\n    if script_path.endswith('.py'):\n        script_command = [sys.executable, script_path]\n    else:\n        script_command = [script_path]\n\n    utils.make_executable(script_path)\n\n    proc = subprocess.Popen(\n        script_command,\n        shell=run_thru_shell,\n        cwd=cwd\n    )\n    exit_status = proc.wait()\n    if exit_status != EXIT_SUCCESS:\n        raise FailedHookException(\n            \"Hook script failed (exit status: %d)\" % exit_status)\n\n\ndef run_script_with_context(script_path, cwd, context):\n    \"\"\"\n    Executes a script after rendering with it Jinja.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    :param context: Cookiecutter project template context.\n    \"\"\"\n    _, extension = os.path.splitext(script_path)\n\n    contents = io.open(script_path, 'r', encoding='utf-8').read()\n\n    with tempfile.NamedTemporaryFile(\n        delete=False,\n        mode='wb',\n        suffix=extension\n    ) as temp:\n        output = Template(contents).render(**context)\n        temp.write(output.encode('utf-8'))\n\n    run_script(temp.name, cwd)\n\n\ndef run_hook(hook_name, project_dir, context):\n    \"\"\"\n    Try to find and execute a hook from the specified project directory.\n\n    :param hook_name: The hook to execute.\n    :param project_dir: The directory to execute the script from.\n    :param context: Cookiecutter project context.\n    \"\"\"\n    script = find_hooks().get(hook_name)\n    if script is None:\n        logging.debug('No hooks found')\n        return\n    run_script_with_context(script, project_dir, context)\n", "path": "cookiecutter/hooks.py"}]}