def install_package(pearl_env: PearlEnvironment, package: Package, args: Namespace): """ Installs the Pearl package. """ if package.is_installed(): raise PackageAlreadyInstalledError( '{} package is already installed.'.format(package)) messenger.print('{cyan}* {normal}Installing {pkg} package'.format( cyan=Color.CYAN, normal=Color.NORMAL, pkg=package, )) package.dir.mkdir(parents=True, exist_ok=True) if package.is_local(): check_and_copy(Path(package.url), package.dir) else: quiet = "false" if args.verbose else "true" script = dedent(""" install_git_repo {pkgurl} {pkgdir} "" {quiet} """).format(pkgdir=package.dir, pkgurl=package.url, quiet=quiet) run_pearl_bash(script, pearl_env, input=_DEFAULT_INPUT if args.no_confirm else None) package.vardir.mkdir(parents=True, exist_ok=True) hook = 'post_install' try: _run( hook, pearl_env, package, input=_DEFAULT_INPUT if args.no_confirm else None, enable_xtrace=(args.verbose >= 2), enable_errexit=(not args.force), ) except Exception as exc: msg = "Error while performing {} hook function. Rolling back...".format( hook) if args.force: message = "{}: {}".format(msg, exc.args) if args.verbose: messenger.exception(message) else: messenger.error(message) else: args.force = True remove_package( pearl_env, package, args=args, ) raise HookFunctionError(msg) from exc
def test_check_and_copy(tmp_path): src_dir = tmp_path / 'src' (src_dir / 'path1').mkdir(parents=True) (src_dir / 'path1/file1').touch() dst_dir = tmp_path / 'dst' (dst_dir / 'path1').mkdir(parents=True) (dst_dir / 'path1/fileold1').touch() check_and_copy(src_dir, dst_dir) assert not (dst_dir / 'path1/fileold1').exists() assert (dst_dir / 'path1/file1').exists()
def test_check_and_copy_not_a_dir(tmp_path): with pytest.raises(NotADirectoryError): check_and_copy(tmp_path / 'not-a-dir', tmp_path)
def update_package(pearl_env: PearlEnvironment, package: Package, args: Namespace): """ Updates the Pearl package. """ if not package.is_installed(): raise PackageNotInstalledError( '{} package has not been installed.'.format(package)) messenger.print('{cyan}* {normal}Updating {pkg} package'.format( cyan=Color.CYAN, normal=Color.NORMAL, pkg=package, )) if package.has_url_changed(): messenger.info("The package URL for {} has changed to {}".format( package.full_name, package.url)) messenger.info("Replacing the package with the new repository...") remove_package(pearl_env, package, args) install_package(pearl_env, package, args) hook = 'pre_update' try: _run( hook, pearl_env, package, input=_DEFAULT_INPUT if args.no_confirm else None, enable_xtrace=(args.verbose >= 2), enable_errexit=(not args.force), ) except Exception as exc: msg = "Error while performing {} hook function".format(hook) if not args.force: raise HookFunctionError(msg) from exc message = "{}: {}".format(msg, exc.args) if args.verbose: messenger.exception(message) else: messenger.error(message) if package.is_local(): check_and_copy(Path(package.url), package.dir) else: quiet = "false" if args.verbose else "true" script = dedent(""" update_git_repo {pkgdir} "" {quiet} """).format(pkgdir=package.dir, quiet=quiet) run_pearl_bash(script, pearl_env, input=_DEFAULT_INPUT if args.no_confirm else None) hook = 'post_update' try: _run( hook, pearl_env, package, input=_DEFAULT_INPUT if args.no_confirm else None, enable_xtrace=(args.verbose >= 2), enable_errexit=(not args.force), ) except Exception as exc: msg = "Error while performing {} hook function".format(hook) if not args.force: raise HookFunctionError(msg) from exc message = "{}: {}".format(msg, exc.args) if args.verbose: messenger.exception(message) else: messenger.error(message)