def install( self, requirements, ctx=None, wheel_installer=None, resolve=_nullop, reject=_nullop ): try: if ctx is None: ctx = {'extra': None} if wheel_installer is None: wheel_installer = _RawWheelInstaller() complete_ctx = dict(markers.DEFAULT_CONTEXT) complete_ctx.update(ctx) if isinstance(requirements, str): requirements = [requirements] transaction = { 'wheels': [], 'pyodide_packages': set(), 'locked': dict(self.installed_packages) } for requirement in requirements: self.add_requirement(requirement, complete_ctx, transaction) except Exception as e: reject(str(e)) resolve_count = [len(transaction['wheels'])] def do_resolve(*args): resolve_count[0] -= 1 if resolve_count[0] == 0: resolve( f'Installed {", ".join(self.installed_packages.keys())}' ) # Install built-in packages pyodide_packages = transaction['pyodide_packages'] if len(pyodide_packages): resolve_count[0] += 1 self.installed_packages.update( dict((k, None) for k in pyodide_packages) ) js_pyodide.loadPackage( list(pyodide_packages) ).then(do_resolve) # Now install PyPI packages for name, wheel, ver in transaction['wheels']: wheel_installer(name, wheel, do_resolve, reject) self.installed_packages[name] = ver
def install( self, requirements: Union[str, List[str]], ctx=None, resolve=_nullop, reject=_nullop, ): try: if ctx is None: ctx = {"extra": None} complete_ctx = dict(markers.DEFAULT_CONTEXT) complete_ctx.update(ctx) if isinstance(requirements, str): requirements = [requirements] transaction: Dict[str, Any] = { "wheels": [], "pyodide_packages": set(), "locked": dict(self.installed_packages), } for requirement in requirements: self.add_requirement(requirement, complete_ctx, transaction) except Exception as e: reject(str(e)) resolve_count = [len(transaction["wheels"])] def do_resolve(*args): resolve_count[0] -= 1 if resolve_count[0] == 0: resolve( f'Installed {", ".join(self.installed_packages.keys())}') # Install built-in packages pyodide_packages = transaction["pyodide_packages"] if len(pyodide_packages): resolve_count[0] += 1 self.installed_packages.update( dict((k, None) for k in pyodide_packages)) js_pyodide.loadPackage(list(pyodide_packages)).then(do_resolve) # Now install PyPI packages for name, wheel, ver in transaction["wheels"]: _install_wheel(name, wheel, do_resolve, reject) self.installed_packages[name] = ver
async def install(self, requirements: Union[str, List[str]], ctx=None): if ctx is None: ctx = {"extra": None} complete_ctx = dict(markers.DEFAULT_CONTEXT) complete_ctx.update(ctx) if isinstance(requirements, str): requirements = [requirements] transaction: Dict[str, Any] = { "wheels": [], "pyodide_packages": set(), "locked": dict(self.installed_packages), } requirement_promises = [] for requirement in requirements: requirement_promises.append( self.add_requirement(requirement, complete_ctx, transaction)) await gather(*requirement_promises) wheel_promises = [] # Install built-in packages pyodide_packages = transaction["pyodide_packages"] if len(pyodide_packages): # Note: branch never happens in out-of-browser testing because we # report that all dependencies are empty. self.installed_packages.update( dict((k, None) for k in pyodide_packages)) wheel_promises.append( js_pyodide.loadPackage(list(pyodide_packages))) # Now install PyPI packages for name, wheel, ver in transaction["wheels"]: wheel_promises.append(_install_wheel(name, wheel)) self.installed_packages[name] = ver await gather(*wheel_promises) return f'Installed {", ".join(self.installed_packages.keys())}'