轻量级无依赖更新软件
rupdater的Python项目详细描述
水
rupdater是一个超轻量级且无依赖关系的更新程序,它提供 远程版本解析、下载并获取更新的哈希和。
安装
rupdater可以通过运行以下命令从pypi安装:
pip install rupdater
用法
首先,创建updater的新实例。它需要两个参数: 当前本地版本和远程版本数据文件的URL:
fromrupdaterimportUpdaterupdater=Updater('1.0.0a0','https://example.com/remote_version.json',use_json=True)
现在您可以通过调用check()
方法来检查更新:
ifupdater.check():print('Update found!')
检查后,您可能需要下载更新。这是可以做到的
使用download()
方法,返回类似文件的对象(字节模式)
下载的更新(存储在临时文件中)。
您应该只在上下文管理器中使用它。
withupdater.download()asupdate_file,open('update.zip','w+b')asf:f.write(update_file.read())
此示例将从update_文件读取数据并将其写入update.zip。
更复杂的逐块读/写示例:
withupdater.download()asupdate_file,open('update.zip','w+b')asf:forchunkiniter(lambda:update_file.read(1024*16),b''):f.write(chunk)
rupdater还有hash static方法,该方法接受2个必需参数和1个可选参数: 类文件对象、散列算法(字符串)和块大小,即 默认为1024*16。下面是用法示例:
withupdater.download()asupdate_file:hashes_match=Updater.hash_file(update_file,updater.hash_algo)==updater.hash
警告!如果要重用更新文件,必须seek(0)
。示例:
withupdater.download()asupdate_file,open('update.zip','w+b')asf:forchunkiniter(lambda:update_file.read(1024*16),b''):f.write(chunk)f.seek(0)assertUpdater.hash_file(f,updater.hash_algo)==updater.hash
您还可以通过调用get_version_data()
再次手动解析版本数据。
便携性
rupdater.py
目前绝对独立于任何事物。
python标准库是唯一的需求。所以如果你需要,
您只需复制此文件并像单个模块一样使用它。
异常处理
rupdater不会发现任何错误,你必须按照自己的意愿去做。 此外,当输入数据不正确时,rupdater可能会产生值错误。 (版本数据文件格式不正确,哈希算法未知等)。