import logging
import subprocess
import json
from types import SimpleNamespace
def load_config(filename: str) -> object:
"""Получает настройки из файла JSON
Args:
filename (str): Имя файла конфигурации в формате JSON
Returns:
object: объект настроек
"""
try:
with open(filename, 'r') as f:
return json.load(f, object_hook=lambda d: SimpleNamespace(**d))
except FileNotFoundError as e:
logging.warning(f'Load config file: {e.strerror}')
return
except json.decoder.JSONDecodeError as e:
logging.warning(f'Parsing config file: {e.msg}')
return
def main():
logging.basicConfig(
filename='rebind_configuration_repository.log',
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
config = load_config('rebind_configuration_repository_config.json')
if config is None:
return
logging.info(f'ConfigurationRepositoryUnbindCfg {config.path_to_db}')
user = config.user.encode('cp1251')
password = ('/P' + config.password).encode('cp1251')
repository_user = config.repository_user.encode('cp1251')
repository_password = ('/ConfigurationRepositoryP' + config.repository_password).encode('cp1251')
extension_name = config.extension_name.encode('cp1251')
result = subprocess.run([
config.platform_path, 'DESIGNER',
'/DisableStartupDialogs',
'/Out', 'rebind_configuration_repository_1c.log',
'/S', config.path_to_db,
'/N', user,
password,
'/ConfigurationRepositoryUnbindCfg', '-force',
'-Extension', extension_name],
encoding='cp1251',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if result.returncode != 0:
logging.warning(f'ConfigurationRepositoryUnbindCfg: {result.stdout}')
logging.info(result.args)
return
else:
logging.info(
f'ConfigurationRepositoryUnbindCfg: end {config.path_to_db}, {result.stdout}')
result = subprocess.run([
config.platform_path, 'DESIGNER',
'/DisableStartupDialogs',
'/Out', 'rebind_configuration_repository_1c.log',
'/S', config.path_to_db,
'/N', user,
password,
'/ConfigurationRepositoryF', config.repository_path,
'/ConfigurationRepositoryN', repository_user,
repository_password,
'/ConfigurationRepositoryBindCfg',
'-forceBindAlreadyBindedUser',
'-forceReplaceCfg',
'-Extension', extension_name],
encoding='cp1251',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if result.returncode != 0:
logging.warning(f'ConfigurationRepositoryBindCfg: {result.stderr}')
else:
logging.info(
f'ConfigurationRepositoryBindCfg: end {config.path_to_db}, {result.stdout}')
if __name__ == '__main__':
main()