|
|
|
|
@ -1,3 +1,6 @@
|
|
|
|
|
'''
|
|
|
|
|
hashdb.py
|
|
|
|
|
'''
|
|
|
|
|
import collections
|
|
|
|
|
import hashlib
|
|
|
|
|
import sqlite3
|
|
|
|
|
@ -28,6 +31,13 @@ def get_file_sha1sum(filename):
|
|
|
|
|
return sha1sum.hexdigest()
|
|
|
|
|
|
|
|
|
|
def _build_sql_constraints(inclusive, constraints):
|
|
|
|
|
'''Build an SQL constraint clause out of a dictionary.
|
|
|
|
|
|
|
|
|
|
inclusive - If True, uses SQL AND operator, otherwise OR will be used.
|
|
|
|
|
constraints - A dictionary of arbitrary constraints
|
|
|
|
|
|
|
|
|
|
returns a tuple containing the appropriately formatted SQL string and a list of parameters
|
|
|
|
|
'''
|
|
|
|
|
if constraints == {}:
|
|
|
|
|
return ('', [])
|
|
|
|
|
|
|
|
|
|
@ -48,12 +58,13 @@ def _build_sql_constraints(inclusive, constraints):
|
|
|
|
|
return (sql_constraint_string, sql_parameter_list)
|
|
|
|
|
|
|
|
|
|
class HashDB:
|
|
|
|
|
'''Store and retrieve hash metadata from an SQLite database.'''
|
|
|
|
|
# TODO: Low-priority: Probably design this around using multiple hash algorithms eventually.
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
|
"""
|
|
|
|
|
'''
|
|
|
|
|
If db file does not exist, create it and create necessary tables.
|
|
|
|
|
Either way, create a connection and a cursor.
|
|
|
|
|
"""
|
|
|
|
|
Either way, create a connection object.
|
|
|
|
|
'''
|
|
|
|
|
# TODO: This process needs real error handling.
|
|
|
|
|
self._connection = sqlite3.connect(filename)
|
|
|
|
|
|
|
|
|
|
@ -75,7 +86,7 @@ class HashDB:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_hash(self, rom_info):
|
|
|
|
|
""" Add a hash to the database. """
|
|
|
|
|
''' Add a hash to the database. '''
|
|
|
|
|
# INSERT INTO sha1sums (sha1sum, filename, platform, datorigin);
|
|
|
|
|
with self._connection:
|
|
|
|
|
self._connection.execute('INSERT INTO sha1sums VALUES (?, ?, ?, ?)', rom_info)
|
|
|
|
|
@ -87,7 +98,7 @@ class HashDB:
|
|
|
|
|
self._connection.execute('INSERT INTO sha1sums VALUES (?, ?, ?, ?)', rom_info)
|
|
|
|
|
|
|
|
|
|
def remove_hash(self, rom_info):
|
|
|
|
|
""" Remove a hash from the database. """
|
|
|
|
|
''' Remove a hash from the database. '''
|
|
|
|
|
# DELETE FROM sha1sums WHERE sha1sum=sha1sum;
|
|
|
|
|
with self._connection:
|
|
|
|
|
self._connection.execute('DELETE FROM sha1sums WHERE sha1sum=?;', [rom_info.sha1sum])
|
|
|
|
|
@ -99,7 +110,7 @@ class HashDB:
|
|
|
|
|
self._connection.execute('DELETE FROM sha1sums WHERE sha1sum=?;', [rom_info.sha1sum])
|
|
|
|
|
|
|
|
|
|
def add_platform(self, platform_info):
|
|
|
|
|
""" Add a platform shortcode to the database. """
|
|
|
|
|
''' Add a platform shortcode to the database. '''
|
|
|
|
|
# TODO: Collisions need user input to resolve, so remove this try block later.
|
|
|
|
|
try:
|
|
|
|
|
with self._connection:
|
|
|
|
|
@ -108,11 +119,11 @@ class HashDB:
|
|
|
|
|
print('Warning: %s is already in database.' % platform_info.shortcode)
|
|
|
|
|
|
|
|
|
|
def update_platform_aliases(self, shortcode, aliases):
|
|
|
|
|
""" Change the list of aliases for a platform shortcode """
|
|
|
|
|
''' Change the list of aliases for a platform shortcode '''
|
|
|
|
|
# UPDATE platforms SET aliases=aliases WHERE shortcode=shortcode;
|
|
|
|
|
|
|
|
|
|
def remove_platform(self, platform_info):
|
|
|
|
|
""" Remove a platform and all associated DATs and hashes from the database. """
|
|
|
|
|
''' Remove a platform and all associated DATs and hashes from the database. '''
|
|
|
|
|
# DELETE FROM sha1sums WHERE platform=shortcode;
|
|
|
|
|
# DELETE FROM dats WHERE platform=shortcode;
|
|
|
|
|
# DELETE FROM platform WHERE platform=shortcode;
|
|
|
|
|
@ -130,7 +141,7 @@ class HashDB:
|
|
|
|
|
self._connection.execute('INSERT INTO platforms VALUES (?, ?, ?, ?);', dat_info)
|
|
|
|
|
|
|
|
|
|
def remove_dat(self, dat_info):
|
|
|
|
|
""" Delete a DAT and all of its' hashes from the database. """
|
|
|
|
|
''' Delete a DAT and all of its' hashes from the database. '''
|
|
|
|
|
# DELETE FROM sha1sums WHERE datorigin=name;
|
|
|
|
|
# DELETE FROM dats WHERE name=name;
|
|
|
|
|
|
|
|
|
|
|