import os import platform import xml.dom.minidom as minidom import sys import shutil import zipfile def get_install_directory(): """Get the correct installation directory based on the platform.""" system = platform.system() if system == "Windows": return os.path.join(os.environ.get("APPDATA"), "kingsoft", "wps", "jsaddons") elif system == "Linux": return os.path.join( os.path.expanduser("~"), ".local", "share", "Kingsoft", "wps", "jsaddons" ) else: print(f"Unsupported platform: {system}") sys.exit(1) def create_authwebsite_xml(directory, node_name="xbotwps"): """Create the authwebsite.xml file if it doesn't exist or update it if it exists.""" file_path = os.path.join(directory, "authwebsite.xml") # Create the basic XML structure if the file doesn't exist if not os.path.exists(file_path): # Create base XML structure content = '\n\n\n' with open(file_path, "w", encoding="utf-8") as f: f.write(content) print(f"Created basic authwebsite.xml structure at {file_path}") try: # Parse the XML file dom = minidom.parse(file_path) # Get the websites element websites_elements = dom.getElementsByTagName("websites") if not websites_elements: # If no websites element exists, create it websites = dom.createElement("websites") dom.appendChild(websites) else: websites = websites_elements[0] # Check if a website node with the specified name already exists existing_nodes = [] for website in websites.getElementsByTagName("website"): if website.getAttribute("name") == node_name: existing_nodes.append(website) # Remove existing nodes with the same name for node in existing_nodes: websites.removeChild(node) # Create and append the new website node new_website = dom.createElement("website") new_website.setAttribute("origin", "null") new_website.setAttribute("status", "enable") new_website.setAttribute("name", node_name) websites.appendChild(new_website) # Save the updated XML file with open(file_path, "w", encoding="utf-8") as f: f.write(dom.toprettyxml(indent=" ")) print(f"Updated authwebsite.xml at {file_path}") except Exception as e: raise Exception(f"Error working with authwebsite.xml: {e}") def create_publish_xml( directory, node_name="xbotwps", verson="1.0.0", addon_type="wps" ): """Create the publish.xml file if it doesn't exist or update it if it exists.""" file_path = os.path.join(directory, "publish.xml") # Create the basic XML structure if the file doesn't exist if not os.path.exists(file_path): # Create base XML structure content = '\n\n\n' with open(file_path, "w", encoding="utf-8") as f: f.write(content) print(f"Created basic publish.xml structure at {file_path}") try: # Parse the XML file dom = minidom.parse(file_path) # Get the jsplugins element jsplugins_elements = dom.getElementsByTagName("jsplugins") if not jsplugins_elements: # If no jsplugins element exists, create it jsplugins = dom.createElement("jsplugins") dom.appendChild(jsplugins) else: jsplugins = jsplugins_elements[0] # Check if a jsplugin node with the specified name already exists existing_nodes = [] for jsplugin in jsplugins.getElementsByTagName("jsplugin"): if jsplugin.getAttribute("name") == node_name: existing_nodes.append(jsplugin) # Remove existing nodes with the same name for node in existing_nodes: jsplugins.removeChild(node) # Create and append the new jsplugin node new_jsplugin = dom.createElement("jsplugin") new_jsplugin.setAttribute("enable", "enable_dev") new_jsplugin.setAttribute("url", "null") new_jsplugin.setAttribute("install", "null") new_jsplugin.setAttribute("version", verson) new_jsplugin.setAttribute("type", addon_type) new_jsplugin.setAttribute("customDomain", "") new_jsplugin.setAttribute("name", node_name) jsplugins.appendChild(new_jsplugin) # Save the updated XML file with open(file_path, "w", encoding="utf-8") as f: f.write(dom.toprettyxml(indent=" ")) print(f"Updated publish.xml at {file_path}") except Exception as e: raise Exception(f"Error working with publish.xml: {e}") def extract_addon(addon_path, install_dir, addon_name, addon_version): """Extract the addon archive to the installation directory.""" addon_name_with_version = f"{addon_name}_{addon_version}" extract_dir = os.path.join(install_dir, addon_name_with_version) # Clean the existing directory if it exists if os.path.exists(extract_dir): print(f"Removing existing addon directory: {extract_dir}") shutil.rmtree(extract_dir) # Create the extraction directory os.makedirs(extract_dir) # Determine the archive type and extract accordingly file_ext = os.path.splitext(addon_path)[1].lower() try: print(f"Extracting ZIP archive to {extract_dir}") with zipfile.ZipFile(addon_path, "r") as zip_ref: zip_ref.extractall(extract_dir) print("Extraction completed successfully") return True except Exception as e: print(f"Error extracting archive: {e}") return False def main(): # 1. 将插件文件解压到插件安装目录下 # 2. 从插件目录下读取package.json文件,获取插件名称, 版本号, 插件类型 # 3. 创建authwebsite.xml文件,添加插件名称节点 # 4. 创建publish.xml文件,添加插件名称节点 addon_name = "XbotWps" addon_type = "et" addon_version = "1.0.0" addon_path = r"C:\Users\32075\OneDrive\文档\Code\RPA_Script\wps-js-extension\XbotWps\scripts\XbotWps_1.0.0.zip" install_dir = get_install_directory() print(f"Installation directory: {install_dir}") # Create the installation directory if it doesn't exist if not os.path.exists(install_dir): os.makedirs(install_dir) print(f"Created installation directory: {install_dir}") # Create the required XML files create_authwebsite_xml(install_dir, addon_name) create_publish_xml( install_dir, addon_name, verson=addon_version, addon_type=addon_type ) # Extract the addon to the installation directory if extract_addon(addon_path, install_dir, addon_name, addon_version): print("Addon extracted successfully.") else: print("Failed to extract addon.") print("Installation completed successfully.") if __name__ == "__main__": main()