def check(): # find missing packages from importlib.util import find_spec missing = [ requirement for requirement in requirements if not (find_spec(requirement)) ] if not missing: return # install missing packages sys.stdout.write("Installing" + ','.join(missing) + ".\n") # redirect out to nothing so no installing messages will be seen. sys_stdout = sys.stdout sys_stderr = sys.stderr sys.stdout = None sys.stderr = None from pip.commands.install import InstallCommand from pip.status_codes import SUCCESS cmd = InstallCommand() for requirement in requirements: try: if cmd.main([requirement]) is not SUCCESS: sys_stderr.write("Can not install " + requirement + ", program aborts.\n") sys.exit() # this might occur because of redirection of stdout and stderr except AttributeError: pass # direct out back to normal sys.stdout = sys_stdout sys.stderr = sys_stderr sys.stdout.write("All packages are installed, starting game...") sys.stdout.flush()
def install_missing_requirements(module): """ Some of the modules require external packages to be installed. This gets the list from the `REQUIREMENTS` module attribute and attempts to install the requirements using pip. :param module: GPIO module :type module: ModuleType :return: None :rtype: NoneType """ reqs = getattr(module, "REQUIREMENTS", []) if not reqs: _LOG.info("Module %r has no extra requirements to install." % module) return import pkg_resources pkgs_installed = pkg_resources.WorkingSet() pkgs_required = [] for req in reqs: if pkgs_installed.find(pkg_resources.Requirement.parse(req)) is None: pkgs_required.append(req) if pkgs_required: from pip.commands.install import InstallCommand from pip.status_codes import SUCCESS cmd = InstallCommand() result = cmd.main(pkgs_required) if result != SUCCESS: raise CannotInstallModuleRequirements( "Unable to install packages for module %r (%s)..." % ( module, pkgs_required))
def install_missing_requirements(module): """ Some of the modules require external packages to be installed. This gets the list from the `REQUIREMENTS` module attribute and attempts to install the requirements using pip. :param module: GPIO module :type module: ModuleType :return: None :rtype: NoneType """ reqs = getattr(module, "REQUIREMENTS", []) if not reqs: _LOG.info("Module %r has no extra requirements to install." % module) return import pkg_resources pkgs_installed = pkg_resources.WorkingSet() pkgs_required = [] for req in reqs: if pkgs_installed.find(pkg_resources.Requirement.parse(req)) is None: pkgs_required.append(req) if pkgs_required: from pip.commands.install import InstallCommand from pip.status_codes import SUCCESS cmd = InstallCommand() result = cmd.main(pkgs_required) if result != SUCCESS: raise CannotInstallModuleRequirements( "Unable to install packages for module %r (%s)..." % (module, pkgs_required))
def install_package(self, packages, params=None): # type: (str, Optional[str]) -> int try: # For pip <= 19.1.1 install = InstallCommand() except TypeError: # For pip > 19.1.1 install = InstallCommand("Pippel", "Backend server for the Pippel service.") assert packages, "`packages` should not be an empty string." for package in packages.split(): if self.in_virtual_env(): args = [package, "--upgrade"] elif params: args = [package, "--upgrade", "--target", params] else: args = [package, "--upgrade", "--user"] k = install.main(args) return k
def install_missing_requirements(module): """ Some of the modules require external packages to be installed. This gets the list from the `REQUIREMENTS` module attribute and attempts to install the requirements using pip. :param module: Backend module :type module: ModuleType :return: None :rtype: NoneType """ reqs = getattr(module, "REQUIREMENTS", []) if not reqs: print("Module %r has no extra requirements to install." % module) return import pkg_resources pkgs_installed = pkg_resources.WorkingSet() pkgs_required = [] for req in reqs: if req.startswith("git+"): url = urlparse(req) params = { x[0]: x[1] for x in map(lambda y: y.split('='), url.fragment.split('&')) } try: pkg = params["egg"] except KeyError: raise CannotInstallModuleRequirements( "Package %r in module %r must include '#egg=<pkgname>'" % (req, module)) else: pkg = req if pkgs_installed.find(pkg_resources.Requirement.parse(pkg)) is None: pkgs_required.append(req) if pkgs_required: from pip.commands.install import InstallCommand from pip.status_codes import SUCCESS cmd = InstallCommand() result = cmd.main(pkgs_required) if result != SUCCESS: raise CannotInstallModuleRequirements( "Unable to install packages for module %r (%s)..." % (module, pkgs_required))