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

add 'info' command to cli #959

Merged
merged 5 commits into from
Mar 5, 2024
Merged

add 'info' command to cli #959

merged 5 commits into from
Mar 5, 2024

Conversation

con-f-use
Copy link
Contributor

@con-f-use con-f-use commented Oct 14, 2023

Adds an info command to the setuptools_scm command line. This info command can be used to print settings, like so:

$ python -m setuptools_scm info dist_name parent root relative_to version_scheme local_scheme tag_regex
dist_name = setuptools-scm
parent = /home/jan/devel/python/setuptools_scm
root = .
relative_to = /home/jan/devel/python/setuptools_scm/pyproject.toml
version_scheme = guess-next-dev
local_scheme = node-and-date
tag_regex = re.compile('^(?:[\\w-]+-)?(?P<version>[vV]?\\d+(?:\\.\\d+){0,2}[^\\+]*)(?:\\+.*)?$')

This is only a proof of concept implementation, I could work on tests, stabilization and a proper implementation if that was desired.

More details and examples:

$ python -m setuptools_scm --help
usage: python -m setuptools_scm [-h] [-r ROOT] [-c PATH] [--strip-dev]  ...

Print project version according to SCM metadata

options:
  -h, --help            show this help message and exit
  -r ROOT, --root ROOT  directory managed by the SCM, default: inferred from config file, or "."
  -c PATH, --config PATH
                        path to 'pyproject.toml' with setuptools_scm config, default: looked up in the current or parent directories
  --strip-dev           remove the dev/local parts of the version before printing the version

extra commands:
  
    ls                  list files managed by the SCM
    info                display setuptools_scm settings according to query e.g. dist_name

$ python -m setuptools_scm info --help
usage: python -m setuptools_scm info [-h] [query ...]

Display setuptools_scm settings according to query e.g. dist_name

positional arguments:
  query       query string(s) for the desired information, omit for list of valid query strings

options:
  -h, --help  show this help message and exit

$ python -m setuptools_scm info
relative_to
root
version_scheme
local_scheme
tag_regex
parentdir_prefix_version
fallback_version
fallback_root
write_to
write_to_template
version_file
version_file_template
parse
git_describe_command
dist_name
version_cls
search_parent_directories
parent

@con-f-use con-f-use marked this pull request as draft October 14, 2023 10:10
@con-f-use
Copy link
Contributor Author

con-f-use commented Oct 14, 2023

Note: The current implementation does not print the version anymore, when the ls command is used. I'm not sure if the old behavior was intentional or a bug.

I also noticed, that invoking python -m setuptools_scm ... does not honor the projects build-system requirements (how would it?). I.e. if a project uses a plugin that provides, e.g., a custom version scheme, python -m setuptools_scm will error because it is not aware of that scheme until that plugin is installed in the environment the invocation takes place in.

One might also want to restrict the valid queries, filtering out the rather unuseful keywords.

Copy link
Contributor

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The move up and early return skip's the version printing

I recommend reordering the logic

I'm currently not sure on the details and won't be able to try it's on a computer soon due to traveling

@con-f-use
Copy link
Contributor Author

con-f-use commented Oct 14, 2023

No worries, take your time. I'm glad you even look at it at all.

The move is intentional, because I thought that behavior for ls might be a bug. If one were to pipe or parse the output of ls, one would have to awkwardly filter out the first line. I would expect ls to only print files.

In short, the code in this PR has the following intended behavior:

  • python -m setuptools_scm --> prints the version
  • python -m setuptools_scm ls --> does NOT print the version, only the files found
  • python -m setuptools_scm info ... --> does NOT print the version, only the desired settings fields

But if you think it would break that much legacy, I'm not married to that.

@RonnyPfannschmidt
Copy link
Contributor

its not that this breaks legacy, its just that the change would require a major release

so i'm under the impression a little extra complexity to have a ls-only would help that

@con-f-use
Copy link
Contributor Author

con-f-use commented Oct 18, 2023

Sorry, not sure what you mean. Do you want me to change ls so it still prints the version and then have something we could call e.g. list or files that does the same without version printing first?

@RonnyPfannschmidt
Copy link
Contributor

correct

i'm also wondering about whether listing files, versions and info should be flags instead of commands

(i'm imagining something like python -m setuptools_scm --list-files --show-version --show-info -o json)

@con-f-use
Copy link
Contributor Author

I'm not averse to doing that. One could also think of re-writing the cli in something like click. Would you be averse to that and the extra dependency?

@RonnyPfannschmidt
Copy link
Contributor

unfortunately not - it can only be a extra dependency , never a mandatory one

the argparse setup should get simpler if the new options are enable/disable flags
i propose leaving the "ls" command as is, and adding the opt in/out for the info/version as a starting point thats quicky merged

then in a followup a json output style where each parameter can be asked for can be added

Copy link
Contributor

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry this was hanging for a while

looks good 👍

please take a look at the type nitpicks and add a scriv fragment to the changelog

src/setuptools_scm/_cli.py Outdated Show resolved Hide resolved
src/setuptools_scm/_cli.py Outdated Show resolved Hide resolved
print("\n".join(data.values()))


def _print_key_value(data: dict[Any, Any]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dito

@RonnyPfannschmidt RonnyPfannschmidt merged commit 1bac9b5 into pypa:main Mar 5, 2024
18 checks passed
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.

None yet

2 participants