We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
.cookiecutterrc and cookiecutters_dir not working as I expected
Hi,
Here's the setup:
- I have a ~/.cookiecutterrc as talked about here: http://cookiecutter.readthedocs.org/en/latest/advanced_usage.html
- I also have a cookiecutters_dir at ~/.cookiecutters/ with the subdirectory cookiecutter-pypackage/.

When I try to run "cookiecutter cookiecutter-pypackage/" in ~/Projects/, I get the following error

```
Traceback (most recent call last):
  File "/opt/anaconda/bin/cookiecutter", line 9, in <module>
    load_entry_point('cookiecutter==0.9.0', 'console_scripts', 'cookiecutter')()
  File "/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py", line 610, in __call__
    return self.main(*args, **kwargs)
  File "/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py", line 590, in main
    rv = self.invoke(ctx)
  File "/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py", line 782, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py", line 416, in invoke
    return callback(*args, **kwargs)
  File "/Users/alexp/Projects/cookiecutter/cookiecutter/cli.py", line 70, in main
    cookiecutter(template, checkout, no_input)
  File "/Users/alexp/Projects/cookiecutter/cookiecutter/main.py", line 95, in cookiecutter
    extra_context=extra_context,
  File "/Users/alexp/Projects/cookiecutter/cookiecutter/generate.py", line 43, in generate_context
    file_handle = open(context_file)
IOError: [Errno 2] No such file or directory: u'cookiecutter-pypackage/cookiecutter.json'
```

This error shows up if I either do pip install or with the git repo locally.  Naturally, it makes a bit of sense.  There is no directory ~/Projects/cookiecutter-pypackage/.  

However, and perhaps I'm making a poor assumption about usage, it seems to me if I clone or otherwise create a cookiecutter and it's sitting in cookiecutters_dir, it'd be nice to just refer to it as I did above.  For my usage, if I create a cookiecutter, I don't particularly want it sitting around a Projects directory, especially if I have multiple project directories for personal and organizational purposes.

In order to do this, I added three lines to main.py in my fork (see below) and it seems to work.  I did it as an `elif` to try to preserve the possibility of a lack of a cookiecutters_dir.  I have not written a test for this and admittedly I don't really know how.  I will likely just use my fork with this modificationgoing forward but I wanted to let the developer crew know about this.  

Cheers.

```
# cookiecutter.main
...
# TODO: find a better way to tell if it's a repo URL
if 'git@' in template or 'https://' in template:
    repo_dir = clone(
        repo_url=template,
        checkout=checkout,
        clone_to_dir=config_dict['cookiecutters_dir'],
        no_input=no_input
    )
#### Added these three lines
elif 'cookiecutters_dir' in config_dict:
    cookiecutters_dir = os.path.expanduser(config_dict['cookiecutters_dir'])
    repo_dir = os.path.join(cookiecutters_dir,template)
else:
    # If it's a local repo, no need to clone or copy to your
    # cookiecutters_dir
    repo_dir = 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/repository.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 """Cookiecutter repository functions."""
4 from __future__ import unicode_literals
5 import os
6 import re
7 
8 from .exceptions import RepositoryNotFound
9 from .vcs import clone
10 
11 REPO_REGEX = re.compile(r"""
12 (?x)
13 ((((git|hg)\+)?(git|ssh|https?):(//)?)  # something like git:// ssh:// etc.
14  |                                      # or
15  (\w+@[\w\.]+)                          # something like user@...
16 )
17 """)
18 
19 
20 def is_repo_url(value):
21     """Return True if value is a repository URL."""
22     return bool(REPO_REGEX.match(value))
23 
24 
25 def expand_abbreviations(template, abbreviations):
26     """
27     Expand abbreviations in a template name.
28 
29     :param template: The project template name.
30     :param abbreviations: Abbreviation definitions.
31     """
32     if template in abbreviations:
33         return abbreviations[template]
34 
35     # Split on colon. If there is no colon, rest will be empty
36     # and prefix will be the whole template
37     prefix, sep, rest = template.partition(':')
38     if prefix in abbreviations:
39         return abbreviations[prefix].format(rest)
40 
41     return template
42 
43 
44 def repository_has_cookiecutter_json(repo_directory):
45     """Determines if `repo_directory` contains a `cookiecutter.json` file.
46 
47     :param repo_directory: The candidate repository directory.
48     :return: True if the `repo_directory` is valid, else False.
49     """
50     repo_directory_exists = os.path.isdir(repo_directory)
51 
52     repo_config_exists = os.path.isfile(
53         os.path.join(repo_directory, 'cookiecutter.json')
54     )
55     return repo_directory_exists and repo_config_exists
56 
57 
58 def determine_repo_dir(template, abbreviations, clone_to_dir, checkout,
59                        no_input):
60     """
61     Locate the repository directory from a template reference.
62 
63     Applies repository abbreviations to the template reference.
64     If the template refers to a repository URL, clone it.
65     If the template is a path to a local repository, use it.
66 
67     :param template: A directory containing a project template directory,
68         or a URL to a git repository.
69     :param abbreviations: A dictionary of repository abbreviation
70         definitions.
71     :param clone_to_dir: The directory to clone the repository into.
72     :param checkout: The branch, tag or commit ID to checkout after clone.
73     :param no_input: Prompt the user at command line for manual configuration?
74     :return: The cookiecutter template directory
75     :raises: `RepositoryNotFound` if a repository directory could not be found.
76     """
77     template = expand_abbreviations(template, abbreviations)
78 
79     if is_repo_url(template):
80         repo_dir = clone(
81             repo_url=template,
82             checkout=checkout,
83             clone_to_dir=clone_to_dir,
84             no_input=no_input,
85         )
86     else:
87         # If it's a local repo, no need to clone or copy to your
88         # cookiecutters_dir
89         repo_dir = template
90 
91     if repository_has_cookiecutter_json(repo_dir):
92         return repo_dir
93 
94     raise RepositoryNotFound(
95         'The repository {} could not be located or does not contain '
96         'a "cookiecutter.json" file.'.format(repo_dir)
97     )
98
```
--- 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/repository.py b/cookiecutter/repository.py
--- a/cookiecutter/repository.py
+++ b/cookiecutter/repository.py
@@ -77,21 +77,27 @@
     template = expand_abbreviations(template, abbreviations)
 
     if is_repo_url(template):
-        repo_dir = clone(
+        cloned_repo = clone(
             repo_url=template,
             checkout=checkout,
             clone_to_dir=clone_to_dir,
             no_input=no_input,
         )
+        repository_candidates = [cloned_repo]
     else:
-        # If it's a local repo, no need to clone or copy to your
-        # cookiecutters_dir
-        repo_dir = template
+        repository_candidates = [
+            template,
+            os.path.join(clone_to_dir, template)
+        ]
 
-    if repository_has_cookiecutter_json(repo_dir):
-        return repo_dir
+    for repo_candidate in repository_candidates:
+        if repository_has_cookiecutter_json(repo_candidate):
+            return repo_candidate
 
     raise RepositoryNotFound(
-        'The repository {} could not be located or does not contain '
-        'a "cookiecutter.json" file.'.format(repo_dir)
+        'A valid repository for "{}" could not be found in the following '
+        'locations:\n{}'.format(
+            template,
+            '\n'.join(repository_candidates)
+        )
     )

{"golden_diff": "diff --git a/cookiecutter/repository.py b/cookiecutter/repository.py\n--- a/cookiecutter/repository.py\n+++ b/cookiecutter/repository.py\n@@ -77,21 +77,27 @@\n     template = expand_abbreviations(template, abbreviations)\n \n     if is_repo_url(template):\n-        repo_dir = clone(\n+        cloned_repo = clone(\n             repo_url=template,\n             checkout=checkout,\n             clone_to_dir=clone_to_dir,\n             no_input=no_input,\n         )\n+        repository_candidates = [cloned_repo]\n     else:\n-        # If it's a local repo, no need to clone or copy to your\n-        # cookiecutters_dir\n-        repo_dir = template\n+        repository_candidates = [\n+            template,\n+            os.path.join(clone_to_dir, template)\n+        ]\n \n-    if repository_has_cookiecutter_json(repo_dir):\n-        return repo_dir\n+    for repo_candidate in repository_candidates:\n+        if repository_has_cookiecutter_json(repo_candidate):\n+            return repo_candidate\n \n     raise RepositoryNotFound(\n-        'The repository {} could not be located or does not contain '\n-        'a \"cookiecutter.json\" file.'.format(repo_dir)\n+        'A valid repository for \"{}\" could not be found in the following '\n+        'locations:\\n{}'.format(\n+            template,\n+            '\\n'.join(repository_candidates)\n+        )\n     )\n", "issue": ".cookiecutterrc and cookiecutters_dir not working as I expected\nHi,\nHere's the setup:\n- I have a ~/.cookiecutterrc as talked about here: http://cookiecutter.readthedocs.org/en/latest/advanced_usage.html\n- I also have a cookiecutters_dir at ~/.cookiecutters/ with the subdirectory cookiecutter-pypackage/.\n\nWhen I try to run \"cookiecutter cookiecutter-pypackage/\" in ~/Projects/, I get the following error\n\n```\nTraceback (most recent call last):\n  File \"/opt/anaconda/bin/cookiecutter\", line 9, in <module>\n    load_entry_point('cookiecutter==0.9.0', 'console_scripts', 'cookiecutter')()\n  File \"/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py\", line 610, in __call__\n    return self.main(*args, **kwargs)\n  File \"/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py\", line 590, in main\n    rv = self.invoke(ctx)\n  File \"/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py\", line 782, in invoke\n    return ctx.invoke(self.callback, **ctx.params)\n  File \"/opt/anaconda/lib/python2.7/site-packages/click-3.3-py2.7.egg/click/core.py\", line 416, in invoke\n    return callback(*args, **kwargs)\n  File \"/Users/alexp/Projects/cookiecutter/cookiecutter/cli.py\", line 70, in main\n    cookiecutter(template, checkout, no_input)\n  File \"/Users/alexp/Projects/cookiecutter/cookiecutter/main.py\", line 95, in cookiecutter\n    extra_context=extra_context,\n  File \"/Users/alexp/Projects/cookiecutter/cookiecutter/generate.py\", line 43, in generate_context\n    file_handle = open(context_file)\nIOError: [Errno 2] No such file or directory: u'cookiecutter-pypackage/cookiecutter.json'\n```\n\nThis error shows up if I either do pip install or with the git repo locally.  Naturally, it makes a bit of sense.  There is no directory ~/Projects/cookiecutter-pypackage/.  \n\nHowever, and perhaps I'm making a poor assumption about usage, it seems to me if I clone or otherwise create a cookiecutter and it's sitting in cookiecutters_dir, it'd be nice to just refer to it as I did above.  For my usage, if I create a cookiecutter, I don't particularly want it sitting around a Projects directory, especially if I have multiple project directories for personal and organizational purposes.\n\nIn order to do this, I added three lines to main.py in my fork (see below) and it seems to work.  I did it as an `elif` to try to preserve the possibility of a lack of a cookiecutters_dir.  I have not written a test for this and admittedly I don't really know how.  I will likely just use my fork with this modificationgoing forward but I wanted to let the developer crew know about this.  \n\nCheers.\n\n```\n# cookiecutter.main\n...\n# TODO: find a better way to tell if it's a repo URL\nif 'git@' in template or 'https://' in template:\n    repo_dir = clone(\n        repo_url=template,\n        checkout=checkout,\n        clone_to_dir=config_dict['cookiecutters_dir'],\n        no_input=no_input\n    )\n#### Added these three lines\nelif 'cookiecutters_dir' in config_dict:\n    cookiecutters_dir = os.path.expanduser(config_dict['cookiecutters_dir'])\n    repo_dir = os.path.join(cookiecutters_dir,template)\nelse:\n    # If it's a local repo, no need to clone or copy to your\n    # cookiecutters_dir\n    repo_dir = template\n```\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n\"\"\"Cookiecutter repository functions.\"\"\"\nfrom __future__ import unicode_literals\nimport os\nimport re\n\nfrom .exceptions import RepositoryNotFound\nfrom .vcs import clone\n\nREPO_REGEX = re.compile(r\"\"\"\n(?x)\n((((git|hg)\\+)?(git|ssh|https?):(//)?)  # something like git:// ssh:// etc.\n |                                      # or\n (\\w+@[\\w\\.]+)                          # something like user@...\n)\n\"\"\")\n\n\ndef is_repo_url(value):\n    \"\"\"Return True if value is a repository URL.\"\"\"\n    return bool(REPO_REGEX.match(value))\n\n\ndef expand_abbreviations(template, abbreviations):\n    \"\"\"\n    Expand abbreviations in a template name.\n\n    :param template: The project template name.\n    :param abbreviations: Abbreviation definitions.\n    \"\"\"\n    if template in abbreviations:\n        return abbreviations[template]\n\n    # Split on colon. If there is no colon, rest will be empty\n    # and prefix will be the whole template\n    prefix, sep, rest = template.partition(':')\n    if prefix in abbreviations:\n        return abbreviations[prefix].format(rest)\n\n    return template\n\n\ndef repository_has_cookiecutter_json(repo_directory):\n    \"\"\"Determines if `repo_directory` contains a `cookiecutter.json` file.\n\n    :param repo_directory: The candidate repository directory.\n    :return: True if the `repo_directory` is valid, else False.\n    \"\"\"\n    repo_directory_exists = os.path.isdir(repo_directory)\n\n    repo_config_exists = os.path.isfile(\n        os.path.join(repo_directory, 'cookiecutter.json')\n    )\n    return repo_directory_exists and repo_config_exists\n\n\ndef determine_repo_dir(template, abbreviations, clone_to_dir, checkout,\n                       no_input):\n    \"\"\"\n    Locate the repository directory from a template reference.\n\n    Applies repository abbreviations to the template reference.\n    If the template refers to a repository URL, clone it.\n    If the template is a path to a local repository, use it.\n\n    :param template: A directory containing a project template directory,\n        or a URL to a git repository.\n    :param abbreviations: A dictionary of repository abbreviation\n        definitions.\n    :param clone_to_dir: The directory to clone the repository into.\n    :param checkout: The branch, tag or commit ID to checkout after clone.\n    :param no_input: Prompt the user at command line for manual configuration?\n    :return: The cookiecutter template directory\n    :raises: `RepositoryNotFound` if a repository directory could not be found.\n    \"\"\"\n    template = expand_abbreviations(template, abbreviations)\n\n    if is_repo_url(template):\n        repo_dir = clone(\n            repo_url=template,\n            checkout=checkout,\n            clone_to_dir=clone_to_dir,\n            no_input=no_input,\n        )\n    else:\n        # If it's a local repo, no need to clone or copy to your\n        # cookiecutters_dir\n        repo_dir = template\n\n    if repository_has_cookiecutter_json(repo_dir):\n        return repo_dir\n\n    raise RepositoryNotFound(\n        'The repository {} could not be located or does not contain '\n        'a \"cookiecutter.json\" file.'.format(repo_dir)\n    )\n", "path": "cookiecutter/repository.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n\"\"\"Cookiecutter repository functions.\"\"\"\nfrom __future__ import unicode_literals\nimport os\nimport re\n\nfrom .exceptions import RepositoryNotFound\nfrom .vcs import clone\n\nREPO_REGEX = re.compile(r\"\"\"\n(?x)\n((((git|hg)\\+)?(git|ssh|https?):(//)?)  # something like git:// ssh:// etc.\n |                                      # or\n (\\w+@[\\w\\.]+)                          # something like user@...\n)\n\"\"\")\n\n\ndef is_repo_url(value):\n    \"\"\"Return True if value is a repository URL.\"\"\"\n    return bool(REPO_REGEX.match(value))\n\n\ndef expand_abbreviations(template, abbreviations):\n    \"\"\"\n    Expand abbreviations in a template name.\n\n    :param template: The project template name.\n    :param abbreviations: Abbreviation definitions.\n    \"\"\"\n    if template in abbreviations:\n        return abbreviations[template]\n\n    # Split on colon. If there is no colon, rest will be empty\n    # and prefix will be the whole template\n    prefix, sep, rest = template.partition(':')\n    if prefix in abbreviations:\n        return abbreviations[prefix].format(rest)\n\n    return template\n\n\ndef repository_has_cookiecutter_json(repo_directory):\n    \"\"\"Determines if `repo_directory` contains a `cookiecutter.json` file.\n\n    :param repo_directory: The candidate repository directory.\n    :return: True if the `repo_directory` is valid, else False.\n    \"\"\"\n    repo_directory_exists = os.path.isdir(repo_directory)\n\n    repo_config_exists = os.path.isfile(\n        os.path.join(repo_directory, 'cookiecutter.json')\n    )\n    return repo_directory_exists and repo_config_exists\n\n\ndef determine_repo_dir(template, abbreviations, clone_to_dir, checkout,\n                       no_input):\n    \"\"\"\n    Locate the repository directory from a template reference.\n\n    Applies repository abbreviations to the template reference.\n    If the template refers to a repository URL, clone it.\n    If the template is a path to a local repository, use it.\n\n    :param template: A directory containing a project template directory,\n        or a URL to a git repository.\n    :param abbreviations: A dictionary of repository abbreviation\n        definitions.\n    :param clone_to_dir: The directory to clone the repository into.\n    :param checkout: The branch, tag or commit ID to checkout after clone.\n    :param no_input: Prompt the user at command line for manual configuration?\n    :return: The cookiecutter template directory\n    :raises: `RepositoryNotFound` if a repository directory could not be found.\n    \"\"\"\n    template = expand_abbreviations(template, abbreviations)\n\n    if is_repo_url(template):\n        cloned_repo = clone(\n            repo_url=template,\n            checkout=checkout,\n            clone_to_dir=clone_to_dir,\n            no_input=no_input,\n        )\n        repository_candidates = [cloned_repo]\n    else:\n        repository_candidates = [\n            template,\n            os.path.join(clone_to_dir, template)\n        ]\n\n    for repo_candidate in repository_candidates:\n        if repository_has_cookiecutter_json(repo_candidate):\n            return repo_candidate\n\n    raise RepositoryNotFound(\n        'A valid repository for \"{}\" could not be found in the following '\n        'locations:\\n{}'.format(\n            template,\n            '\\n'.join(repository_candidates)\n        )\n    )\n", "path": "cookiecutter/repository.py"}]}