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.
83 lines
3.2 KiB
Python
83 lines
3.2 KiB
Python
#!/usr/bin/python3
|
|
"""
|
|
### ps2-sync
|
|
This tool makes links to ISO files in the way that Open PS2 Loader expects. It should work for both
|
|
Samba and USB loading, but has only been tested with Samba.
|
|
|
|
This depends on hdl-dump.
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
|
|
# TODO: Replace these with user input and/or config files.
|
|
ROOT_PATH = '.'
|
|
|
|
def get_disc_info(disc_image_path):
|
|
this_disc_info = {
|
|
'gameid': None,
|
|
'disc_type': None,
|
|
'filename': None
|
|
}
|
|
|
|
# TODO: Figure out how to do hdl-dump's work in this script.
|
|
# I tried this with isoparser, but the parsing took far too long.
|
|
hdl_dump_args = ['hdl-dump', 'cdvd_info2', disc_image_path]
|
|
# hdl-dump has a weird output format, quoting items inconsistently. Fortunately, it isn't
|
|
# likely to change anytime soon.
|
|
hdl_dump_result = subprocess.run(hdl_dump_args, capture_output=True)
|
|
if hdl_dump_result.returncode == 0:
|
|
hdl_dump_output = hdl_dump_result.stdout.decode('utf-8').split(' ')
|
|
|
|
# hdl-dump prepends "dual-layer" to the disc type field instead of using DVD5/DVD9.
|
|
if hdl_dump_output[0] == 'dual-layer':
|
|
this_disc_info['gameid'] = hdl_dump_output[4][1:-1]
|
|
this_disc_info['disc_type'] = hdl_dump_output[1]
|
|
|
|
else:
|
|
this_disc_info['gameid'] = hdl_dump_output[3][1:-1]
|
|
this_disc_info['disc_type'] = hdl_dump_output[0]
|
|
|
|
this_disc_info['filename'] = os.path.basename(disc_image_path)
|
|
else:
|
|
print('File %s is not a Playstation 2 disc image.' % os.path.basename(disc_image_path))
|
|
|
|
return this_disc_info
|
|
|
|
def remove_dead_links(directory):
|
|
for link_name in os.listdir(directory):
|
|
link_path = os.path.abspath(os.path.join(directory, link_name))
|
|
if os.path.islink(link_path):
|
|
link_target = os.path.join(directory, os.readlink(link_path))
|
|
if not os.path.exists(link_target):
|
|
print('Removing dead link %s.' % link_path)
|
|
os.remove(link_path)
|
|
|
|
root_path = os.path.abspath(ROOT_PATH)
|
|
source_path = os.path.join(root_path, 'iso')
|
|
disc_repo = os.path.join(root_path, '.opl-srv')
|
|
|
|
os.makedirs(os.path.join(disc_repo, 'DVD'), exist_ok=True)
|
|
os.makedirs(os.path.join(disc_repo, 'CD'), exist_ok=True)
|
|
remove_dead_links(os.path.join(disc_repo, 'DVD'))
|
|
remove_dead_links(os.path.join(disc_repo, 'CD'))
|
|
|
|
for filename in os.listdir(source_path):
|
|
disc_path = os.path.join(source_path, filename)
|
|
disc_info = get_disc_info(disc_path)
|
|
|
|
if not disc_info['disc_type'] or not disc_info['gameid']:
|
|
print("Disc type could not be determined for %s" % disc_info['filename'])
|
|
|
|
else:
|
|
disc_link_filename = '%s.%s' % (disc_info['gameid'], disc_info['filename'])
|
|
disc_link_path = os.path.join(disc_repo, disc_info['disc_type'], disc_link_filename)
|
|
disc_rel_path = os.path.relpath(disc_path, start=os.path.dirname(disc_link_path))
|
|
|
|
if not os.path.exists(disc_link_path):
|
|
print('Adding disc %s.' % disc_info['filename'])
|
|
print('Linking to %s.' % disc_rel_path)
|
|
os.symlink(disc_rel_path, disc_link_path)
|
|
else:
|
|
print('Disc %s already linked.' % disc_info['filename'])
|