def list_available(args): """List the name and title of the models in the Hub.""" # Setup. mlhub = utils.get_repo(args.mlhub) meta = utils.get_repo_meta_data(mlhub) # Provide some context. if not args.quiet: print("The repository '{}' provides the following models:\n".format( mlhub)) # List the meta data. for info in meta: utils.print_meta_line(info) # Suggest next step. if not args.quiet: msg = "\nInstall a model with:\n\n $ {} install <model>\n" msg = msg.format(CMD) print(msg)
def list_available(args): """List the name and title of the models in the Hub.""" # Setup. logger = logging.getLogger(__name__) logger.info("List available models.") logger.debug(f"args: {args}") meta, repo = utils.get_repo_meta_data(args.mlhub) model_names = [entry["meta"]["name"] for entry in meta] # Update bash completion list. utils.update_model_completion(set(model_names)) # List model name only. if args.name_only: print("\n".join(model_names)) return # Provide some context. if not args.quiet: msg = "The repository '{}' provides the following models:\n" print(msg.format(repo)) # List the meta data. for entry in meta: utils.print_meta_line(entry) # Suggest next step. if not args.quiet: utils.print_next_step("available") if not os.path.exists(utils.get_init_dir()): print( "Why not give the 'rain' model a go...\n\n" " $ ml install rain\n" )
def list_available(args): """List the name and title of the models in the Hub.""" # Setup. mlhub = utils.get_repo(args.mlhub) meta = utils.get_repo_meta_data(mlhub) # List model name only. if args.name_only: models = [info["meta"]["name"] for info in meta] print('\n'.join(models)) return # Provide some context. if not args.quiet: print("The repository '{}' provides the following models:\n".format(mlhub)) # List the meta data. for info in meta: utils.print_meta_line(info) # Update bash tab completion utils.update_completion_list(COMPLETION_MODELS, {e['meta']['name'] for e in meta}) # Suggest next step. if not args.quiet: utils.print_next_step('available') if not os.path.exists(MLINIT): print("Why not give the 'rain' model a go...\n\n" + " $ ml install rain\n")
def install_model(args): """Install a model.""" # Setup. url = None # Identify if it is a local file name to install. if args.model.endswith(EXT_MLM) and not re.findall('http[s]?:', args.model): # Identify the local mlm file to install. local = args.model mlmfile = local model = os.path.basename(local).split("_")[0] version = os.path.basename(local).split("_")[1].replace(EXT_MLM, "") # Ensure the local init dir exists. init = utils.create_init() path = os.path.join(init, model) else: if re.findall('http[s]?:', args.model): # A specific URL was provided. url = args.model model = url.split("/")[-1].split("_")[0] # Check that the URL exists. r = requests.head(url) if not r.status_code == requests.codes.ok: msg = "{}the url '{}' was not found." msg = msg.format(APPX, url) print(msg, file=sys.stderr) sys.exit(1) else: # Or obtain the repository meta data from Packages.yaml. model = args.model mlhub = utils.get_repo(args.mlhub) meta = utils.get_repo_meta_data(mlhub) # Update available models for fast bash tab completion. utils.update_completion_list(COMPLETION_MODELS, {e['meta']['name'] for e in meta}) # Find the first matching entry in the meta data. url = None for entry in meta: if model == entry["meta"]["name"]: url = mlhub + entry["meta"]["filename"] break # If not found suggest how a model might be installed. if url is None: msg = "{}no model named '{}' was found on '{}'." msg = msg.format(APPX, model, mlhub) print(msg, file=sys.stderr) if not args.quiet: msg = "\nYou can list available models with:\n\n $ {} available\n" msg = msg.format(CMD) print(msg, file=sys.stderr) sys.exit(1) if args.debug: print(DEBUG + "model file url is: " + url) # Ensure file to be downloaded has the expected filename extension. if not url.endswith(EXT_MLM) and not url.endswith(EXT_AIPK): msg = "{}the below url is not a {} file. Malformed '{}' from the repository?\n {}" msg = msg.format(APPX, EXT_MLM, META_YAML, url) print(msg, file=sys.stderr) sys.exit(1) # Further setup. init = utils.create_init() mlmfile = url.split("/")[-1] version = mlmfile.split("_")[1].replace(EXT_MLM, "").replace(EXT_AIPK, "") local = os.path.join(init, mlmfile) path = os.path.join(init, model) # Check if model is already installed. if os.path.exists(path): info = utils.load_description(model) installed_version = info['meta']['version'] if StrictVersion(installed_version) > StrictVersion(version): msg = "Installed version '{}' of '{}' to be downgraded to version '{}'. Continue [Y/n]? " msg = msg.format(installed_version, model, version) sys.stdout.write(msg) choice = input().lower() if choice == 'n': sys.exit(1) elif StrictVersion(installed_version) == StrictVersion(version): msg = "Installed version '{}' of '{}' to be overwritten. Continue [Y/n]? " msg = msg.format(installed_version, model, version) sys.stdout.write(msg) choice = input().lower() if choice == 'n': sys.exit(1) else: msg = "Replacing '{}' version '{}' with '{}'." msg = msg.format(model, installed_version, version) print(msg) rmtree(path) print() # Download the model now if not a local file. if not url is None: # Informative message about the model location and size. if not args.quiet: print("Model " + url + "\n") meta = requests.head(url) dsize = "{:,}".format(int(meta.headers.get("content-length"))) if not args.quiet: print("Downloading '{}' ({} bytes) ...\n". format(mlmfile, dsize)) # Download the archive from the URL. try: urllib.request.urlretrieve(url, local) except urllib.error.HTTPError as error: msg = "{}'{}' {}." msg = msg.format(APPX, url, error.reason.lower()) print(msg, file=sys.stderr) sys.exit(1) zip = zipfile.ZipFile(local) zip.extractall(MLINIT) # Support either .yml or .yaml "cheaply". Should really try and # except but eventually will remove the yml file. The yaml authors # suggest .yaml. desc_yml = os.path.join(path, DESC_YML) desc_yaml = os.path.join(path, DESC_YAML) if (not os.path.exists(desc_yaml)) and os.path.exists(desc_yml): move(desc_yml, desc_yaml) # Update available commands for the model for fast bash tab completion. info = utils.load_description(model) model_cmds = set(info['commands']) utils.update_completion_list(COMPLETION_COMMANDS, model_cmds) # Informative message about the size of the installed model. if not args.quiet: dsz = 0 for (pth, dir, files) in os.walk(path): for f in files: tfilename = os.path.join(pth, f) dsz += os.path.getsize(tfilename) print("Extracted '{}' into\n'{}' ({:,} bytes).". format(mlmfile, path, dsz)) # Suggest next step. README or DOWNLOAD if not args.quiet: utils.print_next_step('install', model=model)