Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cli.py to __main__.py to allow for python -m package #481

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ Network Trash Folder
Temporary Items
.apdisk

# Local Editor Config Files
.vscode/

# C extensions
*.so

# Distribution / packaging
.Python
venv/
env/
build/
develop-eggs/
Expand Down
4 changes: 2 additions & 2 deletions docs/console_script_setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ How It Works
------------

If the 'command_line_interface' option is set to ['click'] during setup, cookiecutter will
add a file 'cli.py' in the project_slug subdirectory. An entry point is added to
setup.py that points to the main function in cli.py.
add a file '__main__.py' in the project_slug subdirectory. An entry point is added to
setup.py that points to the main function in `__main__`.py.

Usage
------------
Expand Down
4 changes: 2 additions & 2 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def remove_file(filepath):
remove_file('tests/__init__.py')

if 'no' in '{{ cookiecutter.command_line_interface|lower }}':
cli_file = os.path.join('{{ cookiecutter.project_slug }}', 'cli.py')
remove_file(cli_file)
main_file = os.path.join('{{ cookiecutter.project_slug }}', '__main__.py')
remove_file(main_file)

if 'Not open source' == '{{ cookiecutter.open_source_license }}':
remove_file('LICENSE')
24 changes: 13 additions & 11 deletions tests/test_bake_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def test_bake_with_apostrophe_and_run_tests(cookies):

def test_bake_without_travis_pypi_setup(cookies):
with bake_in_temp_dir(cookies, extra_context={'use_pypi_deployment_with_travis': 'n'}) as result:
result_travis_config = yaml.load(result.project.join(".travis.yml").open())
result_travis_config = yaml.load(result.project.join(".travis.yml").open(),
Loader=yaml.FullLoader,
)
assert "deploy" not in result_travis_config
assert "python" == result_travis_config["language"]
found_toplevel_files = [f.basename for f in result.project.listdir()]
Expand Down Expand Up @@ -219,7 +221,7 @@ def test_bake_with_no_console_script(cookies):
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
found_project_files = os.listdir(project_dir)
assert "cli.py" not in found_project_files
assert "__main__.py" not in found_project_files

setup_path = os.path.join(project_path, 'setup.py')
with open(setup_path, 'r') as setup_file:
Expand All @@ -231,7 +233,7 @@ def test_bake_with_console_script_files(cookies):
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
found_project_files = os.listdir(project_dir)
assert "cli.py" in found_project_files
assert "__main__.py" in found_project_files

setup_path = os.path.join(project_path, 'setup.py')
with open(setup_path, 'r') as setup_file:
Expand All @@ -242,22 +244,22 @@ def test_bake_with_console_script_cli(cookies):
context = {'command_line_interface': 'click'}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
module_path = os.path.join(project_dir, 'cli.py')
module_name = '.'.join([project_slug, 'cli'])
module_path = os.path.join(project_dir, '__main__.py')
module_name = '.'.join([project_slug, '__main__'])
if sys.version_info >= (3, 5):
spec = importlib.util.spec_from_file_location(module_name, module_path)
cli = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cli)
__main__ = importlib.util.module_from_spec(spec)
spec.loader.exec_module(__main__)
elif sys.version_info >= (3, 3):
file_loader = importlib.machinery.SourceFileLoader
cli = file_loader(module_name, module_path).load_module()
__main__ = file_loader(module_name, module_path).load_module()
else:
cli = imp.load_source(module_name, module_path)
__main__ = imp.load_source(module_name, module_path)
runner = CliRunner()
noarg_result = runner.invoke(cli.main)
noarg_result = runner.invoke(__main__.main)
assert noarg_result.exit_code == 0
noarg_output = ' '.join(['Replace this message by putting your code into', project_slug])
assert noarg_output in noarg_result.output
help_result = runner.invoke(cli.main, ['--help'])
help_result = runner.invoke(__main__.main, ['--help'])
assert help_result.exit_code == 0
assert 'Show this message' in help_result.output
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{%- if 'no' not in cookiecutter.command_line_interface|lower %}
entry_points={
'console_scripts': [
'{{ cookiecutter.project_slug }}={{ cookiecutter.project_slug }}.cli:main',
'{{ cookiecutter.project_slug }}={{ cookiecutter.project_slug }}.__main__:main',
],
},
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from {{ cookiecutter.project_slug }} import {{ cookiecutter.project_slug }}
{%- if cookiecutter.command_line_interface|lower == 'click' %}
from {{ cookiecutter.project_slug }} import cli
from {{ cookiecutter.project_slug }} import __main__
{%- endif %}

{%- if cookiecutter.use_pytest == 'y' %}
Expand All @@ -40,10 +40,10 @@ def test_content(response):
def test_command_line_interface():
"""Test the CLI."""
runner = CliRunner()
result = runner.invoke(cli.main)
result = runner.invoke(__main__.main)
assert result.exit_code == 0
assert '{{ cookiecutter.project_slug }}.cli.main' in result.output
help_result = runner.invoke(cli.main, ['--help'])
assert '{{ cookiecutter.project_slug }}.__main__.main' in result.output
help_result = runner.invoke(__main__.main, ['--help'])
assert help_result.exit_code == 0
assert '--help Show this message and exit.' in help_result.output
{%- endif %}
Expand All @@ -66,10 +66,10 @@ def test_000_something(self):
def test_command_line_interface(self):
"""Test the CLI."""
runner = CliRunner()
result = runner.invoke(cli.main)
result = runner.invoke(__main__.main)
assert result.exit_code == 0
assert '{{ cookiecutter.project_slug }}.cli.main' in result.output
help_result = runner.invoke(cli.main, ['--help'])
assert '{{ cookiecutter.project_slug }}.__main__.main' in result.output
help_result = runner.invoke(__main__.main, ['--help'])
assert help_result.exit_code == 0
assert '--help Show this message and exit.' in help_result.output
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def main(args=None):
"""Console script for {{cookiecutter.project_slug}}."""
click.echo("Replace this message by putting your code into "
"{{cookiecutter.project_slug}}.cli.main")
"{{cookiecutter.project_slug}}.__main__.main")
click.echo("See click documentation at http://click.pocoo.org/")
return 0

Expand Down