def test_file_plugin(self): available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(self.project_d_fixtures_root): available_plugins = set(discover_plugins()) self.assertSetEqual({"d"}, available_plugins) import_plugins() subcommands_available = Subcommand.list_available() self.assertIn("d", subcommands_available)
def test_file_plugin(self): available_plugins = set(discover_plugins()) assert available_plugins == set() with pushd(self.plugins_root): available_plugins = set(discover_plugins()) assert available_plugins == {"d"} import_plugins() subcommands_available = Subcommand.list_available() assert "d" in subcommands_available
def test_local_namespace_plugin(self): available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(self.project_b_fixtures_root): available_plugins = set(discover_plugins()) self.assertSetEqual({"allennlp_plugins.b"}, available_plugins) import_plugins() subcommands_available = Subcommand.list_available() self.assertIn("b", subcommands_available)
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(plugins_root): main() subcommands_available = Subcommand.list_available() self.assertIn("d", subcommands_available)
def push_python_project(path: PathType) -> ContextManagerFunctionReturnType[None]: """ Changes the current directory to the given path and prepends it to `sys.path`. It simulates the behavior of running a command from a Python's project root directory, which is part of Python's path. This method is intended to use with `with`, so after its usage, the current directory will be set to the previous value and its value removed from `sys.path`. """ with pushd(path), push_python_path("."): yield
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) assert available_plugins == set() with pushd(plugins_root): main() subcommands_available = Subcommand.list_available() assert "d" in subcommands_available
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" # "d" sets a "local" file plugin, because it's supposed to be run from that directory # and has a ".allennlp_plugins" file in it. project_d_fixtures_root = plugins_root / "project_d" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(project_d_fixtures_root): main() subcommands_available = Subcommand.list_available() self.assertIn("d", subcommands_available)
def test_local_namespace_plugin_different_path(self): available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with tempfile.TemporaryDirectory() as temp_dir_b: distutils.dir_util.copy_tree(self.project_b_fixtures_root, temp_dir_b) # We move to another directory with a different plugin "b", as if it were another # separate project which is not installed ("local" usage of the plugin declared in # the namespace). with pushd(temp_dir_b): available_plugins = set(discover_plugins()) self.assertSetEqual({"allennlp_plugins.b"}, available_plugins) import_plugins() subcommands_available = Subcommand.list_available() self.assertIn("b", subcommands_available)
def test_file_and_two_global_namespace_plugins(self): available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) # We make plugins "a" and "c" available as packages, each from other directories, # as if they were separate installed projects ("global" usage of the plugins). # We move to another directory with a different plugin "b", as if it were another separate # project which is not installed ("local" usage of the plugin declared in a file). with pip_install(self.project_a_fixtures_root, "a"), pip_install( self.project_c_fixtures_root, "c" ), pushd(self.project_d_fixtures_root): available_plugins = set(discover_plugins()) self.assertSetEqual( {"allennlp_plugins.a", "allennlp_plugins.c", "d"}, available_plugins ) import_plugins() subcommands_available = Subcommand.list_available() self.assertIn("a", subcommands_available) self.assertIn("c", subcommands_available) self.assertIn("d", subcommands_available)
def _run_test(args: argparse.Namespace): module_parent = _get_module_root().parent logger.info("Changing directory to %s", module_parent) with pushd(module_parent): test_dir = os.path.join(module_parent, "allennlp") logger.info("Running tests at %s", test_dir) if args.k: pytest_k = ["-k", args.k] pytest_m = ["-m", "not java"] if args.run_all: logger.warning("the argument '-k' overwrites '--run-all'.") elif args.run_all: pytest_k = [] pytest_m = [] else: pytest_k = ["-k", "not sniff_test"] pytest_m = ["-m", "not java"] exit_code = pytest.main([test_dir, "--color=no"] + pytest_k + pytest_m) sys.exit(exit_code)
params["trainer"]["cuda_device"] = 0 # train this one to a tempdir tempdir = tempfile.gettempdir() train_model(params, tempdir) # now copy back the weights and and archived model shutil.copy(os.path.join(tempdir, "best.th"), os.path.join(serialization_dir, "best_gpu.th")) shutil.copy(os.path.join(tempdir, "model.tar.gz"), os.path.join(serialization_dir, "model_gpu.tar.gz")) if __name__ == "__main__": module_root = _get_module_root().parent with pushd(module_root, verbose=True): if len(sys.argv) >= 2 and sys.argv[1].lower() == "gpu": train_fixture_gpu("allennlp/tests/fixtures/srl/") else: models = [ ("basic_classifier", "experiment_seq2seq.jsonnet"), "biaffine_dependency_parser", "constituency_parser", "coref", "decomposable_attention", "encoder_decoder/composed_seq2seq", "encoder_decoder/simple_seq2seq", "encoder_decoder/copynet_seq2seq", "esim", "simple_tagger_with_span_f1", "srl",