-
Notifications
You must be signed in to change notification settings - Fork 297
Show installed packages when running tests. #3985
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """ | ||
| Produce a listing output of the calling Python environment. | ||
|
|
||
| Combine "conda list" with a "pip list" to describe all installed packages. | ||
|
|
||
| """ | ||
| from subprocess import check_output | ||
| import beautifultable as bt | ||
|
|
||
|
|
||
| def sanitise_lines(lines, header_line_hint=None): | ||
| """ | ||
| Split string by spaces, removing leading, trailing and repeated whitespace. | ||
| Remove header lines up to the last one containing 'header_line_hint'. | ||
|
|
||
| """ | ||
| pkg_info = [] | ||
| for line in lines: | ||
| line = line.strip() | ||
| while '\t' in line: | ||
| line = line.replace('\t', ' ') | ||
| while ' ' in line: | ||
| ... | ||
| line = line.replace(' ', ' ') | ||
| line = line.split(' ') | ||
| if len(line) > 0 and line[0] not in ('', '#'): | ||
| pkg_info.append(line) | ||
|
|
||
| pkg_keys = [pkg[0] for pkg in pkg_info] | ||
| if header_line_hint is not None: | ||
| header_inds = [ind for ind, key in enumerate(pkg_keys) | ||
| if header_line_hint in key] | ||
| if len(header_inds) > 0: | ||
| pkg_info = pkg_info[header_inds[-1] + 1:] | ||
|
|
||
| result = {pkg[0]: pkg[1:] for pkg in pkg_info} | ||
| return result | ||
|
|
||
|
|
||
| def scan_env(): | ||
| """ | ||
| Get package listings from conda and pip. | ||
|
|
||
| Return: | ||
| package_names, conda_info, pip_info | ||
|
|
||
| The package names are sorted. | ||
| 'pip_info' is a dict : name --> version | ||
| 'conda_info' is a dict : name --> version, build, channel | ||
| Any package may be present in either dict, or both. | ||
|
|
||
| """ | ||
| conda_list_bytes = check_output(['conda list'], shell=True) | ||
| conda_list_lines = conda_list_bytes.decode().split('\n') | ||
| pip_list_bytes = check_output(['pip list'], shell=True) | ||
| pip_list_lines = pip_list_bytes.decode().split('\n') | ||
|
|
||
| conda_info = sanitise_lines(conda_list_lines) | ||
| pip_info = sanitise_lines(pip_list_lines, header_line_hint='---') | ||
| conda_keys = set(conda_info.keys()) | ||
| pip_keys = set([pkg[0] for pkg in pip_info]) | ||
| all_keys = sorted(set(conda_info.keys()) | set(pip_info.keys())) | ||
|
|
||
| return all_keys, conda_info, pip_info | ||
|
|
||
|
|
||
| def make_package_table(package_names, conda_info, pip_info): | ||
| """ | ||
| Turn the package info returned from 'scan_env' into a printable table. | ||
|
|
||
| """ | ||
| table = bt.BeautifulTable() | ||
| table.columns.header = [ | ||
| 'Package', 'source', 'version(s)', 'conda-version', 'conda-channel'] | ||
|
|
||
| def version_summary(package, pip_info, conda_info): | ||
| """ | ||
| Extract + combine info from pip and conda about 'package'. | ||
| Return a list of 5 strings : package, source, version, build, channel. | ||
|
|
||
| """ | ||
| pipver = pip_info[package][0] if package in pip_info else None | ||
| condaver = conda_info[package][0] if package in conda_info else None | ||
| columns = [package] + [''] * 4 | ||
| if condaver: | ||
| conda_extra_columns = conda_info[package][1:] | ||
| # Normally will get ", build, channel' | ||
| # But it seems we may need to allow for more or less. | ||
| while len(conda_extra_columns) < 2: | ||
| conda_extra_columns += [''] | ||
| if len(conda_extra_columns) > 2: | ||
| conda_extra_columns[1] = ' '.join(conda_extra_columns[1:]) | ||
| conda_extra_columns = conda_extra_columns[:2] | ||
| assert len(conda_extra_columns) == 2 | ||
| columns[3:] = conda_extra_columns | ||
| if pipver is not None and condaver is None: | ||
| source, version = "pip", pipver | ||
| elif pipver is None and condaver is not None: | ||
| source, version = "conda", condaver | ||
| else: | ||
| if (pipver == condaver): | ||
| source, version = "both", pipver | ||
| else: | ||
| source, version = '**CONFLICT**', f'pip={pipver} conda={condaver}' | ||
| if 'CONFLICT' not in source: | ||
| version = '= ' + version | ||
| columns[1:3] = [source, version] | ||
| return columns | ||
|
|
||
| for key in package_names: | ||
| table.rows.append(version_summary(key, | ||
| pip_info=pip_info, | ||
| conda_info=conda_info)) | ||
|
|
||
| # Pre-style the table output. | ||
| table.maxwidth = 9999 | ||
| table.columns.alignment = bt.ALIGN_LEFT | ||
| table.set_style(bt.STYLE_COMPACT) | ||
| return table # Ready to print | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| env_info = scan_env() | ||
| table = make_package_table(*env_info) | ||
| print(str(table)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pp-mo Personally, I'd opt to add the following 3 line
conda_scriptin.cirrus-ciinstead: