We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cookiecutter/hooks.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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='.'):

{"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"}], "after_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    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 hooks\n\n    for f in os.listdir(hooks_dir):\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\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"}]}