diff --git a/RetroUFO_GUI.py b/RetroUFO_GUI.py index 6dc8d08..33375fc 100644 --- a/RetroUFO_GUI.py +++ b/RetroUFO_GUI.py @@ -7,12 +7,26 @@ __author__ = "Melon Bread" __version__ = "0.8.0" __license__ = "MIT" +import argparse +import os +import platform import sys - +import zipfile +from pathlib import Path +from shutil import rmtree +from urllib.request import urlretrieve from PySide2.QtWidgets import QApplication, QDialog, QLineEdit, QComboBox, QCheckBox, QPushButton, QFileDialog, \ QVBoxLayout, QTextEdit +URL = 'https://buildbot.libretro.com/nightly' + +# These are the default core locations with normal RetroArch installs based off of 'retroarch.default.cfg` +CORE_LOCATION = { + 'linux': '{}/.config/retroarch/cores'.format(Path.home()), + 'windows': '{}/AppData/Roaming/RetroArch/cores'.format(Path.home()) +} + class Form(QDialog): def __init__(self, parent=None): @@ -43,9 +57,13 @@ class Form(QDialog): self.teditLog = QTextEdit() self.teditLog.setReadOnly(True) + + self.chkboxKeepDownload = QCheckBox('Keep Downloaded Cores') + self.chkboxKeepDownload.setChecked(False) + self.btnGrabCores = QPushButton('Grab Cores') - self.btnGrabCores.clicked.connect(self.download_cores()) + self.btnGrabCores.clicked.connect(self.grab_cores) # Create layout and add widgets layout = QVBoxLayout() @@ -55,6 +73,7 @@ class Form(QDialog): layout.addWidget(self.leditCoreLocation) layout.addWidget(self.btnCoreLocation) layout.addWidget(self.teditLog) + layout.addWidget(self.chkboxKeepDownload) layout.addWidget(self.btnGrabCores) # Set dialog layout @@ -79,8 +98,90 @@ class Form(QDialog): self.leditCoreLocation.insert(directory) - def download_cores(self): - pass + def grab_cores(self): + self.teditLog.insertPlainText('Starting UFO Grabber') + """ Where the magic happens """ + + # If a platform and/or architecture is not supplied it is grabbed automatically + platform = self.get_platform() # TODO: rename this var to prevent conflict + architecture = self.get_architecture() + location = CORE_LOCATION[platform] + + self.download_cores(platform, architecture) + self.extract_cores(location) + + if not args.keep: + self.clean_up() + + def get_platform(self): + """ Gets the Platform and Architecture if not supplied """ + + if platform.system() == 'Linux': + return 'linux' + + elif platform.system() == 'Windows' or 'MSYS_NT' in platform.system(): # Checks for MSYS environment as well + return 'windows' + else: + print('ERROR: Platform not found or supported') + sys.exit(0) + + def get_architecture(self): + """ Gets the Platform and Architecture if not supplied """ + + if '64' in platform.architecture()[0]: + return 'x86_64' + + elif '32' in platform.architecture()[0]: + return 'x86' + else: + print('ERROR: Architecture not found or supported') + sys.exit(0) + + def download_cores(self, _platform, _architecture): + """ Downloads every core to the working directory """ + + cores = [] + + # Makes core directory to store archives if needed + if not os.path.isdir('cores'): + os.makedirs("cores") + + # Downloads a list of all the cores available + urlretrieve('{}/{}/{}/latest/.index-extended'.format(URL, _platform, _architecture), + 'cores/index') + self.teditLog.insertPlainText('Obtained core index!') + + # Adds all the core's file names to a list + core_index = open('cores/index') + + for line in core_index: + file_name = line.split(' ', 2)[2:] + cores.append(file_name[0].rstrip()) + core_index.close() + cores.sort() + + # Downloads each core from the list + for core in cores: + urlretrieve('{}/{}/{}/latest/{}'.format(URL, _platform, _architecture, core), + 'cores/{}'.format(core)) + print('Downloaded {} ...'.format(core)) + + # Removes index file for easier extraction + os.remove('cores/index') + + def extract_cores(self, _location): + """ Extracts each downloaded core to the RA core directory """ + print('Extracting all cores to: {}'.format(_location)) + + for file in os.listdir('cores'): + archive = zipfile.ZipFile('cores/{}'.format(file)) + archive.extractall(_location) + print('Extracted {} ...'.format(file)) + + def clean_up(self): + """ Removes all the downloaded files """ + if os.listdir('cores'): + rmtree('cores/') if __name__ == '__main__':