J'aimerai proposer un ajout au paquet notepad-plus-plus, notamment au niveau de la fonction update_package.
En regardant sur le github de notepad, j'ai remarqué qu'ils fournissaient les hash des fichiers.
Pourquoi pas faire cette vérification en plus ?
Pour cela, je propose deux bouts de code assez simples :
Code : Tout sélectionner
import hashlib
# Obtention du hash du fichier fournit sur le Github
github_url = 'https://github.com/notepad-plus-plus/notepad-plus-plus/releases'
github_page = wgets(github_url)
checksum_regex = re.compile(r'([a-f0-9]{64}) ' + filenamex64)
checksum = checksum_regex.findall(github_page)[0]
# Obtention du hash calculé du fichier téléchargé
sha256_hash = hashlib.sha256()
with open(filenamex64, 'rb') as f:
for byte_block in iter(lambda: f.read(4096),b''):
sha256_hash.update(byte_block)
print(sha256_hash.hexdigest())
if sha256_hash != checksum:
print(f'Integrity check failed. The file may have been corrupted or an error occured while downloading.')
Exemple avec notepad-plus-plus :
Code : Tout sélectionner
from pkg_resources import parse_version
url = 'https://api.github.com/repos/notepad-plus-plus/notepad-plus-plus/releases/latest'
data = json.loads(wgets(url, proxies=proxies))
assets = data['assets']
filenamex64, url64 = [(str(p['name']), str(p['browser_download_url'])) for p in assets if 'Installer.x64.exe' in p['name']][0]
package_version = control.get_software_version()
latest_version = filenamex64.split('npp.',)[1].split('.Installer.x64.exe')[0]
for fileexe in glob.glob('npp.*.Installer.x64.exe'):
if parse_version(package_version) < parse_version(latest_version)
print('Delete ' +
remove_file(fileexe)
[...]
Cordialement,
Étienne