简体   繁体   English

如何安全地检查python包是否过时?

[英]How can I safely check if a python package is outdated?

I want to include some kind of self-update mechanism on a python package and I do not want to call pip update before the script is run as it is very slow, I am looking for a smart mechanism. 我想在python包中包含某种自我更新机制,我不想在脚本运行之前调用pip update,因为它非常慢,我正在寻找一种智能机制。

Every time it is used it could make an HTTP call, probably to PyPi and if it would detect a new version it would output a warning on STDERR. 每次使用它都可以进行HTTP调用,可能是PyPi,如果它会检测到新版本,它会在STDERR上输出警告。

Obviously, as part of this process I would also want to cache the result of last call, so the library would not check for updates more than once a day, let say. 显然,作为此过程的一部分,我还想缓存上次调用的结果,因此库不会每天多次检查更新,比如说。

Does anyone has something like this already implemented? 有人这样的东西已经实现了吗? Do you have an example that can be used to cache results from HTTP calls, between different executions, so it would not impose signifiant delays? 您是否有一个示例可用于在不同的执行之间缓存来自HTTP调用的结果,因此它不会强加显着的延迟?

To show the outdated packages you can simply run pip list -o , but that doesn't involve any caching by itself. 要显示过时的软件包,您只需运行pip list -o ,但这本身不涉及任何缓存。

Although it would be trivial to simply add pip list -o > outdated.txt to a cronjob so it automatically updates daily :) 虽然简单地将pip list -o > outdated.txt添加到cronjob是微不足道的,所以它每天自动更新:)

Here's some example code to use pip as a library: 以下是使用pip作为库的一些示例代码:

def get_outdated():
    import pip

    list_command = pip.commands.list.ListCommand()
    options, args = list_command.parse_args([])
    packages = pip.utils.get_installed_distributions()

    return list_command.get_outdated(packages, options)

print(get_outdated())

I've written a library outdated to solve this problem. 我写了一个outdated的库来解决这个问题。 The quickest way to use it is to insert code like this somewhere at the root of your package: 使用它的最快方法是在包的根目录中插入这样的代码:

from outdated import warn_if_outdated

warn_if_outdated('my-package-name', '1.2.3')

This will output a warning on stderr as requested, cache the HTTP call for 24 hours, and more. 这将根据请求在stderr上输出警告,将HTTP调用缓存24小时等等。 Details can be found in the link above. 详情可在上面的链接中找到。

It's also much faster than pip list -o even without caching. 即使没有缓存,它也比点击pip list -o快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM