""" core.pkg.clone - Package clone backend abstraction """ import os from core.pkg.backend import PKG_CORE_BACKEND_REMOTE, PKG_CORE_BACKEND_LOCAL from core.pkg.version import VxVersion from core.pkg.find import find_pkg from core.logger import log __all__ = [ 'clone_pkg' ] #--- # Internals #--- def _pkg_clone_core(pkg, prefix, bare): """ Perform remote clone if needed then local clone if needed too @args > pkg (dict) - package information > prefix (str) - clone path prefix > bare (bool) - do not performs local installation @return > the package path if successfully cloned, None otherwise """ # handle prefix. If not bare clone is required then force the "remote" # cloning operation to be performed in the global storage remote_prefix = prefix if bare else '' # be sure that the package target is in the global storage if pkg['storage'] != 'local': log.user(f"cloning package {pkg['name']}...") version = pkg['version'] pkg_path = PKG_CORE_BACKEND_REMOTE.package_clone(pkg, remote_prefix) if not pkg_path: log.error( f"{pkg['name']}@{version.name}: unable to clone the package", ) return '' if bare: return pkg_path pkg['url'] = pkg_path # "clone" the package (create a symbolic link) return PKG_CORE_BACKEND_LOCAL.package_clone(pkg, prefix, bare) #--- # Public #--- def clone_pkg(name, version, prefix='', confirm=False, bare=False): r""" Clone the package This function will try to clone the package with the exact selected version, with all of its dependencies. See for more information about the process. @args > name (str) - exact valid package name > version (str) - version query string > prefix (str) - clone path prefix > confirm (bool) - display user input to confirm the clone > bare (bool) - do not clone the package to the global storage @return > the package path if successfully cloned, None otherwise """ # if no output prefix is provided, then use the current working directory if not prefix: prefix = f"{os.getcwd()}/" # try to find the package anywhere that the vxSDK allow pkg_list = find_pkg(name, version, local=True, remote=False) if not pkg_list: pkg_list = find_pkg(name, version, local=False, remote=True) if not pkg_list: log.error("[pkg] pacakge find error") return '' if len(pkg_list) != 1: log.warn("[pkg] multiple package found, other will be ignored") # check version information pkg_info = pkg_list[0] if not pkg_info['version']: if version: log.error(f"{name}@{version}: unable to find the version") return '' pkg_info['version'] = VxVersion(pkg_info['default_branch'], 'branch') # wait user interaction if needed if confirm and pkg_info['storage'] != 'local': log.user( f"Do you want to install '{pkg_info['full_name']}'? (Y/n) ", end = '' ) valid = input() if valid and not valid in ['Y', 'y', 'yes', 'Yes']: return '' # "real" clone the package test = _pkg_clone_core(pkg_info, prefix, bare) return test