-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bump version number, dependencies, and linters * Started work to support directory scanning for config files Added stub get_config_lock_file_pairs function and helper find_files function. Added Asyncer dependency. * Add support for scanning a root directory for uv.lock files and hierarchical config files * Allow hierarchical configuration to be used when passing multiple lock file arguments * Updating command help and README documentation * Updating README to describe configuration discovery * Remove some superfluous comments * Added test case for valid file name that doesn't exist
- Loading branch information
1 parent
3f9add6
commit 97fbc55
Showing
12 changed files
with
364 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.2.2" | ||
__version__ = "0.3.0" |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
from uv_secure.dependency_checker.dependency_checker import check_dependencies | ||
from uv_secure.dependency_checker.dependency_checker import ( | ||
check_dependencies, | ||
check_lock_files, | ||
RunStatus, | ||
) | ||
|
||
|
||
__all__ = ["check_dependencies"] | ||
__all__ = ["RunStatus", "check_dependencies", "check_lock_files"] |
This file contains 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 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,4 @@ | ||
from uv_secure.directory_scanner.directory_scanner import get_lock_to_config_map | ||
|
||
|
||
__all__ = ["get_lock_to_config_map"] |
This file contains 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,104 @@ | ||
from collections.abc import Iterable | ||
from typing import Union | ||
|
||
from anyio import Path | ||
from asyncer import create_task_group | ||
|
||
from uv_secure.configuration import config_file_factory, Configuration | ||
|
||
|
||
async def search_file(directory: Path, filename: str) -> list[Path]: | ||
return [file_path async for file_path in directory.glob(f"**/{filename}")] | ||
|
||
|
||
async def _find_files( | ||
directory: Path, filenames: Iterable[str] | ||
) -> dict[str, list[Path]]: | ||
async with create_task_group() as tg: | ||
tasks = { | ||
filename: tg.soonify(search_file)(directory, filename) | ||
for filename in filenames | ||
} | ||
|
||
return {filename: task.value for filename, task in tasks.items()} | ||
|
||
|
||
async def _get_root_dir(file_paths: Iterable[Path]) -> Path: | ||
async with create_task_group() as tg: | ||
tasks = [tg.soonify(path.resolve)() for path in file_paths] | ||
|
||
resolved_paths = [task.value for task in tasks] | ||
if len(resolved_paths) == 1: | ||
return resolved_paths[0].parent | ||
|
||
split_paths = [list(rp.parts) for rp in resolved_paths] | ||
min_length = min(len(parts) for parts in split_paths) | ||
common_prefix_len = 0 | ||
|
||
for part_idx in range(min_length): | ||
segment_set = {parts[part_idx] for parts in split_paths} | ||
if len(segment_set) == 1: | ||
common_prefix_len += 1 | ||
else: | ||
break | ||
|
||
common_parts = split_paths[0][:common_prefix_len] | ||
return Path(*common_parts) | ||
|
||
|
||
async def get_lock_to_config_map( | ||
file_paths: Union[Path, list[Path]], | ||
) -> dict[Path, Configuration]: | ||
"""Get map of uv.lock files to their configurations. | ||
Using provided uv.lock files or root directory discover the uv.lock files and also | ||
find and map the nearest parent configuration for each uv.lock file. | ||
Args: | ||
file_paths: A list of uv.lock files or root directory | ||
Returns: | ||
A dictionary mapping uv.lock files to their nearest Configuration | ||
""" | ||
if type(file_paths) is Path: | ||
root_dir = file_paths | ||
config_and_lock_files = await _find_files( | ||
root_dir, ["pyproject.toml", "uv-secure.toml", ".uv-secure.toml", "uv.lock"] | ||
) | ||
else: | ||
root_dir = await _get_root_dir(file_paths) | ||
config_and_lock_files = await _find_files( | ||
root_dir, ["pyproject.toml", "uv-secure.toml", ".uv-secure.toml"] | ||
) | ||
config_and_lock_files["uv.lock"] = file_paths | ||
|
||
config_file_paths = ( | ||
config_and_lock_files["pyproject.toml"] | ||
+ config_and_lock_files["uv-secure.toml"] | ||
+ config_and_lock_files[".uv-secure.toml"] | ||
) | ||
|
||
async with create_task_group() as tg: | ||
config_futures = [ | ||
tg.soonify(config_file_factory)(path) for path in config_file_paths | ||
] | ||
configs = [future.value for future in config_futures] | ||
path_config_map = { | ||
p.parent: c for p, c in zip(config_file_paths, configs) if c is not None | ||
} | ||
|
||
lock_file_paths = config_and_lock_files.get("uv.lock", []) | ||
lock_to_config_map: dict[Path, Configuration] = {} | ||
default_config = Configuration() | ||
for lock_file in lock_file_paths: | ||
current_dir = lock_file.parent | ||
while True: | ||
found_config = path_config_map.get(current_dir) | ||
if found_config is not None or current_dir == root_dir: | ||
break | ||
current_dir = current_dir.parent | ||
|
||
if found_config is None: | ||
found_config = default_config | ||
lock_to_config_map[lock_file] = found_config | ||
return lock_to_config_map |
Oops, something went wrong.