def upgrade(args, config, pattern='*'): """ Upgrades the given plugin """ available = _get_available() installed = _get_installed(config) for plugin, filename in installed.items(): if not fnmatch(plugin, pattern) or plugin not in available: continue available_version = _extract_version(available[plugin]) installed_version = _extract_version(filename) if installed_version and available_version: if available_version <= installed_version: continue else: continue logging.info('Upgrade %s from %s to %s', plugin, '.'.join(installed_version), '.'.join(available_version)) shutil.copyfile(available[plugin], installed[plugin]) # maybe has config for conf in glob.glob(available[plugin].replace('.py', '.y?ml')): dst = os.path.join(os.path.dirname(installed[plugin]), os.path.basename(conf)) if os.path.exists(dst) and md5(dst) != md5(conf): # backup logging.info('Backing up config: %s', os.path.basename(conf)) shutil.move(dst, dst + '.bak') shutil.copyfile(conf, dst) return 0
def _analyse_dir(path): results = dict() path += '*' if path.endswith('/') else '/*' for filename in glob.glob(path, recursive=True): if not os.path.isfile(filename): continue try: results[filename] = md5(filename) except OSError: continue return results
def install(args, config): """ Installs the given plugin """ global DEFAULT_INSTALL_PATH plugin_name = args.name available = _get_available() installed = _get_installed(config) if plugin_name not in available: logging.error('%s not found.', plugin_name) return 1 if plugin_name in installed: logging.error('%s already installed.', plugin_name) # install into custom_plugins path install_path = config['main']['custom_plugins'] if not install_path: install_path = DEFAULT_INSTALL_PATH config['main']['custom_plugins'] = install_path save_config(config, args.user_config) os.makedirs(install_path, exist_ok=True) shutil.copyfile( available[plugin_name], os.path.join(install_path, os.path.basename(available[plugin_name]))) # maybe has config for conf in glob.glob(available[plugin_name].replace('.py', '.y?ml')): dst = os.path.join(install_path, os.path.basename(conf)) if os.path.exists(dst) and md5(dst) != md5(conf): # backup logging.info('Backing up config: %s', os.path.basename(conf)) shutil.move(dst, dst + '.bak') shutil.copyfile(conf, dst) return 0