def open_epub_view(self): if filename := request.json.get("filename"): if not os.path.isfile(filename): raise HTTPError(400, f"Bad epub file: {filename}") book_uid = generate_sha1hash(filename) if book_uid not in OPENED_EPUBS: self.extract_epub( book_uid, filename, ) return {"book_uid": book_uid}
def update_version_info(c): from bookworm import app from bookworm.utils import generate_sha1hash artifacts_folder = PROJECT_ROOT / "scripts" json_file = artifacts_folder / "release-info.json" release_type = app.get_version_info()["pre_type"] or "" json_info = {release_type: {"version": app.version}} artifacts = dict( installer=artifacts_folder.glob("Bookworm*setup.exe"), update_bundle=artifacts_folder.glob("Bookworm*update.bundle"), ) for artifact_type, artifact_files in artifacts.items(): for file in artifact_files: json_info[release_type][ f"{file.name}.sha1hash"] = generate_sha1hash(file) json_file.write_text(json.dumps(json_info, indent=2)) print("Updated version information")
def update_version_info(c): from bookworm import app from bookworm.utils import generate_sha1hash artifacts_folder = PROJECT_ROOT / "scripts" json_file = artifacts_folder / "release-info.json" json_info = { "version": app.version, "updated": datetime.utcnow().isoformat(), } artifacts = sorted( itertools.chain( artifacts_folder.glob("Bookworm*setup.exe"), artifacts_folder.glob("Bookworm*portable.zip"), artifacts_folder.glob("Bookworm*update.bundle"), )) for file in artifacts: json_info[f"{file.name}.sha1hash"] = generate_sha1hash(file) json_file.write_text(json.dumps(json_info, indent=2)) print("Updated version information")
def perform_update(update_url, sha1hash): try: log.debug(f"Downloading update from: {update_url}") update_file = requests.get(update_url, stream=True) update_file.raise_for_status() except RequestException as e: log.info(f"Faild to obtain the update file. {e.args}") wx.CallAfter( wx.MessageBox, # Translators: the content of a message indicating a failure in downloading an update _("A network error was occured when trying to download the update.\n" "Make sure you are connected to the internet, " "or try again at a later time."), # Translators: the title of a message indicating a failure in downloading an update _("Network Error"), style=wx.ICON_ERROR, ) return update_file_size = int( update_file.headers.get('content-length', 20 * 1024**2)) dlg = wx.ProgressDialog( # Translators: the title of a message indicating the progress of downloading an update _("Downloading Update"), # Translators: a message indicating the progress of downloading an update bundle _("Downloading {url}:").format(url=update_url), parent=wx.GetApp().mainFrame, maximum=99, style=wx.PD_APP_MODAL | wx.PD_REMAINING_TIME | wx.PD_AUTO_HIDE) bundle = tempfile.SpooledTemporaryFile(max_size=1024 * 30 * 1000) # Translators: a message indicating the progress of downloading an update bundle update_progress = lambda c, t=update_file_size: _( "Downloading. {downloaded} MB of {total} MB").format( downloaded=round(c / (1024**2)), total=round(t / (1024**2))) csize = ceil(update_file_size / 100) for (progval, chunk) in enumerate(update_file.iter_content(chunk_size=csize)): bundle.write(chunk) downloaded = bundle.tell() wx.CallAfter(dlg.Update, progval, update_progress(downloaded)) wx.CallAfter(dlg.Hide) wx.CallAfter(dlg.Destroy) log.debug("The update bundle has been downloaded successfully.") if generate_sha1hash(bundle) != sha1hash: log.debug("Hashes do not match.") bundle.close() msg = wx.MessageBox( # Translators: the content of a message indicating a corrupted file _("The update file has been downloaded, but it has been corrupted during download.\n" "Would you like to download the update file again?"), # Translators: the title of a message indicating a corrupted file _("Download Error"), style=wx.YES_NO | wx.ICON_QUESTION, ) if msg == wx.YES: return perform_update(update_url, sha1hash) else: return # Go ahead and install the update log.debug("Installing the update...") wx.MessageBox( # Translators: the content of a message indicating successful download of the update bundle _("The update has been downloaded successfully, and it is ready to be installed.\n" "The application will be restarted in order to complete the update process.\n" "Click the OK button to continue."), # Translators: the title of a message indicating successful download of the update bundle _("Download Completed"), style=wx.ICON_INFORMATION, ) ex_dlg = wx.ProgressDialog( # Translators: the title of a message shown when extracting an update bundle _("Extracting Update Bundle"), # Translators: a message shown when extracting an update bundle _("Please wait..."), parent=wx.GetApp().mainFrame, style=wx.PD_APP_MODAL) extraction_dir = extract_update_bundle(bundle) bundle.close() wx.CallAfter(ex_dlg.Close) wx.CallAfter(ex_dlg.Destroy) if extraction_dir is not None: wx.CallAfter(execute_bootstrap, extraction_dir)
def perform_update(upstream_version_info): msg = wx.MessageBox( # Translators: the content of a message indicating the availability of an update _( "A new update for Bookworm has been released.\n" "Would you like to download and install it?\n" "Installed Version: {current}\n" "New Version: {new}\n" ).format(current=app.version, new=upstream_version_info.version), # Translators: the title of a message indicating the availability of an update _("Bookworm Update"), style=wx.YES_NO | wx.ICON_INFORMATION, ) if msg != wx.YES: log.info("User cancelled the update.") return # Download the update package progress_dlg = RobustProgressDialog( wx.GetApp().mainFrame, # Translators: the title of a message indicating the progress of downloading an update _("Downloading Update"), # Translators: a message indicating the progress of downloading an update bundle "{} {}".format(_("Downloading update bundle"), "".ljust(25)), maxvalue=101, can_hide=True, can_abort=True, ) bundle_file = tempfile.TemporaryFile() try: log.debug( f"Downloading update from: {upstream_version_info.bundle_download_url}" ) dl_request = HttpResource(upstream_version_info.bundle_download_url).download() callback = partial(file_download_callback, progress_dlg) progress_dlg.set_abort_callback(dl_request.cancel) dl_request.download_to_file(bundle_file, callback) except ConnectionError: log.exception("Failed to download update file", exc_info=True) progress_dlg.Dismiss() wx.CallAfter( wx.MessageBox, # Translators: the content of a message indicating a failure in downloading an update _( "A network error was occured when trying to download the update.\n" "Make sure you are connected to the internet, " "or try again at a later time." ), # Translators: the title of a message indicating a failure in downloading an update _("Network Error"), style=wx.ICON_ERROR, ) return if progress_dlg.WasCancelled(): log.debug("User canceled the download of the update.") return log.debug("The update bundle has been downloaded successfully.") if generate_sha1hash(bundle_file) != upstream_version_info.update_sha1hash: log.debug("Hashes do not match.") progress_dlg.Dismiss() bundle_file.close() msg = wx.MessageBox( # Translators: the content of a message indicating a corrupted file _( "The update file has been downloaded, but it has been corrupted during download.\n" "Would you like to download the update file again?" ), # Translators: the title of a message indicating a corrupted file _("Download Error"), style=wx.YES_NO | wx.ICON_QUESTION, ) if msg == wx.YES: return perform_update(upstream_version_info) else: return # Go ahead and install the update log.debug("Installing the update...") bundle_file.seek(0) try: with progress_dlg.PulseContinuously(_("Extracting update bundle...")): extraction_dir = extract_update_bundle(bundle_file) except: log.debug("Error extracting update bundle.", exc_info=True) wx.MessageBox( _( "A problem has occured when installing the update.\nPlease check the logs for more info." ), _("Error installing update"), style=wx.ICON_ERROR, ) return finally: bundle_file.close() progress_dlg.Dismiss() wx.MessageBox( # Translators: the content of a message indicating successful download of the update bundle _( "The update has been downloaded successfully, and it is ready to be installed.\n" "The application will be restarted in order to complete the update process.\n" "Click the OK button to continue." ), # Translators: the title of a message indicating successful download of the update bundle _("Download Completed"), style=wx.ICON_INFORMATION, ) wx.CallAfter(execute_bootstrap, extraction_dir)