Skip to content

Comments

Show why package is required#2086

Closed
shenek wants to merge 2 commits intopython-poetry:developfrom
shenek:cli-why
Closed

Show why package is required#2086
shenek wants to merge 2 commits intopython-poetry:developfrom
shenek:cli-why

Conversation

@shenek
Copy link

@shenek shenek commented Feb 25, 2020

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Add a flag --why to poetry show. User should see a filtered output why the package is required.
Example (run on poetry repo):

$ poetry show --tree --why pluggy
pluggy 0.13.1 plugin and hook calling mechanisms for python
pytest 4.6.9 pytest: simple powerful testing with Python
└── pluggy >=0.12,<1.0
tox 3.12.1 tox is a generic virtualenv management and test command line tool
└── pluggy >=0.3.0,<1
tox 3.14.3 tox is a generic virtualenv management and test command line tool
└── pluggy >=0.12.0,<1
pytest-cov 2.8.1 Pytest plugin for measuring coverage.
└── pytest >=3.6
    └── pluggy >=0.12,<1.0 
pytest-mock 1.13.0 Thin-wrapper around the mock package for easier use with py.test
└── pytest >=2.7
    └── pluggy >=0.12,<1.0 
pytest-sugar 0.9.2 pytest-sugar is a plugin for pytest that changes the default look and feel of pytest (e.g. progressbar, show tests that fail instantly).
└── pytest >=2.9
    └── pluggy >=0.12,<1.0 

or

$ poetry show --why pluggy
pluggy       0.13.1 plugin and hook calling mechanisms for python
pytest       4.6.9  pytest: simple powerful testing with Python
tox          3.12.1 tox is a generic virtualenv management and test command line tool
tox          3.14.3 tox is a generic virtualenv management and test command line tool
pytest-cov   2.8.1  Pytest plugin for measuring coverage.
pytest-mock  1.13.0 Thin-wrapper around the mock package for easier use with py.test
pytest-sugar 0.9.2  pytest-sugar is a plugin for pytest that changes the default look and feel of pytest (e.g. progressbar, show tests that fail ...

closes: #1906

@shenek shenek changed the base branch from master to develop February 25, 2020 21:24
@shenek shenek changed the title Cli why Show why package is required Feb 25, 2020
@finswimmer finswimmer requested a review from a team February 26, 2020 05:08
@finswimmer finswimmer added the kind/feature Feature requests/implementations label Feb 26, 2020
@abn abn added the area/cli Related to the command line label Apr 1, 2020
@jessekrubin
Copy link

This is dope

@snejus
Copy link
Contributor

snejus commented Apr 20, 2020

Excited!

@sdispater
Copy link
Member

sdispater commented Apr 20, 2020

Thanks for your contribution!

Overall, I am on board with the idea :-)

However, I think, as it is, the output is not clear enough.

For the "non-tree" display, we could output complete sentences that describe why the package is required.

For the "tree" display, we could highlight the required package in the tree for better visibility.

Here is a proof of concept:

Screenshot 2020-04-20 at 15 22 10

@seansfkelley
Copy link

This looks awesome, and I would love to have it!

Re: @sdispater's suggestion, I can take or leave the phrasing or the highlighting; I think the meaning of the output is already quite clear given the context. However, I'd like to note that (1) I like that it specifies the root project as a direct dependent when applicable and (2) specified version ranges are more useful than resolved versions in many cases, but showing both seems good too.

Why does tox appear twice in the example?

shenek added 2 commits May 2, 2020 22:42
* extends show command with `--why` flag
* with `poetry show --why <package>` it displays a list of packages
  which need <package> to be installed (including indirect deps)
* with `poetry show --tree --why <package>` it displays a tree of
  packages which need <package> to be installed (including indirect deps)
@shenek
Copy link
Author

shenek commented May 2, 2020

Hi,

@sdispater
I added the highlighting and the descriptions.

@seansfkelley
The resolved versions in the tree view seems like a good idea. However this PR is about adding --why option. And I want to touch the tree display logic as little as possible. Perhaps it can be a part of other Issue/PR.

Cheers

@seansfkelley
Copy link

That makes sense; the formatting changes can be a different change. I didn't know --tree existed and I've never used it, so I didn't realize that the formatting of the version specifiers was existing behavior.

@echernyavskiy
Copy link

Maintainers, could this be merged and released please?

@snejus
Copy link
Contributor

snejus commented Nov 19, 2020

If anyone's looking for a solution, I have a couple of helpers that parse the poetry.lock for both requires and required by dependencies:

image

It shows the output more or less instantly

./deps pytest  0.07s user 0.02s system 196% cpu 0.045 total        
poetry show pytest  2.35s user 0.10s system 99% cpu 2.456 total

Feel free to use it! Best to chmod u+x this-file and move it to ~/.local/bin. It works with poetry 1.1.3 lock files and I've also read 1.0.10 with it I think.

_RED='\\\\e[1;31m&\\\\e[0m'
_GREEN='\\\\e[1;32m&\\\\e[0m'
_YELLOW='\\\\e[1;33m&\\\\e[0m'
_format () {
    tr -d '"' |
        sed "s/ \+>[^ ]* \+<.*/$_YELLOW/" | # ~ / ^ / < >= ~ a window
        sed "s/ \+>[^ ]* *$/$_GREEN/" |     # >= no upper limit
        sed "/>/ !s/<.*$/$_RED/" |          # < ~ upper limit
        sed "/>\|</ !s/ .*/  $_RED/"        # == ~ locked version
}

_requires () {
    sed -n "/^name = \"$1\"/I,/\[\[package\]\]/{
                /\[package.dep/,/^$/{
                    /^[^[]/ {
                        s/= {version = \(\"[^\"]*\"\).*/, \1/p;
                        s/ =/,/gp
             }}}" poetry.lock |
        sed "/,.*,/!s/</,</; s/^[^<]\+$/&,/" |
        column -t -s , | _format
}

_required_by () {
    sed -n "/\[metadata\]/,//d;
            /\[package\]\|\[package\.depen/,/^$/H;
            /^name\|^$1 = /Ip" poetry.lock |
        sed -n "/^$1/I{x;G;p};h" |
        sed 's/.*"\(.*\)".*/\1/' |
        sed '$!N;s/\n/ /' |
        column -t | _format
}

deps() {
    echo
    echo -e "\e[1mREQUIRES\e[0m"
    _requires "$1" | xargs -i echo -e "\t{}"
    echo
    echo -e "\e[1mREQUIRED BY\e[0m"
    _required_by "$1" | xargs -i echo -e "\t{}"
    echo
}

deps $1

@Secrus
Copy link
Member

Secrus commented May 17, 2022

--why option is already present in master branch. Closing.

@Secrus Secrus closed this May 17, 2022
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area/cli Related to the command line kind/feature Feature requests/implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Display why package was installed

9 participants