def test_validate_max_1_html(self): dirpath = MockDir( { "module.json": b"", "module.py": b"", "module.html": b"", "extra.html": b"", } ) with self.assertRaisesRegex(ValueError, "Multiple.*html.*files"): ModuleFiles.load_from_dirpath(dirpath)
def extract_module_messages(directory: pathlib.Path): module_files = ModuleFiles.load_from_dirpath(directory) # raise ValueError source_catalog = _build_source_catalog( ModuleSpec.load_from_path(module_files.spec), pathlib.Path(module_files.code), directory, ) po_path = _po_path(directory, default_locale) try: old_source_catalog = read_po_catalog(po_path) except FileNotFoundError: old_source_catalog = Catalog(default_locale) if not catalogs_are_same(source_catalog, old_source_catalog): write_po_catalog(po_path, source_catalog) fuzzy = find_fuzzy_messages(old_catalog=old_source_catalog, new_catalog=source_catalog) for locale_id in supported_locales: if locale_id != default_locale: po_path = _po_path(directory, locale_id) try: old_catalog = read_po_catalog(po_path) except FileNotFoundError: old_catalog = Catalog(locale_id) catalog = _merge_nonsource_catalog(locale_id, old_catalog, source_catalog, fuzzy) if not catalogs_are_same(catalog, old_catalog): write_po_catalog(po_path, catalog)
def test_happy_path(self): dirpath = MockDir( {"module.json": b"", "module.py": b"", "module.html": b"", "module.js": b""} ) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.spec.name, "module.json") self.assertEqual(module_files.code.name, "module.py") self.assertEqual(module_files.html.name, "module.html") self.assertEqual(module_files.javascript.name, "module.js")
def test_ignore_package_json(self): dirpath = MockDir( { "module.json": b"", "module.py": b"", "package.json": b"", "package-lock.json": b"", } ) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.spec.name, "module.json")
def import_module_from_directory(version: str, importdir: Path, force_reload=False): module_files = ModuleFiles.load_from_dirpath(importdir) # raise ValueError spec = ModuleSpec.load_from_path(module_files.spec) # raise ValueError cjwstate.modules.kernel.compile(module_files.code, spec.id_name) if not force_reload: # Don't allow importing the same version twice try: ModuleVersion.objects.get(id_name=spec.id_name, source_version_hash=version) raise ValueError(f"Version {version} of module {spec.id_name}" " has already been imported") except ModuleVersion.DoesNotExist: # this is what we want pass if module_files.javascript: js_module = module_files.javascript.read_text(encoding="utf-8") else: js_module = "" # Copy whole directory to S3 prefix = "%s/%s/" % (spec.id_name, version) try: # If files already exist, delete them so we can overwrite them. # # This can race: a fetcher/renderer may be loading the code to execute # it. But races are unlikely to affect anybody because: # # * If force_reload=True we're in dev or test, where we control # everything. # * Otherwise, we've already checked there's no ModuleVersion, so # probably nothing is trying and load what we're deleting here. minio.remove_recursive(minio.ExternalModulesBucket, prefix) except FileNotFoundError: pass # common case: we aren't overwriting code minio.fput_directory_contents(minio.ExternalModulesBucket, prefix, Path(importdir)) # If that succeeds, initialise module in our database module_version = ModuleVersion.create_or_replace_from_spec( spec, source_version_hash=version, js_module=js_module) logger.info("Imported module %s" % spec.id_name) return module_version
def test_ignore_test_py(self): dirpath = MockDir({"module.json": b"", "module.py": b"", "test_module.py": b""}) module_files = ModuleFiles.load_from_dirpath(dirpath) self.assertEqual(module_files.code.name, "module.py")
def test_validate_missing_py(self): dirpath = MockDir({"module.json": b""}) with self.assertRaisesRegex(ValueError, 'Missing ".py" module-code file'): ModuleFiles.load_from_dirpath(dirpath)
def test_validate_missing_json(self): dirpath = MockDir({"module.py": b""}) with self.assertRaisesRegex( ValueError, 'Missing ".json" or ".yaml" module-spec file' ): ModuleFiles.load_from_dirpath(dirpath)
def test_validate_extra_py(self): dirpath = MockDir({"module.json": b"{}", "module.py": b"", "module2.py": b""}) with self.assertRaisesRegex(ValueError, "Multiple.*py.*files"): ModuleFiles.load_from_dirpath(dirpath)