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

bare cruft installs click at > 8.0.4, then syntax errors on cruft create b/c of #6

Merged
merged 19 commits into from
Sep 15, 2022

Conversation

sierra-moxon
Copy link
Member

@sierra-moxon sierra-moxon commented Sep 15, 2022

"__project_slug": "{{ cookiecutter.project_name|lower()|replace(' ', '_')|replace('-', '_') }}",

generates an error in cruft create if the version of click installed > 8.0.4. (I am trying a bare install of cruft in prep for linkml tutorial, as a test of the recipe).

I am not quite sure how to make this a requirement. For now I just added it to the README we want to go back to a slightly less helpful rewrite rule here and just fail the project creation if the naming convention is not quite right?

Another option is to have the user wrap the project creation in a virtual environment via instructions in the README.

@sierra-moxon
Copy link
Member Author

sierra-moxon commented Sep 15, 2022

The original error I had pointed to syntax error in the simple command to replace the dashes with underscores in a project name...a little debugging took me here.

% pip install typer==0.4.0 # this is an install I had by default
% pip install cruft # this brings in click 8.1.3, cruft 2.11.1, cookiecutter 2.1.1
% cruft create https://github.com/linkml/linkml-project-cookiecutter
Traceback (most recent call last):
  File "/Users/SMoxon/.pyenv/versions/3.9.7/bin/cruft", line 5, in <module>
    from cruft._cli import app
  File "/Users/SMoxon/.pyenv/versions/3.9.7/lib/python3.9/site-packages/cruft/__init__.py", line 6, in <module>
    from cruft._commands import check, create, diff, link, update
  File "/Users/SMoxon/.pyenv/versions/3.9.7/lib/python3.9/site-packages/cruft/_commands/__init__.py", line 2, in <module>
    from .check import check
  File "/Users/SMoxon/.pyenv/versions/3.9.7/lib/python3.9/site-packages/cruft/_commands/check.py", line 5, in <module>
    import typer
  File "/Users/SMoxon/.pyenv/versions/3.9.7/lib/python3.9/site-packages/typer/__init__.py", line 12, in <module>
    from click.termui import get_terminal_size as get_terminal_size
ImportError: cannot import name 'get_terminal_size' from 'click.termui' (/Users/SMoxon/.pyenv/versions/3.9.7/lib/python3.9/site-packages/click/termui.py)

two things fix it:

% pip uninstall click
% pip uninstall cruft
% pip uninstall typer
% pip install cruft 

updates typer to 0.6.1

or:

% pip uninstall click
% pip uninstall cruft
% pip install click==8.0.4
% pip install cruft

keeps typer at 0.4.0 but pins click at 8.0.4

both allow the project creation to complete as expected.
Even if we don't change anything here, we can at least have this as a reference in case it happens for others in the tutorial.

@hrshdhgd
Copy link
Collaborator

hrshdhgd commented Sep 15, 2022

Typically I've started a cookiecutter template in a fresh virtual environment with maybe just poetry installed. I've never used typer. Hence these questions (apologies in advance if these are naive):

  1. Is there a reason typer needs to be anchored at 0.4.0?
  2. As you mentioned, after updating typer to the latest version (0.6.1), this issue does not exist. With that being the case we can go ahead with the project generation via cookiecutter. Would this be acceptable or potentially have issues down the line?
  3. Would you expect it to be common for a cookiecutter project to be initiated in an existing virtual environment and not freshly generated for the new project?

As for the naming convention goes, I've just implemented 2 rules :

  1. Lowercase the project name
  2. Replace spaces and hyphens in project names by underscores and display a warning to notify user when this is done.

In other cases the process should automatically fail right at the beginning if the project name is unusual.

@sierra-moxon
Copy link
Member Author

Yep, your comment makes perfect sense :). Ultimately, this PR just changes the README instructions for initializing a new project. I'm always going to make a virtual environment when I start a new project; but I was testing the README for new users that may or may not have the background to do so (in particular, I'm setting up a recipe for a LinkML tutorial). Since README didn't tell me to make the virtual environment, I tried doing it without. I pushed a change to the README to include virtual environment generation and a guide to doing so. (I'm going to hold off on merging until I get a successful initiation - I'm still getting errors).

@hrshdhgd
Copy link
Collaborator

hrshdhgd commented Sep 15, 2022

That makes complete sense and I can't thank you enough for doing this! Please let me know if I can help.

@sierra-moxon
Copy link
Member Author

I can not get this to work, even from a clean poetry virtual shell without specifying that click stay pinned at 8.0.4.
here's my recipe:
python 3.9.7 installed via pyenv

% mkdir cookies
% cd cookies
% poetry init
% add cruft as a dependency (use the latest version by default)
% poetry install
% poetry show # click gets installed by cruft with 8.1.3, typer gets installed at 0.6.1
% poetry shell
% cruft create https://github.com/linkml/linkml-project-cookiecutter # use defaults for everything
github_token_for_pypi_deployment [PYPI_PASSWORD (leave blank if not a python project)]: 
  File "/var/folders/2d/1xhwsfs15qb37bv4m62xn0tc0000gp/T/tmp3nm58nwv.py", line 10
    project_slug = '{{ cookiecutter.project_name|lower()|replace(' ', '_')|replace('-', '_') }}'
                                                                       ^
SyntaxError: invalid syntax
Stopping generation because post_gen_project hook script didn't exit successfully
Traceback (most recent call last):
  File "/usr/local/bin/cruft", line 8, in <module>
    sys.exit(app())
  File "/usr/local/lib/python3.9/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/typer/main.py", line 497, in wrapper
    return callback(**use_params)  # type: ignore
  File "/usr/local/lib/python3.9/site-packages/cruft/_cli.py", line 104, in create
    _commands.create(
  File "/usr/local/lib/python3.9/site-packages/cruft/_commands/utils/__init__.py", line 14, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/cruft/_commands/create.py", line 45, in create
    generate_files(
  File "/usr/local/lib/python3.9/site-packages/cookiecutter/generate.py", line 361, in generate_files
    _run_hook_from_repo_dir(
  File "/usr/local/lib/python3.9/site-packages/cookiecutter/generate.py", line 237, in _run_hook_from_repo_dir
    run_hook(hook_name, project_dir, context)
  File "/usr/local/lib/python3.9/site-packages/cookiecutter/hooks.py", line 131, in run_hook
    run_script_with_context(script, project_dir, context)
  File "/usr/local/lib/python3.9/site-packages/cookiecutter/hooks.py", line 115, in run_script_with_context
    run_script(temp.name, cwd)
  File "/usr/local/lib/python3.9/site-packages/cookiecutter/hooks.py", line 86, in run_script
    raise FailedHookException(
cookiecutter.exceptions.FailedHookException: Hook script failed (exit status: 1)

Alternatively, this recipe works:

% mkdir cookies
% cd cookies
% poetry init
% add click as a dependency (pin to 8.0.4)
% add cruft as a dependency (use the latest version by default)
% poetry install
% poetry show # click gets installed by cruft with 8.0.4, typer gets installed at 0.6.1
% poetry shell
% cruft create https://github.com/linkml/linkml-project-cookiecutter # use defaults for everything

** PROJECT CREATION COMPLETE **

Next steps:
cd my-awesome-schema
make setup

It would be cool if someone else could try my recipe and see if it works for them?

@cmungall
Copy link
Member

there's lots of good info here should we capture it in an issue that is only closed when satisfactorily resolved?

@cmungall cmungall merged commit 7cca7b0 into main Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants