def install_package(name): available = get_available_packages() if name not in available: return InstallationStatuses.FORMULA_NOT_FOUND packages = [package.name for package in Package.select().where(Package.name == name)] if len(packages) > 1: return InstallationStatuses.INSTALL_ERROR elif packages and packages[0] == name: return InstallationStatuses.ALREADY_INSTALLED package = available.get(name) formula = import_formula_module(package.get('formula_name')) if not formula: return InstallationStatuses.BAD_FORMULA status = formula.install() if status != InstallationStatuses.INSTALL_OK: return status Package.create( name=formula.name, formula_name=package.get('formula_name'), version=formula.version ) return status
def get_installed_packages(): """Returns dictionary with all installed packages. """ installed = {} for entry in Package.select(): installed.setdefault(entry.name, { 'formula_name': entry.formula_name, 'version': entry.version, 'updated_at': entry.updated_at, }) return installed