You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lark/lark

100 lines
2.9 KiB
Python

#!/usr/bin/python3
"""
lark
Verify and sort game ROM images.
Intended features:
DAT downloading
File validation
File renaming/moving
Nice Beets-inspired UI.
Release grouping (maybe, this might require another large external database)
UI notes
# Key terms
- hash Unique identifier for each ROM image.
- image ROM image, ripped from physical media.
- dat List of hashes, with associated filenames.
- platform The original hardware on which the image was intended to run.
# Verbs
- list [hash, dat, platform, image]
List items in the database.
- import [datfile, imagefile]
Process and add external items to the database.
- add [platform, hash]
Manually add items to the database.
- remove [hash, dat, platform]
Delete items from the database.
"""
# TODO: Write decent UI
import hashlib
import sys
import os
import xdg.BaseDirectory
import dat
import hashdb
HASH_CHUNK_SIZE = 10485760 # 10mb
SQLITE_FILENAME = 'lark.db'
data_path = os.path.join(xdg.BaseDirectory.xdg_data_home, 'lark')
def get_sha1sum(filename):
sha1sum = hashlib.sha1()
with open(filename, 'rb') as file_contents:
while True:
chunk = file_contents.read(HASH_CHUNK_SIZE)
if not chunk:
break
sha1sum.update(chunk)
return sha1sum.hexdigest()
'''
smd_dat = dat(SMD_DAT_PATH)
# TODO: Default to '.'
# TODO: Use a proper arg parser.
search_dir = sys.argv[1]
for filename in os.listdir(search_dir):
# TODO: Ignore or descend into directories.
# TODO: Compare hashes
file_path = os.path.abspath(os.path.join(search_dir, filename))
file_sha1 = get_sha1sum(file_path)
search_result = smd_dat.search_by_sha1(file_sha1)
if search_result:
rom_data = search_result[0]
print('File %s matches database entry for %s.' % (filename, rom_data.filename))
else:
print('File %s is not in database.' % filename)
'''
# Test code! :D
# TODO: Write test code that doesn't depend on external resources.
SMD_DAT_PATH = '/home/lumia/Downloads/Sega - Mega Drive - Genesis (20200303-035539).dat'
TEST_HASH = 'cfbf98c36c776677290a872547ac47c53d2761d6'
smd_platform= hashdb.PlatformInfo(shortcode='smd', fullname='Sega - Genesis - Megadrive',
aliases='')
db = hashdb.HashDB(os.path.join(data_path, SQLITE_FILENAME))
db.add_platform(smd_platform)
smd_dat = dat.Dat(SMD_DAT_PATH)
smd_dat.set_platform(smd_platform)
#hashes = smd_dat.read_all_hashes()
#db.add_hash_list(hashes)
smd_hashes = db.hash_search(datorigin=smd_dat.info.name)
print(len(smd_hashes))
#print(hashdb._build_sql_constraints(hashdb.SQL_OR, {'butt':'yes', 'platform':'smd'}))
#print(db.hash_search(platform='smd'))
#db.remove_platform(smd_platform)
#db.remove_dat(smd_dat.info)
print(hashdb._build_sql_constraints(True, {'sha1sum':TEST_HASH.upper()}))
print(db.hash_search(sha1sum=TEST_HASH.upper()))