Last active
October 15, 2023 21:12
-
-
Save pwwang/879966128b0408c2459eb0a0b413fa69 to your computer and use it in GitHub Desktop.
Check the information for a python package
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
# ~/.config/fish/functions/whichpy.fish | |
function whichpy --description 'Print python package information' | |
set -l whichpy_script_url "https://gist.githubusercontent.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/01fdd16e690a015aaa1015edf8874a6986bd33af/whichpy.py" | |
set -l whichpy_script /tmp/whichpy.py | |
if not test -f "$whichpy_script" | |
echo "Downloading: $whichpy_script_url" | |
command curl -sS "$whichpy_script_url" -o "$whichpy_script" | |
end | |
if test (count $argv) -eq 0 | |
echo "Usage: whichpy <package> [python]" | |
return 1 | |
end | |
if test (count $argv) -gt 1 | |
set python $argv[2] | |
else | |
set python "python" | |
end | |
echo "Running: $python $whichpy_script $argv[1]" | |
command $python $whichpy_script $argv[1] | |
end |
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
import sys | |
import json | |
from urllib.request import urlopen | |
from datetime import datetime | |
try: | |
im = __import__('importlib.metadata', fromlist=['metadata']) | |
from importlib.metadata import distribution, packages_distributions # noqa | |
except ImportError: | |
im = __import__('importlib_metadata') | |
if len(sys.argv) < 2: | |
print('Usage: %s <package>' % sys.argv[0]) | |
sys.exit(1) | |
pkg = sys.argv[1] | |
mdnames = [] | |
for module, package in im.packages_distributions().items(): | |
if pkg in package: | |
mdnames.append(module) | |
if distribution(pkg) and pkg not in mdnames: | |
mdnames.append(pkg) | |
if mdnames: | |
libs = {} | |
for mdname in mdnames: | |
try: | |
libs[mdname] = __import__(mdname) | |
except Exception: | |
pass | |
print() | |
print('Python:') | |
print('------') | |
print(sys.executable) | |
print(sys.version) | |
print() | |
print('Entry files:') | |
print('--------------') | |
for name, lib in libs.items(): | |
print(name, ":", lib.__file__) | |
print() | |
print('Version (importlib.metadata):') | |
print('----------------------------') | |
print(im.version(pkg)) | |
print() | |
print('Version (__version__/version):') | |
print('-----------------------------') | |
for name, lib in libs.items(): | |
try: | |
print(name, ":", lib.__version__) | |
except AttributeError: | |
try: | |
print(name, ":", lib.version) | |
except AttributeError: | |
print(name, ":", 'N/A') | |
print() | |
print('Latest (PYPI):') | |
print('-------------') | |
url = f'https://pypi.python.org/pypi/{pkg}/json' | |
with urlopen(url) as req: | |
try: | |
status = req.status | |
except AttributeError: | |
status = req.code | |
if status != 200: | |
print('Failed to fetch.') | |
else: | |
def get_key(item): | |
try: | |
return datetime.fromisoformat( | |
item[1][0]['upload_time_iso_8601'].split('.')[0] | |
) | |
except Exception: | |
return datetime.fromisoformat('1970-01-01T00:00:00') | |
data = json.loads(req.read().decode()) | |
releases = data.get('releases', {}) | |
releases = sorted( | |
releases.items(), | |
reverse=True, | |
key=get_key, | |
) | |
if not releases: | |
print('N/A') | |
else: | |
print(releases[0][0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: