diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-24 17:36:28 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-24 17:36:28 +0200 |
commit | bc71fd978699f0c07ee37774efcfd4c624474980 (patch) | |
tree | 251d7255fdf811bb4afdc0b67d13f2e03e3f78ac | |
parent | bd4d2bf420e1dfa3298143daebd485b97335b256 (diff) | |
download | psutil-download_stats.tar.gz |
add pypistats.py scriptdownload_stats
-rw-r--r-- | MANIFEST.in | 1 | ||||
-rw-r--r-- | scripts/internal/pypistats.py | 145 |
2 files changed, 146 insertions, 0 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index 93c49180..803bc182 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -126,6 +126,7 @@ include scripts/internal/print_announce.py include scripts/internal/print_api_speed.py include scripts/internal/print_timeline.py include scripts/internal/purge_installation.py +include scripts/internal/pypistats.py include scripts/internal/tidelift.py include scripts/internal/win_download_wheels.py include scripts/internal/winmake.py diff --git a/scripts/internal/pypistats.py b/scripts/internal/pypistats.py new file mode 100644 index 00000000..d2be2d58 --- /dev/null +++ b/scripts/internal/pypistats.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2009 Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Print PYPI statistics in MarkDown format. +Requires +""" + +import datetime +import json +import os +import subprocess +import textwrap + +import pypinfo # NOQA + +from psutil._common import memoize + + +AUTH_FILE = os.path.expanduser("~/.pypinfo.json") +PKGNAME = 'psutil' +DAYS = 30 +LIMIT = 100 +GITHUB_SCRIPT_URL = "https://github.com/giampaolo/psutil/blob/master/" \ + "scripts/internal/pypistats.py" + + +@memoize +def sh(cmd): + env = os.environ.copy() + env['GOOGLE_APPLICATION_CREDENTIALS'] = AUTH_FILE + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, universal_newlines=True) + stdout, stderr = p.communicate() + if p.returncode != 0: + raise RuntimeError(stderr) + assert not stderr, stderr + return stdout.strip() + + +def top_packages(): + out = sh(f"pypinfo --all --json --days {DAYS} --limit {LIMIT} '' project") + data = json.loads(out) + return data + + +def ranking(): + data = top_packages() + for i, line in enumerate(data['rows'], 1): + if line['project'] == PKGNAME: + return i + raise ValueError(f"can't find {PKGNAME}") + + +def downloads(): + data = top_packages() + for line in data['rows']: + if line['project'] == PKGNAME: + return line['download_count'] + raise ValueError(f"can't find {PKGNAME}") + + +def downloads_pyver(): + out = sh(f"pypinfo --json --days {DAYS} {PKGNAME} pyversion") + return json.loads(out) + + +def downloads_by_country(): + out = sh(f"pypinfo --json --days {DAYS} {PKGNAME} country") + return json.loads(out) + + +def downloads_by_system(): + out = sh(f"pypinfo --json --days {DAYS} {PKGNAME} system") + return json.loads(out) + + +def downloads_by_distro(): + out = sh(f"pypinfo --json --days {DAYS} {PKGNAME} distro") + return json.loads(out) + + +def print_markdown_table(title, left, rows): + pleft = left.replace('_', ' ').capitalize() + s = textwrap.dedent(f"""\ + ### {title} + + | {pleft} | Downloads | + |:--------|---------------:|\ + """) + print(s) + for row in rows: + lval = row[left] + rval = '{0:,}'.format(row['download_count']) + print(f"| {lval} | {rval} |") + print() + + +def print_markdown(): + last_update = datetime.datetime.now().strftime("%Y-%m-%d") + s = textwrap.dedent(f"""\ + # Download stats + + psutil download statistics of the last {DAYS} days (last update + *{last_update}*). + Generated via [pypistats.py]({GITHUB_SCRIPT_URL}) script. + """) + print(s) + + per_month = '{0:,}'.format(downloads()) + per_day = '{0:,}'.format(int(downloads() / 30)) + s = textwrap.dedent(f"""\ + ### Overview + + | | Downloads | + |:--------------------------|-----------------:| + | **Per month** | {per_month} | + | **Per day** | {per_day} | + | **PYPI ranking** | {ranking()} | + + *See more [details](https://pepy.tech/project/psutil).* + """) + print(s) + + print_markdown_table('Operating systems', 'system_name', + downloads_by_system()['rows']) + + print_markdown_table('Distros', 'distro_name', + downloads_by_distro()['rows']) + + print_markdown_table('Python versions', 'python_version', + downloads_pyver()['rows']) + + print_markdown_table('Countries', 'country', + downloads_by_country()['rows']) + + +def main(): + print_markdown() + + +main() |