def setup(install_all, ask_full, verbose): """Setup optional Ocean packages and configuration file(s). Equivalent to running `dwave install [--all]`, followed by `dwave config create [--full]`. """ contrib = get_contrib_packages() packages = list(contrib) if not packages: install = False elif install_all: click.echo("Installing all optional non-open-source packages.\n") install = True else: # The default flow: SDK installed, so some contrib packages registered # and `dwave setup` ran without `--all` flag. click.echo("Optionally install non-open-source packages and " "configure your environment.\n") prompt = "Do you want to select non-open-source packages to install (y/n)?" val = default_text_input(prompt, default='y') install = val.lower() == 'y' click.echo() if install: for pkg in packages: _install_contrib_package(pkg, verbose=verbose, prompt=not install_all) click.echo("Creating the D-Wave configuration file.") return _config_create(config_file=None, profile=None, ask_full=ask_full)
def _install_contrib_package(name, verbose=False, prompt=True): """pip install non-oss package `name` from dwave's pypi repo.""" contrib = get_contrib_packages() dwave_contrib_repo = "https://pypi.dwavesys.com/simple" assert name in contrib pkg = contrib[name] title = pkg['title'] # check is `name` package is already installed # right now the only way to check that is to check if all dependants from # requirements are installed # NOTE: currently we do not check if versions match! if all(_is_pip_package_installed(req) for req in pkg['requirements']): click.echo("{} already installed.\n".format(title)) return # basic pkg info click.echo(title) click.echo(pkg['description']) # license prompt license = pkg['license'] msgtpl = ("This package is available under the {name} license.\n" "The terms of the license are available online: {url}") click.echo(msgtpl.format(name=license['name'], url=license['url'])) if prompt: val = default_text_input('Install (y/n)?', default='y', optional=False) if val.lower() != 'y': click.echo('Skipping: {}.\n'.format(title)) return click.echo('Installing: {}'.format(title)) for req in pkg['requirements']: # NOTE: py35+ required res = subprocess.run([ sys.executable, "-m", "pip", "install", req, "--extra-index", dwave_contrib_repo ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if res.returncode or verbose: click.echo(res.stdout) click.echo('Successfully installed {}.\n'.format(title))
def install(list_all, install_all, update_all, accept_license, verbose, packages): """Install optional non-open-source Ocean packages.""" contrib = get_contrib_packages() if list_all: if verbose: # ~YAML output for pkg, specs in contrib.items(): click.echo("Package: {}".format(pkg)) click.echo(" Title: {}".format(specs['title'])) click.echo(" Description: {}".format(specs['description'])) click.echo(" License: {}".format(specs['license']['name'])) click.echo(" License-URL: {}".format(specs['license']['url'])) click.echo(" Requires: {}".format(', '.join( specs['requirements']))) click.echo() else: # concise list of available packages if contrib: click.echo("Available packages: {}.".format(', '.join( contrib.keys()))) else: click.echo("No available packages.") return if install_all: packages = list(contrib) if update_all: packages = list(filter(_contrib_package_maybe_installed, contrib)) if not packages: click.echo('Nothing to do. Try "dwave install --help".') return # check all packages requested are registered/available for pkg in packages: if pkg not in contrib: click.echo("Package {!r} not found.".format(pkg)) return 1 for pkg in packages: _install_contrib_package(pkg, verbose=verbose, prompt=not accept_license)
def _contrib_package_maybe_installed(name): """Check if contrib package `name` is installed (even partially).""" contrib = get_contrib_packages() pkg = contrib[name] maybe_installed = False for req in pkg['requirements']: try: _get_dist(req) maybe_installed = True except pkg_resources.VersionConflict: # dependency installed, but wrong version maybe_installed = True except pkg_resources.DistributionNotFound: # dependency not installed pass return maybe_installed
def _install_contrib_package(name, verbose=0, prompt=True): """pip install non-oss package `name` from dwave's pypi repo.""" contrib = get_contrib_packages() dwave_contrib_repo = "https://pypi.dwavesys.com/simple" assert name in contrib pkg = contrib[name] title = pkg['title'] # check if `name` package is already installed # right now the only way to check that is to check if all dependencies from # requirements are installed reinstall = False try: if all(_get_dist(req) for req in pkg['requirements']): click.echo("{} installed and up to date.\n".format(title)) return except pkg_resources.VersionConflict: click.echo("{} dependency version mismatch.\n".format(title)) reinstall = True except pkg_resources.DistributionNotFound: pass # dependency not installed, proceed with install action = 'Reinstall' if reinstall else 'Install' # basic pkg info click.echo(title) click.echo(pkg['description']) # license prompt license = pkg['license'] msgtpl = ("This package is available under the {name} license.\n" "The terms of the license are available online: {url}") click.echo(msgtpl.format(name=license['name'], url=license['url'])) if prompt: val = default_text_input('{} (y/n)?'.format(action), default='y', optional=False) if val.lower() != 'y': click.echo('Skipping: {}.\n'.format(title)) return click.echo('{}ing: {}'.format(action, title)) for req in pkg['requirements']: cmd = [sys.executable, "-m", "pip", "install", req, "--extra-index-url", dwave_contrib_repo] if verbose > 1: cmd.append("-{}".format('v' * (verbose - 1))) res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if res.returncode or verbose: click.echo(res.stdout) if res.returncode: click.echo('Failed to install {}.\n'.format(title)) return click.echo('Successfully installed {}.\n'.format(title))