-
Notifications
You must be signed in to change notification settings - Fork 5
bug/medcat: CU-869bbj5u4 Fix core dependencies #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mart-r
wants to merge
8
commits into
main
Choose a base branch
from
bug/medcat/CU-869bbj5u4-fix-base-deps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2af1952
CU-869bbj5u4: Add explict checks for optional deps for conversion mod…
mart-r 163f0e6
CU-869bbj5u4: Add missing core dependencies (packaging, pyyaml, reque…
mart-r 51a1416
CU-869bbj5u4: Add script to be able to test taht base install can imp…
mart-r 8dd68da
CU-869bbj5u4: Rename base install check module
mart-r 58825ac
CU-869bbj5u4: Include __main__ in import checks
mart-r 82d03f2
CU-869bbj5u4: Update workflow to add check of base installs
mart-r aba75ea
CU-869bbj5u4: Fix python version
mart-r 55b44f3
Merge branch 'main' into bug/medcat/CU-869bbj5u4-fix-base-deps
mart-r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
medcat-v2/tests/other/check_base_install_can_import_all.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| from typing import Iterable | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import re | ||
| from collections import Counter | ||
|
|
||
| MISSING_DEP_PATTERN = re.compile( | ||
| r"The optional dependency set '([\w\-_]*)' is missing") | ||
|
|
||
|
|
||
| def walk_packages(path: str, | ||
| base_pkg_name: str, | ||
| base_path: str = '') -> Iterable[str]: | ||
| if not base_path: | ||
| base_path = path | ||
| pkg_path = path.removeprefix(base_path).replace( | ||
| os.path.sep, '.').strip(".") | ||
| pkg_to_here = f"{base_pkg_name}.{pkg_path}" if pkg_path else base_pkg_name | ||
| for fn in os.listdir(path): | ||
| cur_path = os.path.join(path, fn) | ||
| if os.path.isdir(cur_path) and ( | ||
| not fn.startswith("__") and not fn.endswith("__")): | ||
| yield from walk_packages(cur_path, base_pkg_name=base_pkg_name, | ||
| base_path=base_path) | ||
| continue | ||
| if not fn.endswith(".py"): | ||
| continue | ||
| if fn == "__init__.py": | ||
| yield pkg_to_here | ||
| continue | ||
| yield f"{pkg_to_here}.{fn.removesuffix('.py')}" | ||
|
|
||
|
|
||
| def find_all_modules(package_name, package_path=None): | ||
| """Find all importable modules in a package.""" | ||
| if package_path is None: | ||
| # Import the package to get its path | ||
| try: | ||
| pkg = __import__(package_name) | ||
| package_path = pkg.__path__ | ||
| except ImportError: | ||
| print(f"Could not import {package_name}") | ||
| return [] | ||
|
|
||
| modules = [] | ||
| for modname in walk_packages(package_path[0], | ||
| base_pkg_name=package_name): | ||
| modules.append(modname) | ||
|
|
||
| return modules | ||
|
|
||
|
|
||
| def test_import(module_name): | ||
| """Test if a module can be imported in isolation.""" | ||
| code = f"import {module_name}" | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=30, | ||
| ) | ||
| return result.returncode == 0, result.stderr | ||
|
|
||
|
|
||
| def get_missing_dep_set(error: str) -> str | None: | ||
| err1 = error.strip().split('\n')[-1] | ||
| if "MissingDependenciesError" not in err1: | ||
| return None | ||
| matches = MISSING_DEP_PATTERN.findall(err1) | ||
| if len(matches) != 1: | ||
| raise ValueError(f"Unknown error:\n'{error}'\nLookin at:\n{err1}" | ||
| f"\ngot: {matches}") | ||
| return matches[0] | ||
|
|
||
|
|
||
| def main(): | ||
| if len(sys.argv) < 2: | ||
| print("Usage: python check_imports.py <package_name>") | ||
| sys.exit(1) | ||
|
|
||
| package_name = sys.argv[1] | ||
|
|
||
| print(f"Finding all modules in {package_name}...") | ||
| modules = find_all_modules(package_name) | ||
|
|
||
| if not modules: | ||
| print(f"No modules found in {package_name}") | ||
| sys.exit(1) | ||
|
|
||
| print(f"Found {len(modules)} modules. Testing imports...\n") | ||
|
|
||
| successful = [] | ||
| missing_opt_dep_expl = [] | ||
| failed = [] | ||
|
|
||
| for module in modules: | ||
| success, error = test_import(module) | ||
| if success: | ||
| successful.append(module) | ||
| print(f"✓ {module}") | ||
| elif (missing_dep := get_missing_dep_set(error)): | ||
| missing_opt_dep_expl.append((module, missing_dep)) | ||
| print(f"M {module}: missing {missing_dep}") | ||
| else: | ||
| failed.append((module, error)) | ||
| print(f"✗ {module}") | ||
| # Print the first line of error for quick diagnosis | ||
| first_error_line = ( | ||
| error.strip().split('\n')[-1] if error else "Unknown error") | ||
| print(f" → {first_error_line}") | ||
|
|
||
| # Summary | ||
| print("\n" + "="*60) | ||
| per_opt_dep_missing = Counter() | ||
| for _, missing_dep in missing_opt_dep_expl: | ||
| per_opt_dep_missing[missing_dep] += 1 | ||
| print(f"Results: {len(successful)} successful, " | ||
| f"{len(missing_opt_dep_expl)} missing optional deps " | ||
| f"({per_opt_dep_missing}), {len(failed)} failed") | ||
| print("="*60) | ||
|
|
||
| if failed: | ||
| print("\nFailed imports:") | ||
| for module, error in failed: | ||
| print(f"\n{module}:") | ||
| print(error) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Copilot Autofix
AI 1 day ago
To resolve this issue, add a
permissions:block specifying the minimum required permissions to either the root of the workflow file or to each job. Since neither job requires write access to the repository or other privileged resources, a root-level block withcontents: readis sufficient. This will applycontents: readto all jobs unless overridden. The change should be placed immediately after thename:and before theon:block (commonly, after line 1).