def update(config): """ Updates the database """ global SAVE_DIR urls = config['main']['custom_plugin_repos'] if not urls: logging.info('No plugin repositories configured.') return 1 rc = 0 for idx, REPO_URL in enumerate(urls): DEST = os.path.join(SAVE_DIR, 'plugins%d.zip' % idx) logging.info('Downloading plugins from %s to %s', REPO_URL, DEST) try: os.makedirs(SAVE_DIR, exist_ok=True) before_update = _analyse_dir(SAVE_DIR) download_file(REPO_URL, os.path.join(SAVE_DIR, DEST)) logging.info('Unzipping...') unzip(DEST, SAVE_DIR, strip_dirs=1) after_update = _analyse_dir(SAVE_DIR) b_len = len(before_update) a_len = len(after_update) if a_len > b_len: logging.info('Found %d new file(s).', a_len - b_len) changed = 0 for filename, filehash in after_update.items(): if filename in before_update and filehash != before_update[ filename]: changed += 1 if changed: logging.info('%d file(s) were changed.', changed) except Exception as ex: logging.error('Error while updating plugins: %s', ex) rc = 1 return rc
def update(): """ Updates the database """ global REPO_URL, SAVE_DIR DEST = os.path.join(SAVE_DIR, 'plugins.zip') logging.info('Downloading plugins to %s', DEST) try: os.makedirs(SAVE_DIR, exist_ok=True) before_update = _analyse_dir(SAVE_DIR) download_file(REPO_URL, os.path.join(SAVE_DIR, DEST)) logging.info('Unzipping...') unzip(DEST, SAVE_DIR, strip_dirs=1) after_update = _analyse_dir(SAVE_DIR) b_len = len(before_update) a_len = len(after_update) if a_len > b_len: logging.info('Found %d new file(s).', a_len - b_len) changed = 0 for filename, filehash in after_update.items(): if filename in before_update and filehash != before_update[ filename]: changed += 1 if changed: logging.info('%d file(s) were changed.', changed) return 0 except Exception as ex: logging.error('Error while updating plugins %s', ex) return 1
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) plugin_info = analyze_plugin(available[plugin_name]) if '__min_pwnagotchi_version__' in plugin_info: min_version = parse_version(plugin_info['__min_pwnagotchi_version__']) pwn_version = parse_version(pwnagotchi_version) if pwn_version < min_version: looging.error("Can't install %s because it requires pwnagotchi version %s", plugin_name, min_version) return 1 if '__dependencies__' in plugin_info: deps = plugin_info['__dependencies__'] if 'pip' in deps: for d in deps['pip']: if not pip_install(d): logging.error('Dependency "%s" not found', d) return 1 if 'apt' in deps: for d in deps['apt']: if not apt_install(d): logging.error('Dependency "%s" not found', d) return 1 os.makedirs(install_path, exist_ok=True) if '__assets__' in plugin_info: assets = plugin_info['__assets__'] if assets: if not isinstance(assets, list): assets = [assets] for a in assets: if a.startswith('https://'): dst = os.path.join(install_path, os.path.basename(a)) if not os.path.exists(dst) and not has_internet(): logging.error('Could not download asset %s', a) return 1 logging.info('Downloading asset: %s', os.path.basename(a)) download_file(a, dst) continue for f in glob.glob(a): dst = os.path.join(install_path, os.path.basename(f)) logging.info('Copy asset: %s', os.path.basename(f)) shutil.copyfile(f, dst) shutil.copyfile(available[plugin_name], os.path.join(install_path, os.path.basename(available[plugin_name]))) return 0
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_plugin_data = analyze_plugin(available[plugin]) available_version = parse_version(available_plugin_data['__version__']) installed_version = parse_version(analyze_plugin(filename)['__version__']) if installed_version and available_version: if available_version <= installed_version: continue else: continue if '__min_pwnagotchi_version__' in available_plugin_data: min_version = parse_version(available_plugin_data['__min_pwnagotchi_version__']) pwn_version = parse_version(pwnagotchi_version) if pwn_version < min_version: looging.info('Skip %s because it requires pwnagotchi version %s', plugin, min_version) continue logging.info('Upgrade %s from %s to %s', plugin, '.'.join(installed_version), '.'.join(available_version)) if '__dependencies__' in available_plugin_data: deps = available_plugin_data['__dependencies__'] if 'pip' in deps: for d in deps['pip']: if not pip_install(d): logging.error('Dependency "%s" not found', d) return 1 if 'apt' in deps: for d in deps['apt']: if not apt_install(d): logging.error('Dependency "%s" not found', d) return 1 if '__assets__' in available_plugin_data: assets = available_plugin_data['__assets__'] if assets: if not isinstance(assets, list): assets = [assets] for a in assets: if a.startswith('https://'): dst = os.path.join(os.path.dirname(installed[plugin]), os.path.basename(a)) if not os.path.exists(dst) and not has_internet(): logging.error('Could not download asset %s', a) return 1 logging.info('Downloading asset: %s', os.path.basename(a)) download_file(a, dst) continue for f in glob.glob(a): dst = os.path.join(os.path.dirname(installed[plugin]), os.path.basename(f)) logging.info('Copy asset: %s', os.path.basename(f)) shutil.copyfile(f, dst) shutil.copyfile(available[plugin], installed[plugin]) return 0