2023-02-16 05:40:34 +00:00
|
|
|
import os
|
2023-02-15 04:12:17 +00:00
|
|
|
import time
|
2023-02-16 05:40:34 +00:00
|
|
|
import win32api
|
|
|
|
import shutil
|
2023-02-20 14:10:59 +00:00
|
|
|
import subprocess
|
2023-02-15 04:12:17 +00:00
|
|
|
|
2023-03-20 02:02:50 +00:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
from xml.sax import SAXException
|
|
|
|
|
2023-02-15 04:12:17 +00:00
|
|
|
from common import wait_pid, BaseApplication
|
|
|
|
|
|
|
|
|
|
|
|
_default_path = r'C:\Program Files\DBeaver\dbeaver-cli.exe'
|
|
|
|
|
|
|
|
|
|
|
|
class AppletApplication(BaseApplication):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.path = _default_path
|
|
|
|
self.username = self.account.username
|
|
|
|
self.password = self.account.secret
|
|
|
|
self.privileged = self.account.privileged
|
|
|
|
self.host = self.asset.address
|
|
|
|
self.port = self.asset.get_protocol_port(self.protocol)
|
|
|
|
self.db = self.asset.spec_info.db_name
|
|
|
|
self.name = '%s-%s-%s' % (self.host, self.db, int(time.time()))
|
2023-03-20 02:02:50 +00:00
|
|
|
self.app_work_path = self.get_app_work_path()
|
2023-02-15 04:12:17 +00:00
|
|
|
self.pid = None
|
|
|
|
self.app = None
|
|
|
|
|
2023-03-20 02:02:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_app_work_path():
|
2023-02-16 05:40:34 +00:00
|
|
|
win_user_name = win32api.GetUserName()
|
2023-03-20 02:02:50 +00:00
|
|
|
return r'C:\Users\%s\AppData\Roaming\DBeaverData' % win_user_name
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _read_config(config_file):
|
|
|
|
default_config = {}
|
|
|
|
if not os.path.exists(config_file):
|
|
|
|
return default_config
|
|
|
|
|
|
|
|
with open(config_file, 'r') as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
try:
|
|
|
|
config_key, config_value = line.split('=')
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
default_config[config_key] = config_value
|
|
|
|
return default_config
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _write_config(config_file, config):
|
|
|
|
with open(config_file, 'w')as f:
|
|
|
|
for key, value in config.items():
|
|
|
|
f.write(f'{key}={value}\n')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _merge_driver_xml(src_path, dest_path):
|
|
|
|
tree1 = ElementTree.parse(dest_path)
|
|
|
|
tree2 = ElementTree.parse(src_path)
|
|
|
|
|
|
|
|
for child2 in tree2.getroot():
|
|
|
|
found = False
|
|
|
|
for child1 in tree1.getroot():
|
|
|
|
if child1.tag == child2.tag and child1.attrib == child2.attrib:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
tree1.getroot().append(child2)
|
|
|
|
tree1.write(dest_path)
|
|
|
|
|
|
|
|
def init_driver(self):
|
2023-02-16 05:40:34 +00:00
|
|
|
src_driver = os.path.join(os.path.dirname(self.path), 'drivers')
|
2023-03-20 02:02:50 +00:00
|
|
|
dest_driver = os.path.join(self.app_work_path, 'drivers')
|
2023-02-16 05:40:34 +00:00
|
|
|
if not os.path.exists(dest_driver):
|
|
|
|
shutil.copytree(src_driver, dest_driver, dirs_exist_ok=True)
|
|
|
|
|
2023-03-20 02:02:50 +00:00
|
|
|
def init_driver_config(self):
|
|
|
|
driver_yml_path = os.path.join(
|
|
|
|
self.app_work_path, 'workspace6', '.metadata', '.config',
|
|
|
|
)
|
|
|
|
driver_yml_file = os.path.join(driver_yml_path, 'drivers.xml')
|
|
|
|
try:
|
|
|
|
self._merge_driver_xml('./config/drivers.xml', driver_yml_file)
|
|
|
|
except (SAXException, FileNotFoundError):
|
|
|
|
os.makedirs(driver_yml_path, exist_ok=True)
|
|
|
|
shutil.copy('./config/drivers.xml', driver_yml_file)
|
|
|
|
|
|
|
|
def init_other_config(self):
|
|
|
|
config_path = os.path.join(
|
|
|
|
self.app_work_path, 'workspace6', '.metadata',
|
|
|
|
'.plugins', 'org.eclipse.core.runtime', '.settings',
|
|
|
|
)
|
|
|
|
os.makedirs(config_path, exist_ok=True)
|
|
|
|
config_file = os.path.join(config_path, 'org.jkiss.dbeaver.core.prefs')
|
|
|
|
|
|
|
|
config = self._read_config(config_file)
|
|
|
|
config['ui.auto.update.check'] = 'false'
|
|
|
|
config['sample.database.canceled'] = 'true'
|
|
|
|
config['tipOfTheDayInitializer.notFirstRun'] = 'true'
|
|
|
|
config['ui.show.tip.of.the.day.on.startup'] = 'false'
|
|
|
|
self._write_config(config_file, config)
|
|
|
|
|
|
|
|
def launch(self):
|
|
|
|
self.init_driver()
|
|
|
|
self.init_driver_config()
|
|
|
|
self.init_other_config()
|
|
|
|
|
2023-02-15 04:12:17 +00:00
|
|
|
def _get_exec_params(self):
|
|
|
|
driver = getattr(self, 'driver', self.protocol)
|
|
|
|
params_string = f'name={self.name}|' \
|
|
|
|
f'driver={driver}|' \
|
|
|
|
f'host={self.host}|' \
|
|
|
|
f'port={self.port}|' \
|
|
|
|
f'database={self.db}|' \
|
|
|
|
f'"user={self.username}"|' \
|
|
|
|
f'password={self.password}|' \
|
|
|
|
f'save=false|' \
|
|
|
|
f'connect=true'
|
|
|
|
return params_string
|
|
|
|
|
|
|
|
def _get_mysql_exec_params(self):
|
|
|
|
params_string = self._get_exec_params()
|
|
|
|
params_string += '|prop.allowPublicKeyRetrieval=true'
|
|
|
|
return params_string
|
|
|
|
|
|
|
|
def _get_oracle_exec_params(self):
|
|
|
|
if self.privileged:
|
|
|
|
self.username = '%s as sysdba' % self.username
|
|
|
|
return self._get_exec_params()
|
|
|
|
|
|
|
|
def _get_sqlserver_exec_params(self):
|
|
|
|
setattr(self, 'driver', 'mssql_jdbc_ms_new')
|
|
|
|
return self._get_exec_params()
|
|
|
|
|
|
|
|
def run(self):
|
2023-02-16 05:40:34 +00:00
|
|
|
self.launch()
|
2023-02-15 04:12:17 +00:00
|
|
|
|
|
|
|
function = getattr(self, '_get_%s_exec_params' % self.protocol, None)
|
|
|
|
if function is None:
|
|
|
|
params = self._get_exec_params()
|
|
|
|
else:
|
|
|
|
params = function()
|
2023-02-20 14:10:59 +00:00
|
|
|
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
|
|
|
|
startupinfo.wShowWindow = subprocess.SW_HIDE
|
2023-02-20 14:21:46 +00:00
|
|
|
exec_string = '%s -con %s' % (self.path, params)
|
|
|
|
ret = subprocess.Popen(exec_string, startupinfo=startupinfo)
|
2023-02-20 14:10:59 +00:00
|
|
|
self.pid = ret.pid
|
2023-02-15 04:12:17 +00:00
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
wait_pid(self.pid)
|