2022-10-08 21:51:15 +06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Migration plugin from "DeACSM" to "ACSM Input"
|
|
|
|
# See README.md for details.
|
|
|
|
|
2023-02-24 19:11:15 +06:00
|
|
|
'''
|
|
|
|
Copyright (c) 2021-2023 Leseratte10
|
|
|
|
This file is part of the ACSM Input Plugin by Leseratte10
|
|
|
|
ACSM Input Plugin for Calibre / acsm-calibre-plugin
|
|
|
|
|
|
|
|
For more information, see:
|
|
|
|
https://github.com/Leseratte10/acsm-calibre-plugin
|
|
|
|
'''
|
|
|
|
|
2022-10-08 21:51:15 +06:00
|
|
|
import os, sys
|
|
|
|
|
|
|
|
from calibre.gui2.actions import InterfaceAction
|
|
|
|
|
|
|
|
|
|
|
|
class ActualMigrationPlugin(InterfaceAction):
|
|
|
|
name = "DeACSM"
|
|
|
|
|
2022-10-09 15:31:14 +06:00
|
|
|
def file_is_zip_file(self, filepath):
|
|
|
|
try:
|
|
|
|
file = open(filepath, "rb")
|
|
|
|
data = file.read(10)
|
|
|
|
file.close()
|
|
|
|
if data[:4] == b'PK\x03\x04':
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return False
|
2022-10-08 21:51:15 +06:00
|
|
|
|
|
|
|
|
|
|
|
def genesis(self):
|
|
|
|
print("DeACSM -> ACSM Input migration started ...")
|
|
|
|
|
|
|
|
DOWNLOAD_URL = "https://github.com/Leseratte10/acsm-calibre-plugin/releases/download/config/TEST_calibre_plugin_acsminput_new_0_0_30.zip"
|
|
|
|
|
|
|
|
|
|
|
|
# Okay, now download the new version and uninstall myself:
|
|
|
|
from calibre.utils.config import config_dir
|
|
|
|
self.pluginsdir = os.path.join(config_dir,"plugins")
|
|
|
|
if not os.path.exists(self.pluginsdir):
|
|
|
|
os.mkdir(self.pluginsdir)
|
|
|
|
|
|
|
|
new_path = os.path.join(self.pluginsdir, "ACSM Input.zip")
|
|
|
|
|
|
|
|
if os.path.exists(new_path):
|
|
|
|
# If so, delete ourselves and exit
|
2022-10-09 15:31:14 +06:00
|
|
|
print("Migration has already happened? ...")
|
2022-10-08 21:51:15 +06:00
|
|
|
return
|
|
|
|
|
2022-10-09 15:31:14 +06:00
|
|
|
print("Downloading new plugin ...")
|
2022-10-08 21:51:15 +06:00
|
|
|
if sys.version_info[0] == 2:
|
|
|
|
import urllib
|
|
|
|
urllib.urlretrieve(DOWNLOAD_URL, new_path)
|
|
|
|
else:
|
|
|
|
import urllib.request
|
|
|
|
urllib.request.urlretrieve(DOWNLOAD_URL, new_path)
|
|
|
|
|
2022-10-09 15:31:14 +06:00
|
|
|
print("Download done")
|
|
|
|
|
2022-10-08 21:51:15 +06:00
|
|
|
# Check if the download was successful and the new file exists:
|
2022-10-09 15:31:14 +06:00
|
|
|
if os.path.exists(new_path) and self.file_is_zip_file(new_path):
|
|
|
|
print("Downloaded file is valid, replacing old plugin with new one")
|
2022-10-08 21:51:15 +06:00
|
|
|
# Delete myself
|
|
|
|
os.remove(os.path.join(self.pluginsdir, "DeACSM.zip"))
|
|
|
|
|
|
|
|
# Forcibly add the new plugin
|
|
|
|
from calibre.customize.ui import _config
|
|
|
|
ui_plg_config = _config()
|
|
|
|
plugins = ui_plg_config['plugins']
|
|
|
|
plugins["ACSM Input"] = new_path
|
|
|
|
ui_plg_config['plugins'] = plugins
|
|
|
|
|
|
|
|
# Force-kill Calibre and have the user manually restart it:
|
2022-10-09 15:31:14 +06:00
|
|
|
print("Done, exiting Calibre, please restart")
|
2022-10-08 21:51:15 +06:00
|
|
|
try:
|
|
|
|
os._exit(42)
|
|
|
|
except TypeError:
|
|
|
|
os._exit()
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("Download / Update failed, trying again later ...")
|
|
|
|
print("Please open a bug report for the ACSM Input plugin")
|
|
|
|
|
2022-10-09 15:31:14 +06:00
|
|
|
try:
|
|
|
|
# If we downloaded an error page or something else that's not a ZIP, delete that.
|
|
|
|
os.remove(new_path)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-10-08 21:51:15 +06:00
|
|
|
|
|
|
|
|