def pip_install(mod): """Install packages.""" if pkg_util.running_under_virtualenv(): return pkg_util.install_package( mod, constraints=os.path.join( os.path.dirname(__file__), CONSTRAINT_FILE)) return pkg_util.install_package( mod, target=hass.config.path('deps'), constraints=os.path.join( os.path.dirname(__file__), CONSTRAINT_FILE))
def test_install_target(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install with a target.""" target = "target_folder" env = mock_env_copy() env["PYTHONUSERBASE"] = os.path.abspath(target) mock_venv.return_value = False mock_sys.platform = "linux" args = [ mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--user", "--prefix=", ] assert package.install_package(TEST_NEW_REQ, False, target=target) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) assert mock_popen.return_value.communicate.call_count == 1
def test_install_constraint(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test install with constraint file on not installed package.""" env = mock_env_copy() constraints = "constraints_file.txt" assert package.install_package(TEST_NEW_REQ, False, constraints=constraints) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [ mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--constraint", constraints, ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env, ) assert mock_popen.return_value.communicate.call_count == 1
def run(args: List) -> int: """Run a script.""" scripts = [] path = os.path.dirname(__file__) for fil in os.listdir(path): if fil == '__pycache__': continue elif os.path.isdir(os.path.join(path, fil)): scripts.append(fil) elif fil != '__init__.py' and fil.endswith('.py'): scripts.append(fil[:-3]) if not args: print('Please specify a script to run.') print('Available scripts:', ', '.join(scripts)) return 1 if args[0] not in scripts: print('Invalid script specified.') print('Available scripts:', ', '.join(scripts)) return 1 script = importlib.import_module('homeassistant.scripts.' + args[0]) config_dir = extract_config_dir() deps_dir = mount_local_lib_path(config_dir) logging.basicConfig(stream=sys.stdout, level=logging.INFO) for req in getattr(script, 'REQUIREMENTS', []): if not install_package(req, target=deps_dir): print('Aborting scipt, could not install dependency', req) return 1 return script.run(args[1:]) # type: ignore
def pip_install(mod): """Install packages.""" return pkg_util.install_package(mod, target=hass.config.path('deps'), constraints=os.path.join( os.path.dirname(__file__), CONSTRAINT_FILE))
def test_install_existing_package(mock_exists, mock_popen): """Test an install attempt on an existing package.""" mock_exists.return_value = True assert package.install_package(TEST_EXIST_REQ) assert mock_exists.call_count == 1 assert mock_exists.call_args == call(TEST_EXIST_REQ) assert mock_popen.return_value.communicate.call_count == 0
def test_install_target(self, mock_sys, mock_exists, mock_subprocess): """Test an install with a target.""" target = "target_folder" mock_exists.return_value = False mock_subprocess.return_value = 0 self.assertTrue(package.install_package(TEST_NEW_REQ, False, target=target)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual( mock_subprocess.call_args, call( [ mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--target", os.path.abspath(target), ] ), )
def _install(hass: HomeAssistant, req: str, kwargs: Dict) -> bool: """Install requirement.""" progress_path = Path(hass.config.path(PROGRESS_FILE)) progress_path.touch() try: return pkg_util.install_package(req, **kwargs) finally: progress_path.unlink()
def test_install_error(self, mock_sys, mock_logger, mock_exists, mock_subprocess): """Test an install with a target.""" mock_exists.return_value = False mock_subprocess.side_effect = [subprocess.SubprocessError] self.assertFalse(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_logger.exception.call_count, 1)
def test_install_error(caplog, mock_sys, mock_exists, mock_popen, mock_venv): """Test an install with a target.""" caplog.set_level(logging.WARNING) mock_popen.return_value.returncode = 1 assert not package.install_package(TEST_NEW_REQ) assert len(caplog.records) == 1 for record in caplog.records: assert record.levelname == 'ERROR'
def test_install_error(caplog, mock_sys, mock_popen, mock_venv): """Test an install with a target.""" caplog.set_level(logging.WARNING) mock_popen.return_value.returncode = 1 assert not package.install_package(TEST_NEW_REQ) assert len(caplog.records) == 1 for record in caplog.records: assert record.levelname == "ERROR"
def _handle_requirements(component, name): """ Installs requirements for component. """ if hasattr(component, 'REQUIREMENTS'): for req in component.REQUIREMENTS: if not pkg_util.install_package(req): _LOGGER.error('Not initializing %s because could not install ' 'dependency %s', name, req) return False return True
def test_install_existing_package(self, mock_exists, mock_subprocess): """Test an install attempt on an existing package.""" mock_exists.return_value = True self.assertTrue(package.install_package(TEST_EXIST_REQ)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_exists.call_args, call(TEST_EXIST_REQ, None)) self.assertEqual(mock_subprocess.call_count, 0)
def test_install_error(self, mock_sys, mock_logger, mock_exists, mock_popen): """Test an install with a target.""" mock_exists.return_value = False mock_popen.return_value = self.mock_process self.mock_process.returncode = 1 self.assertFalse(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_logger.error.call_count, 1)
def _handle_requirements(hass: core.HomeAssistant, component, name: str) -> bool: """Install the requirements for a component.""" if hass.config.skip_pip or not hasattr(component, "REQUIREMENTS"): return True for req in component.REQUIREMENTS: if not pkg_util.install_package(req, target=hass.config.path("deps")): _LOGGER.error("Not initializing %s because could not install " "dependency %s", name, req) return False return True
def _handle_requirements(hass, component, name): """ Installs requirements for component. """ if hass.config.skip_pip or not hasattr(component, 'REQUIREMENTS'): return True for req in component.REQUIREMENTS: if not pkg_util.install_package(req, target=hass.config.path('lib')): _LOGGER.error('Not initializing %s because could not install ' 'dependency %s', name, req) return False return True
def test_install(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install attempt on a package that doesn't exist.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ, False) assert mock_popen.call_count == 1 assert (mock_popen.call_args == call( [mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)) assert mock_popen.return_value.communicate.call_count == 1
def test_install(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install attempt on a package that doesn't exist.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ, False) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env, ) assert mock_popen.return_value.communicate.call_count == 1
def test_install(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install attempt on a package that doesn't exist.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ, False) assert mock_popen.call_count == 1 assert ( mock_popen.call_args == call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) ) assert mock_popen.return_value.communicate.call_count == 1
def _handle_requirements(component, name): """ Installs requirements for component. """ if not hasattr(component, 'REQUIREMENTS'): return True for req in component.REQUIREMENTS: if not pkg_util.install_package(req): _LOGGER.error( 'Not initializing %s because could not install ' 'dependency %s', name, req) return False return True
def test_install(self, mock_sys, mock_exists, mock_subprocess): """Test an install attempt on a package that doesn't exist.""" mock_exists.return_value = False mock_subprocess.return_value = 0 self.assertTrue(package.install_package(TEST_NEW_REQ, False)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual( mock_subprocess.call_args, call([mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ]) )
def run(args: List) -> int: """Run a script.""" scripts = [] path = os.path.dirname(__file__) for fil in os.listdir(path): if fil == '__pycache__': continue elif os.path.isdir(os.path.join(path, fil)): scripts.append(fil) elif fil != '__init__.py' and fil.endswith('.py'): scripts.append(fil[:-3]) if not args: print('Please specify a script to run.') print('Available scripts:', ', '.join(scripts)) return 1 if args[0] not in scripts: print('Invalid script specified.') print('Available scripts:', ', '.join(scripts)) return 1 script = importlib.import_module('homeassistant.scripts.' + args[0]) config_dir = extract_config_dir() loop = asyncio.get_event_loop() if not is_virtual_env(): loop.run_until_complete(async_mount_local_lib_path(config_dir)) _pip_kwargs = pip_kwargs(config_dir) logging.basicConfig(stream=sys.stdout, level=logging.INFO) hass = HomeAssistant(loop) pkgload = PackageLoadable(hass) for req in getattr(script, 'REQUIREMENTS', []): try: loop.run_until_complete(pkgload.loadable(req)) continue except ImportError: pass returncode = install_package(req, **_pip_kwargs) if not returncode: print('Aborting script, could not install dependency', req) return 1 return script.run(args[1:]) # type: ignore
def test_install_upgrade(self, mock_sys, mock_exists, mock_subprocess): """Test an upgrade attempt on a package.""" mock_exists.return_value = False mock_subprocess.return_value = 0 self.assertTrue(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual( mock_subprocess.call_args, call([mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--upgrade"]), )
def test_install_find_links(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test install with find-links on not installed package.""" env = mock_env_copy() link = 'https://wheels-repository' assert package.install_package(TEST_NEW_REQ, False, find_links=link) assert mock_popen.call_count == 1 assert (mock_popen.call_args == call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--find-links', link ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)) assert mock_popen.return_value.communicate.call_count == 1
def test_install_constraint(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test install with constraint file on not installed package.""" env = mock_env_copy() constraints = 'constraints_file.txt' assert package.install_package( TEST_NEW_REQ, False, constraints=constraints) assert mock_popen.call_count == 1 assert ( mock_popen.call_args == call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--constraint', constraints ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) ) assert mock_popen.return_value.communicate.call_count == 1
def run(args: List) -> int: """Run a script.""" scripts = [] path = os.path.dirname(__file__) for fil in os.listdir(path): if fil == "__pycache__": continue if os.path.isdir(os.path.join(path, fil)): scripts.append(fil) elif fil != "__init__.py" and fil.endswith(".py"): scripts.append(fil[:-3]) if not args: print("Please specify a script to run.") print("Available scripts:", ", ".join(scripts)) return 1 if args[0] not in scripts: print("Invalid script specified.") print("Available scripts:", ", ".join(scripts)) return 1 script = importlib.import_module(f"homeassistant.scripts.{args[0]}") config_dir = extract_config_dir() loop = asyncio.get_event_loop() if not is_virtual_env(): loop.run_until_complete(async_mount_local_lib_path(config_dir)) _pip_kwargs = pip_kwargs(config_dir) logging.basicConfig(stream=sys.stdout, level=logging.INFO) for req in getattr(script, "REQUIREMENTS", []): if is_installed(req): continue if not install_package(req, **_pip_kwargs): print("Aborting script, could not install dependency", req) return 1 asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False)) return script.run(args[1:]) # type: ignore
def test_install_upgrade(self, mock_sys, mock_exists, mock_subprocess): """Test an upgrade attempt on a package.""" mock_exists.return_value = False mock_subprocess.return_value = 0 self.assertTrue(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual( mock_subprocess.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--upgrade' ]) )
def _handle_requirements(hass: core.HomeAssistant, component, name: str) -> bool: """Install the requirements for a component. This method needs to run in an executor. """ if hass.config.skip_pip or not hasattr(component, 'REQUIREMENTS'): return True for req in component.REQUIREMENTS: if not pkg_util.install_package(req, target=hass.config.path('deps')): _LOGGER.error('Not initializing %s because could not install ' 'dependency %s', name, req) _async_persistent_notification(hass, name) return False return True
def test_install_target(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install with a target.""" target = 'target_folder' env = mock_env_copy() env['PYTHONUSERBASE'] = os.path.abspath(target) mock_venv.return_value = False mock_sys.platform = 'linux' args = [ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--user', '--prefix='] assert package.install_package(TEST_NEW_REQ, False, target=target) assert mock_popen.call_count == 1 assert ( mock_popen.call_args == call(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) ) assert mock_popen.return_value.communicate.call_count == 1
def test_install_upgrade(self, mock_sys, mock_exists, mock_popen): """Test an upgrade attempt on a package.""" mock_exists.return_value = False mock_popen.return_value = self.mock_process self.assertTrue(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(self.mock_process.communicate.call_count, 1) self.assertEqual(mock_popen.call_count, 1) self.assertEqual( mock_popen.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--upgrade' ], stdin=PIPE, stdout=PIPE, stderr=PIPE) )
def test_install_target(self, mock_sys, mock_exists, mock_subprocess): """Test an install with a target.""" target = 'target_folder' mock_exists.return_value = False mock_subprocess.return_value = 0 self.assertTrue( package.install_package(TEST_NEW_REQ, False, target=target) ) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(mock_subprocess.call_count, 1) self.assertEqual( mock_subprocess.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--target', os.path.abspath(target) ]) )
def test_install_upgrade(self, mock_sys, mock_exists, mock_popen): """Test an upgrade attempt on a package.""" mock_exists.return_value = False mock_popen.return_value = self.mock_process self.assertTrue(package.install_package(TEST_NEW_REQ)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(self.mock_process.communicate.call_count, 1) self.assertEqual(mock_popen.call_count, 1) self.assertEqual( mock_popen.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--upgrade' ], stdin=PIPE, stdout=PIPE, stderr=PIPE))
def test_install_package_zip(self): """Test an install attempt from a zip path.""" self.assertFalse( package.check_package_exists(TEST_ZIP_REQ, self.lib_dir)) self.assertFalse( package.check_package_exists(TEST_NEW_REQ, self.lib_dir)) self.assertTrue( package.install_package(TEST_ZIP_REQ, True, self.lib_dir)) self.assertTrue( package.check_package_exists(TEST_ZIP_REQ, self.lib_dir)) self.assertTrue( package.check_package_exists(TEST_NEW_REQ, self.lib_dir)) try: import pyhelloworld3 except ImportError: self.fail('Unable to import pyhelloworld3 after installing it.') self.assertEqual(pyhelloworld3.__version__, '1.0.0')
def test_install_upgrade(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an upgrade attempt on a package.""" env = mock_env_copy() assert package.install_package(TEST_NEW_REQ) assert mock_popen.call_count == 1 assert mock_popen.call_args == call( [ mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--upgrade", ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env, ) assert mock_popen.return_value.communicate.call_count == 1
def test_install_target(self, mock_sys, mock_exists, mock_popen): """Test an install with a target.""" target = 'target_folder' mock_exists.return_value = False mock_popen.return_value = self.mock_process self.assertTrue( package.install_package(TEST_NEW_REQ, False, target=target) ) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(self.mock_process.communicate.call_count, 1) self.assertEqual(mock_popen.call_count, 1) self.assertEqual( mock_popen.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--target', os.path.abspath(target) ], stdin=PIPE, stdout=PIPE, stderr=PIPE) )
def test_install_package_zip(self): """Test an install attempt from a zip path.""" self.assertFalse(package.check_package_exists( TEST_ZIP_REQ, self.lib_dir)) self.assertFalse(package.check_package_exists( TEST_NEW_REQ, self.lib_dir)) self.assertTrue(package.install_package( TEST_ZIP_REQ, True, self.lib_dir)) self.assertTrue(package.check_package_exists( TEST_ZIP_REQ, self.lib_dir)) self.assertTrue(package.check_package_exists( TEST_NEW_REQ, self.lib_dir)) try: import pyhelloworld3 except ImportError: self.fail('Unable to import pyhelloworld3 after installing it.') self.assertEqual(pyhelloworld3.__version__, '1.0.0')
def test_install_target(self, mock_sys, mock_exists, mock_popen): """Test an install with a target.""" target = 'target_folder' mock_exists.return_value = False mock_popen.return_value = self.mock_process self.assertTrue( package.install_package(TEST_NEW_REQ, False, target=target)) self.assertEqual(mock_exists.call_count, 1) self.assertEqual(self.mock_process.communicate.call_count, 1) self.assertEqual(mock_popen.call_count, 1) self.assertEqual( mock_popen.call_args, call([ mock_sys.executable, '-m', 'pip', 'install', '--quiet', TEST_NEW_REQ, '--target', os.path.abspath(target) ], stdin=PIPE, stdout=PIPE, stderr=PIPE))
def test_install_find_links(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test install with find-links on not installed package.""" env = mock_env_copy() link = "https://wheels-repository" assert package.install_package(TEST_NEW_REQ, False, find_links=link) assert mock_popen.call_count == 2 assert mock_popen.mock_calls[0] == call( [ mock_sys.executable, "-m", "pip", "install", "--quiet", TEST_NEW_REQ, "--find-links", link, "--prefer-binary", ], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env, ) assert mock_popen.return_value.communicate.call_count == 1
def test_install_target_venv( mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv): """Test an install with a target in a virtual environment.""" target = 'target_folder' with pytest.raises(AssertionError): package.install_package(TEST_NEW_REQ, False, target=target)
def pip_install(mod): """Install packages.""" return pkg_util.install_package( mod, target=hass.config.path('deps'), constraints=os.path.join(os.path.dirname(__file__), CONSTRAINT_FILE))
def test_install_existing_package(self): """Test an install attempt on an existing package.""" self.assertTrue(package.check_package_exists( TEST_EXIST_REQ, self.lib_dir)) self.assertTrue(package.install_package(TEST_EXIST_REQ))
def test_install_existing_package(self): """ Test an install attempt on an existing package """ self.assertTrue( package.check_package_exists(TEST_EXIST_REQ, self.lib_dir)) self.assertTrue(package.install_package(TEST_EXIST_REQ))
def test_install_target_venv(mock_sys, mock_popen, mock_env_copy, mock_venv): """Test an install with a target in a virtual environment.""" target = "target_folder" with pytest.raises(AssertionError): package.install_package(TEST_NEW_REQ, False, target=target)