Skip to content

Instantly share code, notes, and snippets.

@ncatlin
Last active March 1, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncatlin/52a18a4e2b1b17e9dd16fc9b1f5aa8ca to your computer and use it in GitHub Desktop.
Save ncatlin/52a18a4e2b1b17e9dd16fc9b1f5aa8ca to your computer and use it in GitHub Desktop.
fix tmp path and remove it at the end
#!/usr/bin/python3
import re, tempfile, os, hashlib
from urllib.request import urlopen,urlretrieve
RELEASE = "4.1.1"
RELEASES_PATH = "https://github.com/navcoin/navcoin-core/releases"
#get release page data
def get_release_page_data(releaseString):
release_url = "%s/tag/%s"%(RELEASES_PATH,RELEASE)
htmlobj = urlopen(release_url)
htmldata = htmlobj.read().decode('UTF-8')
return htmldata
def remove_tags(item):
return re.sub("<.*?>", "", item)
#get hashes for each filename
def get_releasepage_filehashes(htmlData):
hashBlob = re.search("SHA256 Hashes([.\w>\-<\/\r\n]*)", htmlData)
hashLines = hashBlob.group(0).split('\n')[1:-1]
hashLines = [remove_tags(x) for x in hashLines]
fileHashes = {}
it = iter(hashLines)
for item in it:
fileHashes[item] = next(it)
return fileHashes
def hash_file(filepath):
h = hashlib.sha256()
with open(filepath, 'rb', buffering=0) as f:
for b in iter(lambda : f.read(128*1024), b''):
h.update(b)
return h.hexdigest()
def validate_hashes(hashes):
passes, fails = 0,0
workingDir = tempfile.mkdtemp()
downloadsUrl = "%s/download/%s/"%(RELEASES_PATH,RELEASE)
for file,hash in hashes.items():
downloadPath = "%s//%s"%(workingDir,file)
fileUrl = "%s%s"%(downloadsUrl,file)
print("Downloading file %s for hashing..."%(fileUrl))
urlretrieve(fileUrl, downloadPath)
actualHash = hash_file(downloadPath)
print("File: %s\n\tGithub Hash: %s\n\tActual Hash: %s"%(file,hash,actualHash))
os.remove(downloadPath)
if hash == actualHash:
passes += 1
print("Hash matches\n")
else:
fails += 1
print("HASH DOES NOT MATCH!\n")
os.rmdir(workingDir)
return passes, fails
releaseData = get_release_page_data(RELEASE)
hashes = get_releasepage_filehashes(releaseData)
passes,fails = validate_hashes(hashes)
print("Hash results for %d files"%(passes+fails))
print("------------")
print("Passes: %d"%passes)
print("Fails: %d"%fails)
if fails == 0:
print("All files are good")
else:
print("WARNING! Hash mismatches detected. Everybody panic! Sell sell sell!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment