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.
170 lines
5.9 KiB
Python
170 lines
5.9 KiB
Python
#!/usr/bin/python3
|
|
"""
|
|
lark
|
|
|
|
Verify and sort game ROM images.
|
|
"""
|
|
# TODO: Write decent UI
|
|
import hashlib
|
|
import sys
|
|
import os
|
|
import uuid
|
|
import xdg.BaseDirectory
|
|
|
|
#import dat
|
|
import metadata
|
|
|
|
HASH_CHUNK_SIZE = 10485760 # 10mb
|
|
SQLITE_FILENAME = 'metadata.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'
|
|
|
|
def _kwargs_parse(kwargs_list):
|
|
kwargs = {}
|
|
for kwarg_string in kwargs_list:
|
|
key, value = kwarg_string.split('=')
|
|
kwargs[key] = value
|
|
|
|
return kwargs
|
|
|
|
action_object = sys.argv[1]
|
|
action = sys.argv[2]
|
|
|
|
metadata.configure(os.path.join(data_path, SQLITE_FILENAME))
|
|
|
|
# TODO: Use a real UI library. This mess is just intended for development.
|
|
if action_object == 'platform':
|
|
if action == 'add':
|
|
print('add a platform')
|
|
platform_shortcode = sys.argv[3]
|
|
platform_name = sys.argv[4]
|
|
|
|
platform_data = metadata.Platform(shortcode=platform_shortcode,
|
|
fullname=platform_name)
|
|
with metadata.get_db_session() as session:
|
|
session.add(platform_data)
|
|
|
|
elif action == 'list':
|
|
# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
|
|
# out a good way to include other filters.
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
with metadata.get_db_session() as session:
|
|
print(metadata.search(session, metadata.Platform, **filters))
|
|
|
|
elif action == 'remove':
|
|
constraints = sys.argv[3:]
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
|
|
with metadata.get_db_session() as session:
|
|
platforms = metadata.search(session, metadata.Platform, **filters)
|
|
for platform in platforms:
|
|
print('Removing %s.' % platform.fullname)
|
|
session.delete(platform)
|
|
|
|
elif action == 'test':
|
|
# TODO: Delete this action before merging into dev. It's just for ugly testing.
|
|
platform_shortcode = sys.argv[3]
|
|
platform_name = sys.argv[4]
|
|
|
|
platform_data = metadata.Platform(shortcode=platform_shortcode,
|
|
fullname=platform_name)
|
|
|
|
with metadata.get_db_session() as session:
|
|
print(metadata.search(session, metadata.Platform))
|
|
|
|
elif action_object == 'release-group':
|
|
if action == 'add':
|
|
properties = _kwargs_parse(sys.argv[3:])
|
|
release_group = metadata.ReleaseGroup(name=properties['name'])
|
|
|
|
with metadata.get_db_session() as session:
|
|
if properties['platform']:
|
|
platform = metadata.search(session, metadata.Platform,
|
|
shortcode=properties['platform'])[0]
|
|
release_group.platform = platform
|
|
session.add(release_group)
|
|
|
|
if action == 'list':
|
|
# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
|
|
# out a good way to include other filters.
|
|
print('Listing release groups.')
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
with metadata.get_db_session() as session:
|
|
print(metadata.search(session, metadata.ReleaseGroup, **filters))
|
|
|
|
elif action == 'remove':
|
|
constraints = sys.argv[3:]
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
|
|
with metadata.get_db_session() as session:
|
|
release_groups = metadata.search(session, metadata.ReleaseGroup, **filters)
|
|
for release_group in release_groups:
|
|
print('Removing %s.' % release_group.name)
|
|
session.delete(release_group)
|
|
|
|
elif action_object == 'release':
|
|
if action == 'add':
|
|
properties = _kwargs_parse(sys.argv[3:])
|
|
release = metadata.Release(**properties)
|
|
|
|
with metadata.get_db_session() as session:
|
|
if properties['release-group']:
|
|
release_group = metadata.search(session, metadata.ReleaseGroup,
|
|
name=properties['release-group'])[0]
|
|
release.release_group = release_group
|
|
session.add(release)
|
|
|
|
if action == 'list':
|
|
# TODO: Filter support is exclusively limited to SQLAlchemy's filter.ilike function. Figure
|
|
# out a good way to include other filters.
|
|
print('Listing releases.')
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
with metadata.get_db_session() as session:
|
|
print(metadata.search(session, metadata.Release, **filters))
|
|
|
|
elif action == 'remove':
|
|
constraints = sys.argv[3:]
|
|
filters = _kwargs_parse(sys.argv[3:])
|
|
|
|
with metadata.get_db_session() as session:
|
|
release_groups = metadata.search(session, metadata.Release, **filters)
|
|
for release in release_groups:
|
|
print('Removing %s.' % release.name)
|
|
session.delete(release)
|
|
else:
|
|
print('Unknown object.')
|