Skip to content

Commit

Permalink
Merge pull request #282 from Natim/add_pep8_checks
Browse files Browse the repository at this point in the history
Add tox to check the PEP8 + Fix current code PEP8.
  • Loading branch information
michaeljoseph committed Oct 24, 2014
2 parents a510715 + 7dfe6e1 commit bb4606f
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 49 deletions.
32 changes: 13 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
# Config file for automatic testing at travis-ci.org

language: python
python: 2.7
env:
- TOX_ENV=py26
- TOX_ENV=py27
- TOX_ENV=py33
- TOX_ENV=py34
- TOX_ENV=pypy
- TOX_ENV=flake8

python:
- "3.3"
- "2.7"
- "2.6"
- "pypy"
script: tox -e $TOX_ENV

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
# Note: we only use coveralls with Travis, not tox. pip install coveralls doesn't work on Windows.
# This setup makes it possible for:
# 1. the tests to be run with tox on Windows
# 2. Windows users to use the requirements files without running into installation problems.
install:
- pip install coveralls
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install -r requirements/test_26.txt; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install -r requirements/test_27.txt; fi
- if [[ $TRAVIS_PYTHON_VERSION == 'pypy' ]]; then pip install -r requirements/test_27.txt; fi
- if [[ $TRAVIS_PYTHON_VERSION == '3.3' ]]; then pip install -r requirements/test_33.txt; fi

# command to run tests, e.g. python setup.py test
script: coverage run --source cookiecutter setup.py test
- pip install tox

after_success:
coveralls
# Report coverage results to coveralls.io
- pip install coveralls
- coveralls
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Contributors
* Mishbah Razzaque (`@mishbahr`_)
* Robin Andeer (`@robinandeer`_)
* Rachel Sanders (`@trustrachel`_)
* Rémy Hubscher (`@Natim`_)

.. _`@vincentbernat`: https://github.com/vincentbernat
.. _`@audreyr`: https://github.com/audreyr
Expand Down Expand Up @@ -100,3 +101,4 @@ Contributors
.. _`@mishbahr`: https://github.com/mishbahr
.. _`@robinandeer`: https://github.com/robinandeer
.. _`@trustrachel`: https://github.com/trustrachel
.. _`@Natim`: https://github.com/Natim
5 changes: 5 additions & 0 deletions cookiecutter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,35 @@ class NonTemplatedInputDirException(CookiecutterException):
rendered to something else, so that input_dir != output_dir.
"""


class UnknownTemplateDirException(CookiecutterException):
"""
Raised when Cookiecutter cannot determine which directory is the project
template, e.g. more than one dir appears to be a template dir.
"""


class MissingProjectDir(CookiecutterException):
"""
Raised during cleanup when remove_repo() can't find a generated project
directory inside of a repo.
"""


class ConfigDoesNotExistException(CookiecutterException):
"""
Raised when get_config() is passed a path to a config file, but no file
is found at that path.
"""


class InvalidConfiguration(CookiecutterException):
"""
Raised if the global configuration file is not valid YAML or is
badly contructed.
"""


class UnknownRepoType(CookiecutterException):
"""
Raised if a repo's type cannot be determined.
Expand Down
14 changes: 7 additions & 7 deletions cookiecutter/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
def find_template(repo_dir):
"""
Determines which child directory of `repo_dir` is the project template.
:param repo_dir: Local directory of newly cloned repo.
:returns project_template: Relative path to project template.
"""

logging.debug('Searching {0} for the project template.'.format(repo_dir))

repo_dir_contents = os.listdir(repo_dir)

project_template = None
for item in repo_dir_contents:
if 'cookiecutter' in item and \
'{{' in item and \
'}}' in item:
if 'cookiecutter' in item and '{{' in item and '}}' in item:
project_template = item
break

if project_template:
project_template = os.path.join(repo_dir, project_template)
logging.debug('The project template appears to be {0}'.format(project_template))
logging.debug(
'The project template appears to be {0}'.format(project_template)
)
return project_template
else:
raise NonTemplatedInputDirException
raise NonTemplatedInputDirException
11 changes: 6 additions & 5 deletions cookiecutter/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def generate_context(context_file='cookiecutter.json', default_context=None,
:param context_file: JSON file containing key/value pairs for populating
the cookiecutter's variables.
:param default_context: Dictionary containing any config to take into account.
:param default_context: Dictionary containing config to take into account.
:param extra_context: Dictionary containing configuration overrides
"""

Expand Down Expand Up @@ -127,7 +127,8 @@ def generate_file(project_dir, infile, context, env):

def render_and_create_dir(dirname, context, output_dir):
"""
Renders the name of a directory, creates the directory, and returns its path.
Renders the name of a directory, creates the directory, and
returns its path.
"""

name_tmpl = Template(dirname)
Expand All @@ -147,8 +148,7 @@ def ensure_dir_is_templated(dirname):
"""
Ensures that dirname is a templated directory name.
"""
if '{{' in dirname and \
'}}' in dirname:
if '{{' in dirname and '}}' in dirname:
return True
else:
raise NonTemplatedInputDirException
Expand Down Expand Up @@ -191,7 +191,8 @@ def generate_files(repo_dir, context=None, output_dir="."):

for root, dirs, files in os.walk("."):
for d in dirs:
unrendered_dir = os.path.join(project_dir, os.path.join(root, d))
unrendered_dir = os.path.join(project_dir,
os.path.join(root, d))
render_and_create_dir(unrendered_dir, context, output_dir)

for f in files:
Expand Down
8 changes: 4 additions & 4 deletions cookiecutter/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import subprocess
import sys

from .utils import make_sure_path_exists, work_in

_HOOKS = [
'pre_gen_project',
'post_gen_project',
# TODO: other hooks should be listed here
]


def find_hooks():
'''
Must be called with the project template as the current working directory.
Expand Down Expand Up @@ -60,10 +59,11 @@ def _run_hook(script_path, cwd='.'):
)
proc.wait()


def run_hook(hook_name, project_dir):
'''
Try and find a script mapped to `hook_name` in the current working directory,
and execute it from `project_dir`.
Try and find a script mapped to `hook_name` in the current working
directory, and execute it from `project_dir`.
'''
script = find_hooks().get(hook_name)
if script is None:
Expand Down
11 changes: 8 additions & 3 deletions cookiecutter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def cookiecutter(input_dir, checkout=None, no_input=False, extra_context=None):
no_input=no_input
)
else:
# If it's a local repo, no need to clone or copy to your cookiecutters_dir
# If it's a local repo, no need to clone or copy to your
# cookiecutters_dir
repo_dir = input_dir

context_file = os.path.join(repo_dir, 'cookiecutter.json')
Expand Down Expand Up @@ -123,7 +124,9 @@ def _get_parser():
'-c', '--checkout',
help='branch, tag or commit to checkout after git clone'
)
cookiecutter_pkg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
cookiecutter_pkg_dir = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
parser.add_argument(
'-V', '--version',
help="Show version information and exit.",
Expand All @@ -142,6 +145,7 @@ def _get_parser():

return parser


def parse_cookiecutter_args(args):
""" Parse the command-line arguments to Cookiecutter. """
parser = _get_parser()
Expand All @@ -154,7 +158,8 @@ def main():
args = parse_cookiecutter_args(sys.argv[1:])

if args.verbose:
logging.basicConfig(format='%(levelname)s %(filename)s: %(message)s', level=logging.DEBUG)
logging.basicConfig(format='%(levelname)s %(filename)s: %(message)s',
level=logging.DEBUG)
else:
# Log info and above to console
logging.basicConfig(
Expand Down
1 change: 1 addition & 0 deletions cookiecutter/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
PY3 = sys.version > '3'
if PY3:
iteritems = lambda d: iter(d.items())

def read_response(prompt=''):
"""
Prompt the user for a response.
Expand Down
1 change: 0 additions & 1 deletion cookiecutter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import errno
import logging
import os
import sys
import stat
import shutil
import contextlib
Expand Down
12 changes: 8 additions & 4 deletions cookiecutter/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def prompt_and_delete_repo(repo_dir, no_input=False):
if no_input:
ok_to_delete = True
else:
ok_to_delete = query_yes_no("You've cloned {0} before. "
ok_to_delete = query_yes_no(
"You've cloned {0} before. "
"Is it okay to delete and re-clone it?".format(repo_dir),
default="yes"
)
Expand Down Expand Up @@ -65,7 +66,8 @@ def clone(repo_url, checkout=None, clone_to_dir=".", no_input=False):
:param repo_url: Repo URL of unknown type.
:param checkout: The branch, tag or commit ID to checkout after clone.
:param clone_to_dir: The directory to clone to. Defaults to the current directory.
:param clone_to_dir: The directory to clone to.
Defaults to the current directory.
:param no_input: Suppress all user prompts when calling via API.
"""

Expand All @@ -77,7 +79,8 @@ def clone(repo_url, checkout=None, clone_to_dir=".", no_input=False):

tail = os.path.split(repo_url)[1]
if repo_type == "git":
repo_dir = os.path.normpath(os.path.join(clone_to_dir, tail.rsplit('.git')[0]))
repo_dir = os.path.normpath(os.path.join(clone_to_dir,
tail.rsplit('.git')[0]))
elif repo_type == "hg":
repo_dir = os.path.normpath(os.path.join(clone_to_dir, tail))
logging.debug('repo_dir is {0}'.format(repo_dir))
Expand All @@ -88,6 +91,7 @@ def clone(repo_url, checkout=None, clone_to_dir=".", no_input=False):
if repo_type in ["git", "hg"]:
subprocess.check_call([repo_type, 'clone', repo_url], cwd=clone_to_dir)
if checkout is not None:
subprocess.check_call([repo_type, 'checkout', checkout], cwd=repo_dir)
subprocess.check_call([repo_type, 'checkout', checkout],
cwd=repo_dir)

return repo_dir
1 change: 1 addition & 0 deletions requirements/base_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
22 changes: 16 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
[tox]
envlist = py26, py27, py33, py34, pypy
envlist = py26, py27, py33, py34, pypy, flake8

[testenv:py26]
deps = -r{toxinidir}/requirements/test_26.txt
commands = python setup.py test
commands = coverage run --source cookiecutter setup.py test

[testenv:py27, pypy]
[testenv:py27]
deps = -r{toxinidir}/requirements/test_27.txt
commands = python setup.py test
commands = coverage run --source cookiecutter setup.py test

[testenv:pypy]
deps = -r{toxinidir}/requirements/test_27.txt
commands = coverage run --source cookiecutter setup.py test

[testenv:py33]
deps = -r{toxinidir}/requirements/test_33.txt
commands = python setup.py test
commands = coverage run --source cookiecutter setup.py test

[testenv:py34]
deps = -r{toxinidir}/requirements/test_34.txt
commands = python setup.py test
commands = coverage run --source cookiecutter setup.py test

[testenv:flake8]
deps =
flake8
commands =
flake8 cookiecutter

0 comments on commit bb4606f

Please sign in to comment.