Skip to content

Commit

Permalink
retro: Move verify_hash to data
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicki Pfau committed Sep 10, 2018
1 parent 5ab8acf commit 386e8d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
19 changes: 18 additions & 1 deletion retro/data/__init__.py
@@ -1,5 +1,6 @@
from retro._retro import GameDataGlue, RetroEmulator, data_path as _data_path
import glob
import hashlib
import json
import os
import sys
Expand Down Expand Up @@ -349,7 +350,6 @@ def parse_smd(header, body):


def groom_rom(rom):
import hashlib
with open(rom, 'rb') as r:
if rom.lower().endswith('.smd'):
# Read Super Magic Drive header
Expand All @@ -368,6 +368,23 @@ def groom_rom(rom):
return body, hashlib.sha1(body).hexdigest()


def verify_hash(game, inttype=Integrations.DEFAULT):
import retro
errors = []
rom = get_romfile_path(game, inttype=inttype)
system = retro.get_romfile_system(rom)
with open(retro.data.get_file_path(game, 'rom.sha', inttype=inttype | retro.data.Integrations.STABLE)) as f:
expected_shas = f.read().strip().split('\n')
with open(rom, 'rb') as f:
if system == 'Nes':
# Chop off header for checksum
f.read(16)
real_sha = hashlib.sha1(f.read()).hexdigest()
if real_sha not in expected_shas:
errors.append((game, 'sha mismatch'))
return errors


def merge(*args, quiet=True):
import retro
known_hashes = {}
Expand Down
16 changes: 0 additions & 16 deletions retro/testing/tools.py
Expand Up @@ -198,22 +198,6 @@ def verify_default_state(game, inttype, raw=None):
return [], errors


def verify_hash(game, inttype):
errors = []
rom = retro.data.get_romfile_path(game, inttype=inttype)
system = retro.get_romfile_system(rom)
with open(retro.data.get_file_path(game, 'rom.sha', inttype=inttype | retro.data.Integrations.STABLE)) as f:
expected_shas = f.read().strip().split('\n')
with open(rom, 'rb') as f:
if system == 'Nes':
# Chop off header for checksum
f.read(16)
real_sha = hashlib.sha1(f.read()).hexdigest()
if real_sha not in expected_shas:
errors.append((game, 'sha mismatch'))
return [], errors


def verify_hash_collisions():
errors = []
seen_hashes = {}
Expand Down
5 changes: 3 additions & 2 deletions tests/data/test_roms.py
@@ -1,10 +1,11 @@
from retro.testing import game, handle
import retro.data
import retro.testing.tools


def test_hash(game):
warnings, errors = retro.testing.tools.verify_hash(*game)
handle(warnings, errors)
errors = retro.data.verify_hash(*game)
handle([], errors)


def test_hash_collisions():
Expand Down

0 comments on commit 386e8d6

Please sign in to comment.